| 1 |
// gomuks - A terminal Matrix client written in Go. |
| 2 |
// Copyright (C) 2020 Tulir Asokan |
| 3 |
// |
| 4 |
// This program is free software: you can redistribute it and/or modify |
| 5 |
// it under the terms of the GNU Affero General Public License as published by |
| 6 |
// the Free Software Foundation, either version 3 of the License, or |
| 7 |
// (at your option) any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU Affero General Public License for more details. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU Affero General Public License |
| 15 |
// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
package ui |
| 18 |
|
| 19 |
import ( |
| 20 |
"math" |
| 21 |
"sort" |
| 22 |
"strings" |
| 23 |
|
| 24 |
"github.com/mattn/go-runewidth" |
| 25 |
|
| 26 |
"go.mau.fi/mauview" |
| 27 |
"go.mau.fi/tcell" |
| 28 |
|
| 29 |
"maunium.net/go/mautrix/event" |
| 30 |
"maunium.net/go/mautrix/id" |
| 31 |
|
| 32 |
"maunium.net/go/gomuks/matrix/rooms" |
| 33 |
"maunium.net/go/gomuks/ui/widget" |
| 34 |
) |
| 35 |
|
| 36 |
type MemberList struct { |
| 37 |
list roomMemberList |
| 38 |
} |
| 39 |
|
| 40 |
func NewMemberList() *MemberList { |
| 41 |
return &MemberList{} |
| 42 |
} |
| 43 |
|
| 44 |
type memberListItem struct { |
| 45 |
rooms.Member |
| 46 |
PowerLevel int |
| 47 |
Sigil rune |
| 48 |
UserID id.UserID |
| 49 |
Color tcell.Color |
| 50 |
} |
| 51 |
|
| 52 |
type roomMemberList []*memberListItem |
| 53 |
|
| 54 |
func (rml roomMemberList) Len() int { |
| 55 |
return len(rml) |
| 56 |
} |
| 57 |
|
| 58 |
func (rml roomMemberList) Less(i, j int) bool { |
| 59 |
if rml[i].PowerLevel != rml[j].PowerLevel { |
| 60 |
return rml[i].PowerLevel > rml[j].PowerLevel |
| 61 |
} |
| 62 |
return strings.Compare(strings.ToLower(rml[i].Displayname), strings.ToLower(rml[j].Displayname)) < 0 |
| 63 |
} |
| 64 |
|
| 65 |
func (rml roomMemberList) Swap(i, j int) { |
| 66 |
rml[i], rml[j] = rml[j], rml[i] |
| 67 |
} |
| 68 |
|
| 69 |
func (ml *MemberList) Update(data map[id.UserID]*rooms.Member, levels *event.PowerLevelsEventContent) *MemberList { |
| 70 |
ml.list = make(roomMemberList, len(data)) |
| 71 |
i := 0 |
| 72 |
highestLevel := math.MinInt32 |
| 73 |
count := 0 |
| 74 |
for _, level := range levels.Users { |
| 75 |
if level > highestLevel { |
| 76 |
highestLevel = level |
| 77 |
count = 1 |
| 78 |
} else if level == highestLevel { |
| 79 |
count++ |
| 80 |
} |
| 81 |
} |
| 82 |
for userID, member := range data { |
| 83 |
level := levels.GetUserLevel(userID) |
| 84 |
sigil := ' ' |
| 85 |
if level == highestLevel && count == 1 { |
| 86 |
sigil = '~' |
| 87 |
} else if level > levels.StateDefault() { |
| 88 |
sigil = '&' |
| 89 |
} else if level >= levels.Ban() { |
| 90 |
sigil = '@' |
| 91 |
} else if level >= levels.Kick() || level >= levels.Redact() { |
| 92 |
sigil = '%' |
| 93 |
} else if level > levels.UsersDefault { |
| 94 |
sigil = '+' |
| 95 |
} |
| 96 |
ml.list[i] = &memberListItem{ |
| 97 |
Member: *member, |
| 98 |
UserID: userID, |
| 99 |
PowerLevel: level, |
| 100 |
Sigil: sigil, |
| 101 |
Color: widget.GetHashColor(userID), |
| 102 |
} |
| 103 |
i++ |
| 104 |
} |
| 105 |
sort.Sort(ml.list) |
| 106 |
return ml |
| 107 |
} |
| 108 |
|
| 109 |
func (ml *MemberList) Draw(screen mauview.Screen) { |
| 110 |
width, _ := screen.Size() |
| 111 |
sigilStyle := tcell.StyleDefault.Background(tcell.ColorGreen).Foreground(tcell.ColorDefault) |
| 112 |
for y, member := range ml.list { |
| 113 |
if member.Sigil != ' ' { |
| 114 |
screen.SetCell(0, y, sigilStyle, member.Sigil) |
| 115 |
} |
| 116 |
if member.Membership == "invite" { |
| 117 |
widget.WriteLineSimpleColor(screen, member.Displayname, 2, y, member.Color) |
| 118 |
screen.SetCell(1, y, tcell.StyleDefault, '(') |
| 119 |
if sw := runewidth.StringWidth(member.Displayname); sw+2 < width { |
| 120 |
screen.SetCell(sw+2, y, tcell.StyleDefault, ')') |
| 121 |
} else { |
| 122 |
screen.SetCell(width-1, y, tcell.StyleDefault, ')') |
| 123 |
} |
| 124 |
} else { |
| 125 |
widget.WriteLineSimpleColor(screen, member.Displayname, 1, y, member.Color) |
| 126 |
} |
| 127 |
} |
| 128 |
} |