@@ -36,6 +36,16 @@ var vt100EscapeCodes = EscapeCodes{
36
36
Reset : []byte {keyEscape , '[' , '0' , 'm' },
37
37
}
38
38
39
+ type History interface {
40
+ // adds a new line into the history
41
+ Add (string )
42
+
43
+ // retrieves a line from history
44
+ // 0 should be the most recent entry
45
+ // ok = false when out of range
46
+ At (idx int ) (string , bool )
47
+ }
48
+
39
49
// Terminal contains the state for running a VT100 terminal that is capable of
40
50
// reading lines of input.
41
51
type Terminal struct {
@@ -86,9 +96,10 @@ type Terminal struct {
86
96
remainder []byte
87
97
inBuf [256 ]byte
88
98
89
- // history contains previously entered commands so that they can be
99
+ // History allows to replace the default implementation of the history
100
+ // which contains previously entered commands so that they can be
90
101
// accessed with the up and down keys.
91
- history stRingBuffer
102
+ History History
92
103
// historyIndex stores the currently accessed history entry, where zero
93
104
// means the immediately previous entry.
94
105
historyIndex int
@@ -111,6 +122,7 @@ func NewTerminal(c io.ReadWriter, prompt string) *Terminal {
111
122
termHeight : 24 ,
112
123
echo : true ,
113
124
historyIndex : - 1 ,
125
+ History : & stRingBuffer {},
114
126
}
115
127
}
116
128
@@ -497,7 +509,7 @@ func (t *Terminal) handleKey(key rune) (line string, ok bool) {
497
509
t .pos = len (t .line )
498
510
t .moveCursorToPos (t .pos )
499
511
case keyUp :
500
- entry , ok := t .history . NthPreviousEntry (t .historyIndex + 1 )
512
+ entry , ok := t .History . At (t .historyIndex + 1 )
501
513
if ! ok {
502
514
return "" , false
503
515
}
@@ -516,7 +528,7 @@ func (t *Terminal) handleKey(key rune) (line string, ok bool) {
516
528
t .setLine (runes , len (runes ))
517
529
t .historyIndex --
518
530
default :
519
- entry , ok := t .history . NthPreviousEntry (t .historyIndex - 1 )
531
+ entry , ok := t .History . At (t .historyIndex - 1 )
520
532
if ok {
521
533
t .historyIndex --
522
534
runes := []rune (entry )
@@ -781,7 +793,7 @@ func (t *Terminal) readLine() (line string, err error) {
781
793
if lineOk {
782
794
if t .echo {
783
795
t .historyIndex = - 1
784
- t .history .Add (line )
796
+ t .History .Add (line )
785
797
}
786
798
if lineIsPasted {
787
799
err = ErrPasteIndicator
@@ -942,7 +954,7 @@ func (s *stRingBuffer) Add(a string) {
942
954
// If n is zero then the immediately prior value is returned, if one, then the
943
955
// next most recent, and so on. If such an element doesn't exist then ok is
944
956
// false.
945
- func (s * stRingBuffer ) NthPreviousEntry (n int ) (value string , ok bool ) {
957
+ func (s * stRingBuffer ) At (n int ) (value string , ok bool ) {
946
958
if n < 0 || n >= s .size {
947
959
return "" , false
948
960
}
0 commit comments