/
history.go
76 lines (67 loc) · 1.7 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
package readline
import (
"bufio"
"context"
"io"
)
// IHistory is the interface ReadLine can use as container for history.
// It can be set to Editor.History field
type IHistory interface {
Len() int
At(int) string
}
type _EmptyHistory struct{}
// Len always returns zero because the receiver is dummy.
func (*_EmptyHistory) Len() int { return 0 }
// At always returns empty-string because the receiver is dummy.
func (*_EmptyHistory) At(int) string { return "" }
// KeyMap is the class for key-bindings
type KeyMap struct {
KeyMap map[string]KeyFuncT
}
// Editor is the main class to hold the parameter for ReadLine
type Editor struct {
KeyMap
History IHistory
Writer io.Writer
Out *bufio.Writer
Prompt func() (int, error)
Default string
Cursor int
LineFeed func(Result)
OpenKeyGetter func() (KeyGetter, error)
}
func keyFuncHistoryUp(ctx context.Context, this *Buffer) Result {
if this.History.Len() <= 0 {
return CONTINUE
}
if this.historyPointer <= 0 {
this.historyPointer = this.History.Len()
}
this.historyPointer--
keyFuncClear(ctx, this)
if this.historyPointer >= 0 {
this.InsertString(0, this.History.At(this.historyPointer))
this.ViewStart = 0
this.Cursor = 0
keyFuncTail(ctx, this)
}
return CONTINUE
}
func keyFuncHistoryDown(ctx context.Context, this *Buffer) Result {
if this.History.Len() <= 0 {
return CONTINUE
}
if this.historyPointer+1 > this.History.Len() {
return CONTINUE
}
this.historyPointer++
keyFuncClear(ctx, this)
if this.historyPointer < this.History.Len() {
this.InsertString(0, this.History.At(this.historyPointer))
this.ViewStart = 0
this.Cursor = 0
keyFuncTail(ctx, this)
}
return CONTINUE
}