forked from chzyer/readline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory.go
179 lines (161 loc) · 3.29 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package readline
import (
"bufio"
"container/list"
"os"
"strings"
)
type HisItem struct {
Source []rune
Version int64
Tmp []rune
}
func (h *HisItem) Clean() {
h.Source = nil
h.Tmp = nil
}
type opHistory struct {
path string
history *list.List
historyVer int64
current *list.Element
fd *os.File
}
func newOpHistory(path string) (o *opHistory) {
o = &opHistory{
path: path,
history: list.New(),
}
if o.path == "" {
return
}
f, err := os.OpenFile(o.path, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
return
}
o.fd = f
r := bufio.NewReader(o.fd)
for {
line, err := r.ReadSlice('\n')
if err != nil {
break
}
o.PushHistory([]rune(strings.TrimSpace(string(line))))
}
o.historyVer++
o.PushHistory(nil)
return
}
func (o *opHistory) Close() {
if o.fd != nil {
o.fd.Close()
}
}
func (o *opHistory) FindHistoryBck(rs []rune) (int, *list.Element) {
for elem := o.current; elem != nil; elem = elem.Prev() {
idx := RunesIndex(o.showItem(elem.Value), rs)
if idx < 0 {
continue
}
return idx, elem
}
return -1, nil
}
func (o *opHistory) FindHistoryFwd(rs []rune) (int, *list.Element) {
for elem := o.current; elem != nil; elem = elem.Next() {
idx := RunesIndex(o.showItem(elem.Value), rs)
if idx < 0 {
continue
}
return idx, elem
}
return -1, nil
}
func (o *opHistory) showItem(obj interface{}) []rune {
item := obj.(*HisItem)
if item.Version == o.historyVer {
return item.Tmp
}
return item.Source
}
func (o *opHistory) PrevHistory() []rune {
if o.current == nil {
return nil
}
current := o.current.Prev()
if current == nil {
return nil
}
o.current = current
return o.showItem(current.Value)
}
func (o *opHistory) NextHistory() ([]rune, bool) {
if o.current == nil {
return nil, false
}
current := o.current.Next()
if current == nil {
return nil, false
}
o.current = current
return o.showItem(current.Value), true
}
func (o *opHistory) NewHistory(current []rune) {
// if just use last command without modify
// just clean lastest history
if back := o.history.Back(); back != nil {
prev := back.Prev()
if prev != nil {
use := o.current.Value.(*HisItem)
if equalRunes(use.Tmp, prev.Value.(*HisItem).Source) {
o.current = o.history.Back()
o.current.Value.(*HisItem).Clean()
o.historyVer++
return
}
}
}
if len(current) == 0 {
o.current = o.history.Back()
if o.current != nil {
o.current.Value.(*HisItem).Clean()
o.historyVer++
return
}
}
if o.current != o.history.Back() {
// move history item to current command
use := o.current.Value.(*HisItem)
o.current = o.history.Back()
current = use.Tmp
}
o.UpdateHistory(current, true)
// push a new one to commit current command
o.historyVer++
o.PushHistory(nil)
}
func (o *opHistory) UpdateHistory(s []rune, commit bool) {
if o.current == nil {
o.PushHistory(s)
return
}
r := o.current.Value.(*HisItem)
r.Version = o.historyVer
if commit {
r.Source = make([]rune, len(s))
copy(r.Source, s)
if o.fd != nil {
o.fd.Write([]byte(string(r.Source) + "\n"))
}
} else {
r.Tmp = append(r.Tmp[:0], s...)
}
o.current.Value = r
}
func (o *opHistory) PushHistory(s []rune) {
// copy
newCopy := make([]rune, len(s))
copy(newCopy, s)
elem := o.history.PushBack(&HisItem{Source: newCopy})
o.current = elem
}