forked from layeh/barnard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui_tree.go
79 lines (68 loc) · 1.39 KB
/
ui_tree.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"layeh.com/barnard/uiterm"
"layeh.com/gumble/gumble"
)
type TreeItem struct {
User *gumble.User
Channel *gumble.Channel
}
func (ti TreeItem) String() string {
if ti.User != nil {
return ti.User.Name
}
if ti.Channel != nil {
return ti.Channel.Name
}
return ""
}
func (ti TreeItem) TreeItemStyle(fg, bg uiterm.Attribute, active bool) (uiterm.Attribute, uiterm.Attribute) {
if ti.Channel != nil {
fg |= uiterm.AttrBold
}
if active {
fg, bg = bg, fg
}
return fg, bg
}
func (b *Barnard) TreeItemSelect(ui *uiterm.Ui, tree *uiterm.Tree, item uiterm.TreeItem) {
treeItem := item.(TreeItem)
if treeItem.Channel != nil {
b.Client.Self.Move(treeItem.Channel)
}
}
func (b *Barnard) TreeItem(item uiterm.TreeItem) []uiterm.TreeItem {
if b.Client == nil {
return nil
}
var treeItem TreeItem
if ti, ok := item.(TreeItem); !ok {
root := b.Client.Channels[0]
if root == nil {
return nil
}
return []uiterm.TreeItem{
TreeItem{
Channel: root,
},
}
} else {
treeItem = ti
}
if treeItem.User != nil {
return nil
}
users := []uiterm.TreeItem{}
for _, user := range treeItem.Channel.Users {
users = append(users, TreeItem{
User: user,
})
}
channels := []uiterm.TreeItem{}
for _, subchannel := range treeItem.Channel.Children {
channels = append(channels, TreeItem{
Channel: subchannel,
})
}
return append(users, channels...)
}