Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(history): add ignore duplicates option #231

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions history.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,25 @@ package prompt

// History stores the texts that are entered.
type History struct {
histories []string
tmp []string
selected int
histories []string
tmp []string
selected int
ignoreDuplicates bool
}

// Add to add text in history.
func (h *History) Add(input string) {
h.histories = append(h.histories, input)
if h.ignoreDuplicates {
var histories []string
for _, history := range h.histories {
if history != input {
histories = append(histories, history)
}
}
h.histories = append(histories, input)
} else {
h.histories = append(h.histories, input)
}
h.Clear()
}

Expand Down
31 changes: 25 additions & 6 deletions history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ func TestHistoryClear(t *testing.T) {
h.Add("foo")
h.Clear()
expected := &History{
histories: []string{"foo"},
tmp: []string{"foo", ""},
selected: 1,
histories: []string{"foo"},
tmp: []string{"foo", ""},
selected: 1,
ignoreDuplicates: false,
}
if !reflect.DeepEqual(expected, h) {
t.Errorf("Should be %#v, but got %#v", expected, h)
Expand All @@ -23,9 +24,27 @@ func TestHistoryAdd(t *testing.T) {
h := NewHistory()
h.Add("echo 1")
expected := &History{
histories: []string{"echo 1"},
tmp: []string{"echo 1", ""},
selected: 1,
histories: []string{"echo 1"},
tmp: []string{"echo 1", ""},
selected: 1,
ignoreDuplicates: false,
}
if !reflect.DeepEqual(h, expected) {
t.Errorf("Should be %v, but got %v", expected, h)
}
}

func TestHistoryAddIgnoreDuplicates(t *testing.T) {
h := NewHistory()
h.ignoreDuplicates = true
h.Add("echo 1")
h.Add("echo 2")
h.Add("echo 1")
expected := &History{
histories: []string{"echo 2", "echo 1"},
tmp: []string{"echo 2", "echo 1", ""},
selected: 2,
ignoreDuplicates: true,
}
if !reflect.DeepEqual(h, expected) {
t.Errorf("Should be %v, but got %v", expected, h)
Expand Down
9 changes: 9 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,15 @@ func OptionHistory(x []string) Option {
}
}

// OptionHistoryIgnoreDuplicates to set history not display a line previously found
func OptionHistoryIgnoreDuplicates() Option {
return func(p *Prompt) error {
p.history.ignoreDuplicates = true
p.history.Clear()
return nil
}
}

// OptionSwitchKeyBindMode set a key bind mode.
func OptionSwitchKeyBindMode(m KeyBindMode) Option {
return func(p *Prompt) error {
Expand Down