-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
148 lines (132 loc) · 3.35 KB
/
list.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package client
import (
"fmt"
"github.com/gdamore/tcell/v2"
"github.com/notassigned/tweb/xmlnode"
tview "github.com/rivo/tview"
)
type ListView struct {
client *Client
list *tview.List
events chan *xmlnode.XmlNode
}
func (client *Client) CreateListView(n *xmlnode.XmlNode, events chan *xmlnode.XmlNode) (update func(*xmlnode.XmlNode), node func() *xmlnode.XmlNode, p tview.Primitive) {
listView := &ListView{
client: client,
list: tview.NewList(),
events: events,
}
listView.list.SetSecondaryTextStyle(tcell.StyleDefault.Background(tcell.ColorBlack).Foreground(tcell.ColorDimGray))
listView.list.SetMainTextStyle(tcell.StyleDefault.Background(tcell.ColorBlack).Foreground(tcell.ColorGray))
listView.list.SetMainTextStyle(tcell.StyleDefault.Background(tcell.ColorBlack).Foreground(tcell.ColorWhite))
listView.list.ShowSecondaryText(false)
listView.list.SetSelectedFocusOnly(true)
listView.updateListView(n)
listView.list.SetSelectedFunc(func(i int, s1, s2 string, r rune) {
events <- xmlnode.Node("selection", map[string]string{
"id": n.Attributes["id"],
"index": fmt.Sprint(i),
"key": tcell.KeyNames[tcell.Key(r)],
})
})
return listView.updateListView, node, listView.list
}
func (l *ListView) updateListView(node *xmlnode.XmlNode) {
list := l.list
l.AddItems(node.Nodes)
for a, p := range node.Attributes {
switch a {
case "removesecondary":
indices := list.FindItems("", p, false, false)
for _, i := range indices {
if _, text := list.GetItemText(i); text == p {
list.RemoveItem(i)
}
}
case "remove":
indices := list.FindItems(p, "", false, false)
for _, i := range indices {
if text, _ := list.GetItemText(i); text == p {
list.RemoveItem(i)
}
}
case "color":
color, err := colorLookup(p)
if err == nil {
list.SetMainTextColor(color)
}
case "background":
color, err := colorLookup(p)
if err == nil {
list.SetBackgroundColor(color)
}
case "selectedbackground":
color, err := colorLookup(p)
if err == nil {
list.SetSelectedBackgroundColor(color)
}
case "selectedcolor":
color, err := colorLookup(p)
if err == nil {
list.SetSelectedTextColor(color)
}
case "secondarycolor":
color, err := colorLookup(p)
if err == nil {
list.SetSecondaryTextColor(color)
}
case "title":
list.SetTitle(p)
case "titlecolor":
color, err := colorLookup(p)
if err == nil {
list.SetTitleColor(color)
}
case "titlealign":
list.SetTitleAlign(alignLookup(p))
case "border":
if p == "true" {
list.SetBorder(true)
}
case "bordercolor":
bcolor, err := colorLookup(p)
if err == nil {
list.SetBorderColor(bcolor)
}
}
}
}
func (l *ListView) ClearItems() {
l.list.Clear()
}
func (l *ListView) AddItems(nodes []*xmlnode.XmlNode) {
list := l.list
var count = 0
for _, n := range nodes {
if n.XMLName.Local == "item" {
var r rune
text := string(n.Content)
var secondaryText string = ""
for a, p := range n.Attributes {
switch a {
case "text":
text = p
case "secondary":
secondaryText = p
case "showsecondary":
if p == "true" {
list.ShowSecondaryText(true)
} else {
list.ShowSecondaryText(false)
}
case "selected":
if p == "true" {
list.SetCurrentItem(count)
}
}
}
list.AddItem(text, secondaryText, r, nil)
count++
}
}
}