forked from chzyer/readline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory.go
56 lines (50 loc) · 1.08 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
package readline
func (o *Operation) PrevHistory() []rune {
if o.current == nil {
return nil
}
current := o.current.Prev()
if current == nil {
return nil
}
o.current = current
return current.Value.([]rune)
}
func (o *Operation) NextHistory() []rune {
if o.current == nil {
return nil
}
current := o.current.Next()
if current == nil {
return nil
}
o.current = current
return current.Value.([]rune)
}
func (o *Operation) NewHistory(current []rune) {
o.UpdateHistory(current)
if o.current != o.history.Back() {
// move history item to current command
o.history.Remove(o.current)
use := o.current.Value.([]rune)
o.current = o.history.Back()
o.UpdateHistory(use)
}
// push a new one to commit current command
o.PushHistory(nil)
}
func (o *Operation) UpdateHistory(s []rune) {
if o.current == nil {
o.PushHistory(s)
return
}
r := o.current.Value.([]rune)
o.current.Value = append(r[:0], s...)
}
func (o *Operation) PushHistory(s []rune) {
// copy
newCopy := make([]rune, len(s))
copy(newCopy, s)
elem := o.history.PushBack(newCopy)
o.current = elem
}