diff options
Diffstat (limited to 'internal/model/undo.go')
| -rw-r--r-- | internal/model/undo.go | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/internal/model/undo.go b/internal/model/undo.go new file mode 100644 index 0000000..5b0297b --- /dev/null +++ b/internal/model/undo.go @@ -0,0 +1,42 @@ +package model + +type UndoStack struct { + states []Entry + pos int +} + +type Entry struct { + Target *Map + State *Map +} + +func (u *UndoStack) Push(target *Map) { + keep := u.pos + 1 + if keep > len(u.states) { + keep = len(u.states) + } + u.states = append(u.states[:keep], Entry{Target: target, State: target.Clone()}) + u.pos = len(u.states) - 1 + if len(u.states) > 100 { + u.states = u.states[1:] + u.pos-- + } +} + +func (u *UndoStack) Undo() *Entry { + if u.pos <= 0 { + return nil + } + u.pos-- + return &u.states[u.pos] +} + +func (u *UndoStack) Redo() *Entry { + if u.pos >= len(u.states)-1 { + return nil + } + u.pos++ + return &u.states[u.pos] +} + +func (u *UndoStack) Pos() int { return u.pos } |
