From 5c11b7e185de2fdbe09db138df8e39323bbd68a5 Mon Sep 17 00:00:00 2001 From: David Florness Date: Fri, 31 Dec 2021 21:38:05 -0600 Subject: [PATCH] TUI: don't display user ID more than once if user rejoins --- ui/tui.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/ui/tui.go b/ui/tui.go index 8c9ebac..79f3d48 100644 --- a/ui/tui.go +++ b/ui/tui.go @@ -430,14 +430,23 @@ func ElectionWaitTUI(kill <-chan error, client *mautrix.Client, el *election.Ele el.RUnlock() update := func() { el.RLock() - // TODO: handle duplicate joins from one UserID + defer el.RUnlock() // TODO: handle users who joined after cutoff voters := make([]string, 0, len(el.Joins)) for _, voter := range el.Joins { - voters = append(voters, voter.JoinEvt.Sender.String()) + name := voter.JoinEvt.Sender.String() + i := sort.Search(len(voters), func(i int) bool { + return name <= voters[i] + }) + if i >= len(voters) { + voters = append(voters, name) + } else if voters[i] == name { + // voter already in map; skip to keep unique + continue + } else { + voters = append(voters[:i], append([]string{name}, voters[i:]...)...) + } } - el.RUnlock() - sort.Strings(voters) text := strings.Join(voters, "\n") text = "Joined voters:\n" + text votersTextView.SetText(text) -- 2.38.4