Skip to content

Commit

Permalink
Don't add adjacent duplicates to history (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldemailly authored Aug 8, 2024
1 parent 113a4d5 commit 7796a13
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
4 changes: 3 additions & 1 deletion terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,9 @@ func (s *stRingBuffer) Add(a string) {
s.entries = make([]string, defaultNumEntries)
s.max = defaultNumEntries
}

if s.entries[s.head] == a {
return // already there at the top
}
s.head = (s.head + 1) % s.max
s.entries[s.head] = a
if s.size < s.max {
Expand Down
28 changes: 28 additions & 0 deletions terminal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ package term

import (
"bytes"
"errors"
"io"
"os"
"reflect"
"runtime"
"testing"
)
Expand Down Expand Up @@ -435,3 +437,29 @@ func TestOutputNewlines(t *testing.T) {
t.Errorf("incorrect output: was %q, expected %q", output, expected)
}
}

func TestHistoryNoDuplicates(t *testing.T) {
c := &MockTerminal{
toSend: []byte("a\rb\rb\rb\rc\r"), // 5 with 3 duplicate "b"
bytesPerRead: 1,
}
ss := NewTerminal(c, "> ")
count := 0
for {
_, err := ss.ReadLine()
if errors.Is(err, io.EOF) {
break
}
count++
}
if count != 5 {
t.Errorf("expected 5 lines, got %d", count)
}
h := ss.History()
if len(h) != 3 {
t.Errorf("history length should be 3, got %d", len(h))
}
if !reflect.DeepEqual(h, []string{"c", "b", "a"}) {
t.Errorf("history unexpected: %v", h)
}
}

0 comments on commit 7796a13

Please sign in to comment.