-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
33 lines (24 loc) · 854 Bytes
/
Copy pathmain.py
File metadata and controls
33 lines (24 loc) · 854 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""Demo: an editing session with undo, redo, and a command log."""
from __future__ import annotations
from patterns.behavioral.command.examples.editor_undo.editing import (
delete_span,
insert_text,
replace_span,
)
from patterns.behavioral.command.examples.editor_undo.models import Document
from patterns.behavioral.command.pattern import UndoStack
def main() -> None:
doc = Document()
history = UndoStack()
history.push(insert_text(doc, 0, "hello world"))
history.push(replace_span(doc, 0, 5, "goodbye"))
history.push(delete_span(doc, 7, 6))
print(f"after edits: {doc.text!r}")
history.undo()
history.undo()
print(f"after 2 undos: {doc.text!r}")
history.redo()
print(f"after redo: {doc.text!r}")
print("session log:", " | ".join(history.log()))
if __name__ == "__main__":
main()