diff --git a/common.go b/common.go index a85b025..62ae3b3 100644 --- a/common.go +++ b/common.go @@ -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) @@ -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) {