diff --git a/terminal.go b/terminal.go index b4a79d1..74ce989 100644 --- a/terminal.go +++ b/terminal.go @@ -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 { diff --git a/terminal_test.go b/terminal_test.go index d5c1794..97a5e5f 100644 --- a/terminal_test.go +++ b/terminal_test.go @@ -6,8 +6,10 @@ package term import ( "bytes" + "errors" "io" "os" + "reflect" "runtime" "testing" ) @@ -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) + } +}