From d226dc30b48444ce8cfa10f519e31463d1592168 Mon Sep 17 00:00:00 2001 From: David Florness Date: Sun, 14 Feb 2021 21:31:01 -0500 Subject: [PATCH] Put rooms with elections at the top of the rooms TUI list --- ui/tui.go | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/ui/tui.go b/ui/tui.go index 7877f60..ef236b5 100644 --- a/ui/tui.go +++ b/ui/tui.go @@ -24,7 +24,8 @@ func RoomListTUI(client *mautrix.Client, elections *election.ElectionsMap) (room if err != nil { return } - // sorts rooms by name (putting rooms without names at the end) + // sorts rooms by name (putting rooms without names at the end and + // putting all rooms that have elections at the top) cmpRooms := func(i, j id.RoomID) bool { roomI := client.Store.LoadRoom(i) roomJ := client.Store.LoadRoom(j) @@ -34,6 +35,16 @@ func RoomListTUI(client *mautrix.Client, elections *election.ElectionsMap) (room if roomI == nil { return false } + elections.RLock() + _, iHasElections := elections.L[i] + _, jHasElections := elections.L[j] + elections.RUnlock() + if iHasElections && !jHasElections { + return true + } + if jHasElections && !iHasElections { + return false + } var nameI, nameJ string if nameEvt, ok := roomJ.State[event.StateRoomName][""]; ok { nameJ = nameEvt.Content.AsRoomName().Name @@ -47,24 +58,30 @@ func RoomListTUI(client *mautrix.Client, elections *election.ElectionsMap) (room } return nameI < nameJ } - // try to sort by room name (if available) - sort.Slice(resp.JoinedRooms, func(i, j int) bool { - return cmpRooms(resp.JoinedRooms[i], resp.JoinedRooms[j]) - }) list := tview.NewList() list.SetTitle("Select a room").SetBorder(true) - for _, roomID := range resp.JoinedRooms { - list.AddItem(roomID.String(), roomID.String(), 0, nil) - } update := func() { + // sort rooms + sort.Slice(resp.JoinedRooms, func(i, j int) bool { + return cmpRooms(resp.JoinedRooms[i], resp.JoinedRooms[j]) + }) for i, roomID := range resp.JoinedRooms { + if list.GetItemCount() <= i { + list.AddItem(roomID.String(), roomID.String(), 0, nil) + } room := client.Store.LoadRoom(roomID) if room == nil { continue } if roomNameEvent, ok := room.State[event.StateRoomName][""]; ok { name := roomNameEvent.Content.AsRoomName().Name + electionCount := len(elections.L[roomID]) + if electionCount > 0 { + name = fmt.Sprintf("%s (%d elections)", name, electionCount) + } + elections.RLock() list.SetItemText(i, name, roomID.String()) + elections.RUnlock() } } } -- 2.38.4