~edwargix/tallyard

d226dc30b48444ce8cfa10f519e31463d1592168 — David Florness 4 years ago e97461e
Put rooms with elections at the top of the rooms TUI list
1 files changed, 25 insertions(+), 8 deletions(-)

M ui/tui.go
M ui/tui.go => ui/tui.go +25 -8
@@ 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()
			}
		}
	}