-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.go
249 lines (209 loc) · 4.8 KB
/
tx.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package main
import (
"strings"
"unicode"
tb "github.com/nsf/termbox-go"
)
type TxPos struct {
X, Y int
}
type TxRect struct {
X, Y, W, H int
}
type TxMargin struct {
T, B, L, R int
}
var TxRect0 = TxRect{}
var TxMargin0 = TxMargin{0, 0, 0, 0}
var TxMargin1 = TxMargin{1, 1, 1, 1}
var TxMarginX = TxMargin{0, 0, 1, 1}
var TxMarginY = TxMargin{1, 1, 0, 0}
type TxWidget interface {
Draw()
HandleEvent(e tb.Event) bool
}
type TxEventCode int
const (
TxEventEnter TxEventCode = iota
TxEventEsc
TxEventSel
)
type TxEvent struct {
Code TxEventCode
Item *TxItem
Detail interface{}
}
type TxEventCB func(we *TxEvent)
type TxFmt uint64
const (
TxFmtNormal TxFmt = 1 << iota
TxFmtCenter
TxFmtBox
)
type TxColor struct {
Fg, Bg tb.Attribute
HighlightFg, HighlightBg tb.Attribute
}
var TxWhite = tb.Attribute(16)
var TxBlack = tb.Attribute(17)
var TxDarkolivegreen = tb.Attribute(156)
var TxDarkorange = tb.Attribute(167)
var TxGrey39 = tb.Attribute(242)
var TxPlum1 = tb.Attribute(220)
var TxGold1 = tb.Attribute(221)
var TxColorBWTerm = TxColor{tb.ColorWhite, tb.ColorBlack, tb.ColorBlack, tb.ColorWhite}
var TxColorWhite = TxColor{TxWhite, TxBlack, TxBlack, TxWhite}
var TxColorBlack = TxColor{TxBlack, TxWhite, TxWhite, TxBlack}
var TxColorGreen = TxColor{TxDarkolivegreen, TxBlack, TxBlack, TxDarkolivegreen}
type TxItem struct {
Id int64
Alias string
Display string
}
type TxProps struct {
Rect TxRect
Margin TxMargin
Clr TxColor
EventCB TxEventCB
Fmt TxFmt
}
func defaultProps() *TxProps {
props := &TxProps{
Rect: TxRect0,
Margin: TxMargin0,
Clr: TxColorWhite,
EventCB: nil,
Fmt: TxFmtNormal,
}
return props
}
func initColor(color *TxColor) {
if color.Fg == 0 {
color.Fg = tb.ColorWhite
}
if color.Bg == 0 {
color.Bg = tb.ColorBlack
}
if color.HighlightFg == 0 {
color.HighlightFg = color.Bg
}
if color.HighlightBg == 0 {
color.HighlightBg = color.Fg
}
}
func addRectMargin(rect TxRect, m TxMargin) TxRect {
return TxRect{rect.X + m.L, rect.Y + m.T, rect.W - m.L - m.R, rect.H - m.T - m.B}
}
func addRectBox(rect TxRect) TxRect {
return TxRect{rect.X - 1, rect.Y - 1, rect.W + 2, rect.H + 2}
}
func print0(s string, x, y int) {
for _, c := range s {
tb.SetChar(x, y, c)
x++
}
}
func print(s string, x, y int, fg, bg tb.Attribute) {
for _, c := range s {
tb.SetCell(x, y, c, fg, bg)
x++
}
}
func printw(s string, x, y int, fg, bg tb.Attribute, w int) {
for i, c := range s {
if i > w-1 {
return
}
tb.SetCell(x, y, c, fg, bg)
x++
}
}
func printspaces(nspace, x, y int, fg, bg tb.Attribute) {
for i := 0; i < nspace; i++ {
tb.SetCell(x+i, y, ' ', fg, bg)
}
}
func printpadded(s string, nleftspace, nrightspace int, x, y int, fg, bg tb.Attribute) {
print(strings.Repeat(" ", nleftspace), x, y, fg, bg)
print(s, x+nleftspace, y, fg, bg)
print(strings.Repeat(" ", nrightspace), x+nleftspace+len(s), y, fg, bg)
}
func printcenter(s string, x, y int, fg, bg tb.Attribute, w int) {
if w > len(s) {
x += w/2 - len(s)/2
}
for _, c := range s {
tb.SetCell(x, y, c, fg, bg)
x++
}
}
func printRect(s string, rect TxRect, fg, bg tb.Attribute) {
ss := lexwords(s)
y := rect.Y
x := rect.X
for _, word := range ss {
if word == "\n" {
y++
x = rect.X
continue
}
// If word exceeds rect, go to next line.
if x+len(word)-1 > rect.X+rect.W-1 {
y++
x = rect.X
}
// Stop if no more rect space to write.
if y > rect.Y+rect.H-1 {
break
}
// Skip over any whitespace at the start of the line (except for the first row).
if len(word) == 1 && unicode.IsSpace([]rune(word)[0]) && x == rect.X && y > rect.Y {
continue
}
print(word, x, y, fg, bg)
x += len(word)
}
}
// "Here's a sentence." ==> ["Here's", " ", "a", " ", "sentence."]
func lexwords(s string) []string {
ss := []string{}
var sb strings.Builder
for _, r := range s {
if unicode.IsSpace(r) {
if sb.Len() > 0 {
ss = append(ss, sb.String())
sb.Reset()
}
ss = append(ss, string(r))
continue
}
sb.WriteRune(r)
}
if sb.Len() > 0 {
ss = append(ss, sb.String())
}
return ss
}
func clearRect(rect TxRect, bg tb.Attribute) {
for y := rect.Y; y < rect.Y+rect.H; y++ {
for x := rect.X; x < rect.X+rect.W; x++ {
tb.SetCell(x, y, ' ', 0, bg)
}
}
}
func drawBox(rect TxRect, fg, bg tb.Attribute) {
print("┌", rect.X, rect.Y, fg, bg)
print("┐", rect.X+rect.W-1, rect.Y, fg, bg)
hline := strings.Repeat("─", rect.W-2)
print(hline, rect.X+1, rect.Y, fg, bg)
print(hline, rect.X+1, rect.Y+rect.H-1, fg, bg)
vchar := "│"
for j := rect.Y + 1; j < rect.Y+rect.H-1; j++ {
print(vchar, rect.X, j, fg, bg)
}
for j := rect.Y + 1; j < rect.Y+rect.H-1; j++ {
print(vchar, rect.X+rect.W-1, j, fg, bg)
}
print("┘", rect.X+rect.W-1, rect.Y+rect.H-1, fg, bg)
print("└", rect.X, rect.Y+rect.H-1, fg, bg)
}