...
1 package prompt
2
3
4 type History struct {
5 histories []string
6 tmp []string
7 selected int
8 }
9
10
11 func (h *History) Add(input string) {
12 h.histories = append(h.histories, input)
13 h.Clear()
14 }
15
16
17 func (h *History) Clear() {
18 h.tmp = make([]string, len(h.histories))
19 for i := range h.histories {
20 h.tmp[i] = h.histories[i]
21 }
22 h.tmp = append(h.tmp, "")
23 h.selected = len(h.tmp) - 1
24 }
25
26
27
28 func (h *History) Older(buf *Buffer) (new *Buffer, changed bool) {
29 if len(h.tmp) == 1 || h.selected == 0 {
30 return buf, false
31 }
32 h.tmp[h.selected] = buf.Text()
33
34 h.selected--
35 new = NewBuffer()
36 new.InsertText(h.tmp[h.selected], false, true)
37 return new, true
38 }
39
40
41
42 func (h *History) Newer(buf *Buffer) (new *Buffer, changed bool) {
43 if h.selected >= len(h.tmp)-1 {
44 return buf, false
45 }
46 h.tmp[h.selected] = buf.Text()
47
48 h.selected++
49 new = NewBuffer()
50 new.InsertText(h.tmp[h.selected], false, true)
51 return new, true
52 }
53
54
55 func NewHistory() *History {
56 return &History{
57 histories: []string{},
58 tmp: []string{""},
59 selected: 0,
60 }
61 }
62
View as plain text