-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidget.go
143 lines (130 loc) · 3.16 KB
/
widget.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
package main
import (
"github.com/xyproto/vt100"
)
type MenuWidget struct {
hi string // highlight color
arrowColor string
titleColor string
title string // title
fg string // foreground color
active string // active (selected) color
choices []string
y uint // current position
marginLeft int // margin, may be negative?
marginTop int // margin, may be negative?
oldy uint // previous position
selected int
h uint // height (number of menu items)
w uint // width
}
func NewMenuWidget(title, titleColor string, choices []string, fg, hi, active, arrowColor string, canvasWidth, canvasHeight uint) *MenuWidget {
maxlen := uint(0)
for _, choice := range choices {
if uint(len(choice)) > uint(maxlen) {
maxlen = uint(len(choice))
}
}
marginLeft := 10
if int(canvasWidth)-(int(maxlen)+marginLeft) <= 0 {
marginLeft = 0
}
marginTop := 2
if int(canvasHeight)-(len(choices)+marginTop) <= 0 {
marginTop = 0
}
return &MenuWidget{
title: title,
w: uint(marginLeft + int(maxlen)),
h: uint(len(choices)),
y: 0,
oldy: 0,
fg: fg,
hi: hi,
active: active,
marginLeft: marginLeft,
marginTop: marginTop,
choices: choices,
selected: -1,
titleColor: titleColor,
arrowColor: arrowColor,
}
}
func (m *MenuWidget) Selected() int {
return m.selected
}
func (m *MenuWidget) Draw(c *vt100.Canvas) {
// Draw the title
titleHeight := 2
for x, r := range m.title {
c.PlotColor(uint(m.marginLeft+x), uint(m.marginTop), vt100.LightColorMap[m.titleColor], r)
}
// Draw the menu entries, with various colors
ulenChoices := uint(len(m.choices))
for y := uint(0); y < m.h; y++ {
itemString := "---"
if y < ulenChoices {
itemString = "-> " + m.choices[y] + " ---"
}
for x := uint(0); x < m.w; x++ {
r := '-'
if x < uint(len([]rune(itemString))) {
r = []rune(itemString)[x]
}
if x < 2 && y == m.y {
c.PlotColor(uint(m.marginLeft+int(x)), uint(m.marginTop+int(y)+titleHeight), vt100.LightColorMap[m.arrowColor], r)
} else if y == m.y {
c.PlotColor(uint(m.marginLeft+int(x)), uint(m.marginTop+int(y)+titleHeight), vt100.LightColorMap[m.hi], r)
} else {
c.PlotColor(uint(m.marginLeft+int(x)), uint(m.marginTop+int(y)+titleHeight), vt100.LightColorMap[m.fg], r)
}
}
}
}
func (m *MenuWidget) SelectDraw(c *vt100.Canvas) {
old := m.hi
m.hi = m.active
m.Draw(c)
m.hi = old
}
func (m *MenuWidget) Select() {
m.selected = int(m.y)
}
func (m *MenuWidget) Up(c *vt100.Canvas) bool {
m.oldy = m.y
if m.y <= 0 {
m.y = m.h - 1
} else {
m.y--
}
return true
}
func (m *MenuWidget) Down(c *vt100.Canvas) bool {
m.oldy = m.y
m.y++
if m.y >= m.h {
m.y = 0
}
return true
}
// Terminal was resized
func (m *MenuWidget) Resize() {
//m.hi = "Magenta"
}
// Select a specific index, if possible. Returns false if it was not possible.
func (m *MenuWidget) SelectIndex(n uint) bool {
if n >= m.h {
return false
}
m.oldy = m.y
m.y = n
return true
}
func (m *MenuWidget) SelectFirst() bool {
return m.SelectIndex(0)
}
func (m *MenuWidget) SelectLast() bool {
m.oldy = m.y
m.y = m.h - 1
return true
}