From ffe3107397b0b88cfd8b2fe512dc9cf59e234c4f Mon Sep 17 00:00:00 2001 From: Sven Woldt Date: Sat, 8 Apr 2023 20:33:37 +0200 Subject: [PATCH 1/3] deduplicate list --- common.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/common.go b/common.go index a85b025..06b6113 100644 --- a/common.go +++ b/common.go @@ -125,9 +125,30 @@ func (s *State) WriteHistory(w io.Writer) (num int, err error) { } num++ } + s.history = deduplicateList(s.history) 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) { From 281e96323aeba7fdb2d871a37d027c574c4cc3b7 Mon Sep 17 00:00:00 2001 From: Sven Woldt Date: Sat, 8 Apr 2023 20:41:27 +0200 Subject: [PATCH 2/3] fix invoke --- common.go | 2 +- go.mod | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/common.go b/common.go index 06b6113..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) @@ -125,7 +126,6 @@ func (s *State) WriteHistory(w io.Writer) (num int, err error) { } num++ } - s.history = deduplicateList(s.history) return num, nil } diff --git a/go.mod b/go.mod index a246b2d..3a33173 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/peterh/liner +module github.com/go-dockly/liner require ( github.com/mattn/go-runewidth v0.0.3 From ee07260f2e587aed0881c4154d58bf90a1feaa96 Mon Sep 17 00:00:00 2001 From: Sven Woldt Date: Sat, 8 Apr 2023 20:43:39 +0200 Subject: [PATCH 3/3] deduplicate list --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 3a33173..a246b2d 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/go-dockly/liner +module github.com/peterh/liner require ( github.com/mattn/go-runewidth v0.0.3