Skip to content

Commit b9f8be7

Browse files
committed
x/term: exposing history based on golang/go#68780 (comment)
1 parent e770ddd commit b9f8be7

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

terminal.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ var vt100EscapeCodes = EscapeCodes{
3636
Reset: []byte{keyEscape, '[', '0', 'm'},
3737
}
3838

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+
3949
// Terminal contains the state for running a VT100 terminal that is capable of
4050
// reading lines of input.
4151
type Terminal struct {
@@ -86,9 +96,10 @@ type Terminal struct {
8696
remainder []byte
8797
inBuf [256]byte
8898

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
90101
// accessed with the up and down keys.
91-
history stRingBuffer
102+
History History
92103
// historyIndex stores the currently accessed history entry, where zero
93104
// means the immediately previous entry.
94105
historyIndex int
@@ -111,6 +122,7 @@ func NewTerminal(c io.ReadWriter, prompt string) *Terminal {
111122
termHeight: 24,
112123
echo: true,
113124
historyIndex: -1,
125+
History: &stRingBuffer{},
114126
}
115127
}
116128

@@ -497,7 +509,7 @@ func (t *Terminal) handleKey(key rune) (line string, ok bool) {
497509
t.pos = len(t.line)
498510
t.moveCursorToPos(t.pos)
499511
case keyUp:
500-
entry, ok := t.history.NthPreviousEntry(t.historyIndex + 1)
512+
entry, ok := t.History.At(t.historyIndex + 1)
501513
if !ok {
502514
return "", false
503515
}
@@ -516,7 +528,7 @@ func (t *Terminal) handleKey(key rune) (line string, ok bool) {
516528
t.setLine(runes, len(runes))
517529
t.historyIndex--
518530
default:
519-
entry, ok := t.history.NthPreviousEntry(t.historyIndex - 1)
531+
entry, ok := t.History.At(t.historyIndex - 1)
520532
if ok {
521533
t.historyIndex--
522534
runes := []rune(entry)
@@ -781,7 +793,7 @@ func (t *Terminal) readLine() (line string, err error) {
781793
if lineOk {
782794
if t.echo {
783795
t.historyIndex = -1
784-
t.history.Add(line)
796+
t.History.Add(line)
785797
}
786798
if lineIsPasted {
787799
err = ErrPasteIndicator
@@ -942,7 +954,7 @@ func (s *stRingBuffer) Add(a string) {
942954
// If n is zero then the immediately prior value is returned, if one, then the
943955
// next most recent, and so on. If such an element doesn't exist then ok is
944956
// false.
945-
func (s *stRingBuffer) NthPreviousEntry(n int) (value string, ok bool) {
957+
func (s *stRingBuffer) At(n int) (value string, ok bool) {
946958
if n < 0 || n >= s.size {
947959
return "", false
948960
}

0 commit comments

Comments
 (0)