Learning by doing: Reading books and trying to understand the (code) examples
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

47 lines
1.1 KiB

class Document:
def __init__(self):
self.characters = []
self.cursor = Cursor(self)
self.filename = ""
def insert(self, character):
self.characters.insert(self.cursor.position, character)
self.cursor.forward()
def delete(self):
del self.characters[self.cursor.position]
def save(self):
f = open(self.filename, "w")
f.write("".join(self.characters))
f.close()
@property
def string(self):
return "".join(self.characters)
class Cursor:
def __init__(self, document):
self.document = document
self.position = 0
def forward(self):
self.position += 1
def back(self):
self.position -= 1
def home(self):
while self.document.characters[self.position - 1] != "\n":
self.position -= 1
if self.position == 0:
# got to beginning of file before newline
break
def end(self):
while (self.position < len(self.document.characters) and
self.document.characters[self.position] != "\n"):
self.position += 1