gomuks/ui/member-list.go

129 lines
3.3 KiB
Go
Raw Normal View History

2019-04-13 13:27:50 +02:00
// gomuks - A terminal Matrix client written in Go.
2020-04-19 17:10:14 +02:00
// Copyright (C) 2020 Tulir Asokan
2019-04-13 13:27:50 +02:00
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package ui
import (
2020-02-21 23:03:57 +01:00
"math"
2019-04-13 13:27:50 +02:00
"sort"
"strings"
"github.com/mattn/go-runewidth"
2022-04-15 11:53:09 +02:00
"go.mau.fi/mauview"
"go.mau.fi/tcell"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
2019-04-13 16:04:52 +02:00
"maunium.net/go/gomuks/matrix/rooms"
2019-04-13 16:04:52 +02:00
"maunium.net/go/gomuks/ui/widget"
2019-04-13 13:27:50 +02:00
)
type MemberList struct {
list roomMemberList
}
func NewMemberList() *MemberList {
return &MemberList{}
}
type memberListItem struct {
rooms.Member
2019-04-13 13:27:50 +02:00
PowerLevel int
2020-02-21 23:03:57 +01:00
Sigil rune
UserID id.UserID
2019-04-13 13:27:50 +02:00
Color tcell.Color
}
type roomMemberList []*memberListItem
func (rml roomMemberList) Len() int {
return len(rml)
}
func (rml roomMemberList) Less(i, j int) bool {
2019-04-13 16:04:52 +02:00
if rml[i].PowerLevel != rml[j].PowerLevel {
return rml[i].PowerLevel > rml[j].PowerLevel
}
return strings.Compare(strings.ToLower(rml[i].Displayname), strings.ToLower(rml[j].Displayname)) < 0
2019-04-13 13:27:50 +02:00
}
func (rml roomMemberList) Swap(i, j int) {
rml[i], rml[j] = rml[j], rml[i]
}
2020-04-19 14:00:49 +02:00
func (ml *MemberList) Update(data map[id.UserID]*rooms.Member, levels *event.PowerLevelsEventContent) *MemberList {
2019-04-13 13:27:50 +02:00
ml.list = make(roomMemberList, len(data))
i := 0
2020-02-21 23:03:57 +01:00
highestLevel := math.MinInt32
count := 0
for _, level := range levels.Users {
if level > highestLevel {
highestLevel = level
count = 1
} else if level == highestLevel {
count++
}
}
2019-04-13 13:27:50 +02:00
for userID, member := range data {
2020-02-21 23:03:57 +01:00
level := levels.GetUserLevel(userID)
sigil := ' '
if level == highestLevel && count == 1 {
sigil = '~'
} else if level > levels.StateDefault() {
sigil = '&'
} else if level >= levels.Ban() {
sigil = '@'
} else if level >= levels.Kick() || level >= levels.Redact() {
sigil = '%'
} else if level > levels.UsersDefault {
sigil = '+'
}
2019-04-13 13:27:50 +02:00
ml.list[i] = &memberListItem{
Member: *member,
UserID: userID,
2020-02-21 23:03:57 +01:00
PowerLevel: level,
Sigil: sigil,
2019-04-13 13:27:50 +02:00
Color: widget.GetHashColor(userID),
}
i++
}
sort.Sort(ml.list)
return ml
}
func (ml *MemberList) Draw(screen mauview.Screen) {
width, _ := screen.Size()
2021-04-04 00:09:27 +02:00
sigilStyle := tcell.StyleDefault.Background(tcell.ColorGreen).Foreground(tcell.ColorDefault)
2019-04-13 13:27:50 +02:00
for y, member := range ml.list {
2020-02-21 23:03:57 +01:00
if member.Sigil != ' ' {
screen.SetCell(0, y, sigilStyle, member.Sigil)
}
2019-04-13 13:27:50 +02:00
if member.Membership == "invite" {
2020-02-21 23:10:08 +01:00
widget.WriteLineSimpleColor(screen, member.Displayname, 2, y, member.Color)
2020-02-21 23:03:57 +01:00
screen.SetCell(1, y, tcell.StyleDefault, '(')
2020-02-21 23:10:08 +01:00
if sw := runewidth.StringWidth(member.Displayname); sw+2 < width {
screen.SetCell(sw+2, y, tcell.StyleDefault, ')')
2019-04-13 13:27:50 +02:00
} else {
screen.SetCell(width-1, y, tcell.StyleDefault, ')')
}
} else {
2020-02-21 23:03:57 +01:00
widget.WriteLineSimpleColor(screen, member.Displayname, 1, y, member.Color)
2019-04-13 13:27:50 +02:00
}
}
}