-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathundo.py
More file actions
31 lines (27 loc) · 890 Bytes
/
undo.py
File metadata and controls
31 lines (27 loc) · 890 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
from copy import deepcopy
class Undo():
def __init__(self, limit):
self.undo_history = []
self.undo_index = 0
self.limit = limit
def store(self, obj):
while self.undo_index > 0:
_ = self.undo_history.pop(0)
self.undo_index -= 1
self.undo_history.insert(0, deepcopy(obj))
if self.limit:
while self.limit < len(self.undo_history):
_ = self.undo_history.pop()
def undo(self):
if self.undo_index+1 < len(self.undo_history):
self.undo_index += 1
node = deepcopy(self.undo_history[self.undo_index])
return node
else:
return None
def redo(self):
if self.undo_index > 0:
self.undo_index -= 1
return deepcopy(self.undo_history[self.undo_index])
else:
return None