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/dedup-history #160

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func (s *State) ReadHistory(r io.Reader) (num int, err error) {
func (s *State) WriteHistory(w io.Writer) (num int, err error) {
s.historyMutex.RLock()
defer s.historyMutex.RUnlock()
s.history = deduplicateList(s.history)

for _, item := range s.history {
_, err := fmt.Fprintln(w, item)
Expand All @@ -128,6 +129,26 @@ func (s *State) WriteHistory(w io.Writer) (num int, err error) {
return num, nil
}

// deduplicateList map to keep track of encountered strings.
// If not, it adds it to the result slice and sets the encountered flag for that item to true.
// If it has already been encountered, it skips and moves to the next item.
// Returns slice with all duplicates removed.
func deduplicateList(items []string) []string {
var (
ok = map[string]bool{}
result = []string{}
)

for _, item := range items {
if !ok[item] {
ok[item] = true
result = append(result, item)
}
}

return result
}

// AppendHistory appends an entry to the scrollback history. AppendHistory
// should be called iff Prompt returns a valid command.
func (s *State) AppendHistory(item string) {
Expand Down