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

  1. class Document:
  2. def __init__(self):
  3. self.characters = []
  4. self.cursor = Cursor(self)
  5. self.filename = ""
  6. def insert(self, character):
  7. self.characters.insert(self.cursor.position, character)
  8. self.cursor.forward()
  9. def delete(self):
  10. del self.characters[self.cursor.position]
  11. def save(self):
  12. f = open(self.filename, "w")
  13. f.write("".join(self.characters))
  14. f.close()
  15. @property
  16. def string(self):
  17. return "".join(self.characters)
  18. class Cursor:
  19. def __init__(self, document):
  20. self.document = document
  21. self.position = 0
  22. def forward(self):
  23. self.position += 1
  24. def back(self):
  25. self.position -= 1
  26. def home(self):
  27. while self.document.characters[self.position - 1] != "\n":
  28. self.position -= 1
  29. if self.position == 0:
  30. # got to beginning of file before newline
  31. break
  32. def end(self):
  33. while (self.position < len(self.document.characters) and
  34. self.document.characters[self.position] != "\n"):
  35. self.position += 1