From ba34490aaed5f7c242c2b0453130fefc07d0d5d0 Mon Sep 17 00:00:00 2001 From: historia <[not public]> Date: Mon, 29 Jun 2026 04:03:38 -0400 Subject: restructured project --- internal/model/undo.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 internal/model/undo.go (limited to 'internal/model/undo.go') 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 } -- cgit v1.2.3