-
Notifications
You must be signed in to change notification settings - Fork 16
/
symbolwidget.go
168 lines (155 loc) · 4.52 KB
/
symbolwidget.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"github.com/xyproto/vt100"
)
// SymbolWidget represents a TUI widget for presenting a menu with choices for the user
type SymbolWidget struct {
title string // title
w uint // width
h uint // height (number of menu items)
x uint // current position
oldx uint // previous position
y uint // current position
oldy uint // previous position
marginLeft int // margin, may be negative?
marginTop int // margin, may be negative?
choices [][]string // a slice of a slice of menu items
titleColor vt100.AttributeColor // title color (above the choices)
textColor vt100.AttributeColor // text color (the choices that are not highlighted)
highlightColor vt100.AttributeColor // selected color (the choice that has been selected after return has been pressed)
bgColor vt100.AttributeColor // background color
}
// NewSymbolWidget creates a new SymbolWidget
func NewSymbolWidget(title string, choices [][]string, titleColor, textColor, highlightColor, bgColor vt100.AttributeColor, canvasWidth, canvasHeight uint) *SymbolWidget {
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 := 8
if int(canvasHeight)-(len(choices)+marginTop) <= 8 {
marginTop = 2
} else if int(canvasHeight)-(len(choices)+marginTop) <= 0 {
marginTop = 0
}
return &SymbolWidget{
title: title,
w: uint(marginLeft + int(maxlen)),
h: uint(len(choices)),
x: 0,
oldx: 0,
y: 0,
oldy: 0,
marginLeft: marginLeft,
marginTop: marginTop,
choices: choices,
titleColor: titleColor,
textColor: textColor,
highlightColor: highlightColor,
bgColor: bgColor,
}
}
// Selected returns the currently selected item
func (sw *SymbolWidget) Selected() (int, int) {
return int(sw.x), int(sw.y)
}
// Draw will draw this menu widget on the given canvas
func (sw *SymbolWidget) Draw(c *vt100.Canvas) {
// Draw the title
titleHeight := 2
for x, r := range sw.title {
c.PlotColor(uint(sw.marginLeft+x), uint(sw.marginTop), sw.titleColor, r)
}
// Draw the menu entries, with various colors
for y := 0; y < len(sw.choices); y++ {
row := sw.choices[y]
for x := 0; x < len(row); x++ {
symbol := sw.choices[y][x]
//vt100.SetXY(0, uint(sw.marginTop+y+titleHeight))
if y == int(sw.y) && x == int(sw.x) {
c.Write(uint(sw.marginLeft+x*2), uint(sw.marginTop+y+titleHeight), sw.highlightColor, sw.bgColor, symbol)
} else {
c.Write(uint(sw.marginLeft+x*2), uint(sw.marginTop+y+titleHeight), sw.textColor, sw.bgColor, symbol)
}
}
}
}
// Up will move the highlight up (with wrap-around)
func (sw *SymbolWidget) Up(c *vt100.Canvas) bool {
sw.oldy = sw.y
if sw.y <= 0 {
sw.y = uint(len(sw.choices)) - 1
} else {
sw.y--
}
row := sw.choices[sw.y]
if sw.x > uint(len(row)) {
sw.x = uint(len(row) - 1)
}
return true
}
// Left will move the highlight left (with wrap-around)
func (sw *SymbolWidget) Left(c *vt100.Canvas) bool {
sw.oldx = sw.x
if sw.x <= 0 {
row := sw.choices[sw.y]
sw.x = uint(len(row)) - 1
} else {
sw.x--
}
return true
}
// Down will move the highlight down (with wrap-around)
func (sw *SymbolWidget) Down(c *vt100.Canvas) bool {
sw.oldy = sw.y
sw.y++
if sw.y >= uint(len(sw.choices)) {
sw.y = 0
}
row := sw.choices[sw.y]
if sw.x > uint(len(row)) {
sw.x = uint(len(row) - 1)
}
return true
}
// Right will move the highlight right (with wrap-around)
func (sw *SymbolWidget) Right(c *vt100.Canvas) bool {
sw.oldx = sw.x
sw.x++
row := sw.choices[sw.y]
if sw.x >= uint(len(row)) {
sw.x = 0
}
return true
}
// SelectIndex will select a specific index. Returns false if it was not possible.
func (sw *SymbolWidget) SelectIndex(x, y uint) bool {
if y >= sw.h {
return false
}
if x >= sw.w {
return false
}
sw.oldx = sw.x
sw.oldy = sw.y
sw.x = x
sw.y = y
return true
}
// SelectFirst will select the first menu choice
func (sw *SymbolWidget) SelectFirst() bool {
return sw.SelectIndex(0, 0)
}
// SelectLast will select the last menu choice
func (sw *SymbolWidget) SelectLast() bool {
sw.oldx = sw.x
sw.oldy = sw.y
sw.x = sw.w - 1
sw.y = sw.h - 1
return true
}