Browse Source

completed chapter 05 case study

master
T. Meissner 10 years ago
parent
commit
4a09212506
1 changed files with 21 additions and 3 deletions
  1. +21
    -3
      python_3_oop/chapter05/editor.py

+ 21
- 3
python_3_oop/chapter05/editor.py View File

@ -6,6 +6,8 @@ class Document:
self.filename = "" self.filename = ""
def insert(self, character): def insert(self, character):
if not hasattr(character, 'character'):
character = Character(character)
self.characters.insert(self.cursor.position, character) self.characters.insert(self.cursor.position, character)
self.cursor.forward() self.cursor.forward()
@ -19,7 +21,7 @@ class Document:
@property @property
def string(self): def string(self):
return "".join(self.characters)
return "".join((str(c) for c in self.characters))
class Cursor: class Cursor:
@ -35,7 +37,7 @@ class Cursor:
self.position -= 1 self.position -= 1
def home(self): def home(self):
while self.document.characters[self.position - 1] != "\n":
while self.document.characters[self.position - 1].character != "\n":
self.position -= 1 self.position -= 1
if self.position == 0: if self.position == 0:
# got to beginning of file before newline # got to beginning of file before newline
@ -43,5 +45,21 @@ class Cursor:
def end(self): def end(self):
while (self.position < len(self.document.characters) and while (self.position < len(self.document.characters) and
self.document.characters[self.position] != "\n"):
self.document.characters[self.position].character != "\n"):
self.position += 1 self.position += 1
class Character:
def __init__(self, character, bold=False, italic=False, underline=False):
assert len(character) == 1
self.character = character
self.bold = bold
self.italic = italic
self.underline = underline
def __str__(self):
bold = "*" if self.bold else ''
italic = "/" if self.italic else ''
underline = "_" if self.underline else ''
return bold + italic + underline + self.character

Loading…
Cancel
Save