-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabel_view.go
93 lines (80 loc) · 1.74 KB
/
label_view.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
package main
import "github.com/gdamore/tcell/v2"
const truncatedLabelSuffix = "..."
type LabelView struct {
view View
displayText []rune
text string
selected bool
ViewPosition
ViewSize
}
func NewLabelView(view View, text string, selected bool, x, y, maxWidth int) *LabelView {
label := &LabelView{
view,
formatText(text, maxWidth),
text,
selected,
ViewPosition{x, y},
ViewSize{maxWidth, 1},
}
label.update()
return label
}
func (l *LabelView) TextAndSelect(text string, selected bool) {
if text == l.text {
l.Select(selected)
return
}
if selected == l.selected {
l.Text(text)
return
}
l.text = text
l.displayText = formatText(text, l.width)
l.selected = selected
l.update()
}
func (l *LabelView) Text(text string) {
if text == l.text {
return
}
l.text = text
l.displayText = formatText(text, l.width)
l.update()
}
func (l *LabelView) Select(selected bool) {
if selected == l.selected {
return
}
l.selected = selected
l.update()
}
func (l *ListView) SelectKey(key rune) {
l.model.SelectKey(key)
l.sync()
}
func (l *LabelView) Clear() {
if l.selected {
l.view.ClearArea(l.x, l.y, l.width, l.height)
} else {
l.view.ClearArea(l.x, l.y, len(l.displayText), l.height)
}
l.selected = false
l.text = ""
}
func (l *LabelView) update() {
l.view.DrawLabel(l.x, l.y, l.ViewSize.width, l.style(), l.displayText)
}
func (l *LabelView) style() tcell.Style {
if l.selected {
return tcell.StyleDefault.Background(tcell.ColorWhite).Foreground(tcell.ColorBlack)
}
return tcell.StyleDefault
}
func formatText(text string, maxWidth int) []rune {
if len(text) <= maxWidth {
return []rune(text)
}
return append([]rune(text)[:maxWidth-len(truncatedLabelSuffix)], []rune(truncatedLabelSuffix)...)
}