-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory.go
70 lines (60 loc) · 1.52 KB
/
history.go
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"fmt"
"strconv"
)
// Action is a user action.
// It also remember what are done by given action (Action.value).
type Action struct {
kind string
value string
beforeCursor Cursor
afterCursor Cursor
text *Text
}
func (a Action) String() string {
bc := strconv.Itoa(a.beforeCursor.l) + ":" + strconv.Itoa(a.beforeCursor.b)
ac := strconv.Itoa(a.afterCursor.l) + ":" + strconv.Itoa(a.afterCursor.b)
return fmt.Sprintf("(%v, %v, %v, %v)", a.kind, a.value, bc, ac)
}
// History remembers what actions are done by user.
type History struct {
head int
actions [][]*Action
}
// NewHistory create a new History.
func NewHistory() *History {
return &History{
head: 0,
actions: make([][]*Action, 0),
}
}
// Add adds action group to history.
func (h *History) Add(action []*Action) {
h.actions = append(h.actions, action)
h.head++
}
// Cut cuts history slice to given number and return how many were cut.
func (h *History) Cut(to int) int {
b := len(h.actions)
h.actions = h.actions[:to]
h.head = len(h.actions)
a := len(h.actions)
return b - a
}
// Len returns length of action group slice.
func (h *History) Len() int {
return len(h.actions)
}
// At returns action group of given index.
func (h *History) At(i int) []*Action {
return h.actions[i]
}
// Last return last action group of history.
// If history doesn't have any action group, it will return nil.
func (h *History) Last() []*Action {
if len(h.actions) == 0 {
return nil
}
return h.actions[len(h.actions)-1]
}