-
Notifications
You must be signed in to change notification settings - Fork 6
/
ttk.go
577 lines (503 loc) · 12.6 KB
/
ttk.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
// Copyright (c) 2016 Company 0, LLC.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package ttk
import (
"errors"
"fmt"
"os"
"strconv"
"strings"
"sync"
"unicode/utf8"
"github.com/nsf/termbox-go"
)
const (
// see http://en.wikipedia.org/wiki/ANSI_escape_code#Colors
ANSIFg = 30
ANSIBg = 40
AttrNA = -1
AttrReset = 0
AttrBold = 1
AttrUnderline = 3
AttrReverse = 7
ColorBlack = 0
ColorRed = 1
ColorGreen = 2
ColorYellow = 3
ColorBlue = 4
ColorMagenta = 5
ColorCyan = 6
ColorWhite = 7
)
var (
ErrNotEscSequence = errors.New("not an escape sequence")
ErrInvalidColor = errors.New("invalid parameters for sequence")
ErrInvalidAttribute = errors.New("invalid attribute")
ErrInvalidForeground = errors.New("invalid foreground")
ErrInvalidBackground = errors.New("invalid background")
)
// Color creates an ANSI compatible escape sequence that encodes colors and
// attributes.
func Color(at, fg, bg int) (string, error) {
var a, f, b string
// can't be all NA
if at == AttrNA && fg == AttrNA && bg == AttrNA {
return "", ErrInvalidColor
}
switch at {
case AttrNA:
break
case AttrBold, AttrUnderline, AttrReverse, AttrReset:
a = fmt.Sprintf("%v;", at)
default:
return "", ErrInvalidAttribute
}
switch {
case fg == AttrNA:
break
case fg >= ColorBlack && fg <= ColorWhite:
f = fmt.Sprintf("%v;", fg+ANSIFg)
default:
return "", ErrInvalidForeground
}
switch {
case bg == AttrNA:
break
case bg >= ColorBlack && bg <= ColorWhite:
b = fmt.Sprintf("%v;", bg+ANSIBg)
default:
return "", ErrInvalidBackground
}
es := fmt.Sprintf("\x1b[%v%v%v", a, f, b)
// replace last ; with m
es = es[:len(es)-1] + "m"
return es, nil
}
// DecodeColor decodes an ANSI color escape sequence and ignores trailing
// characters. It returns an Attributes type that can be used directly in
// termbox (note that the termbox colors are off by one). The skip contains
// the location of the next character that was not consumed by the escape
// sequence.
func DecodeColor(esc string) (*Attributes, int, error) {
var a Attributes
if len(esc) < 2 || !strings.HasPrefix(esc, "\x1b[") {
return nil, 0, ErrNotEscSequence
}
// find trailing m
i := strings.Index(esc[2:], "m")
if i == -1 {
return nil, 0, ErrNotEscSequence
}
foundM := false
parameters := strings.Split(esc[2:i+2+1], ";")
for _, v := range parameters {
if strings.HasSuffix(v, "m") {
v = v[:len(v)-1]
foundM = true
}
n, err := strconv.Atoi(v)
if err != nil {
return nil, 0, err
}
switch {
case n == AttrReset:
// return defaults
a.Fg = fg
a.Bg = bg
case n == AttrBold:
a.Fg |= termbox.AttrBold
case n == AttrUnderline:
a.Fg |= termbox.AttrUnderline
case n == AttrReverse:
a.Fg |= termbox.AttrReverse
case n >= ColorBlack+ANSIFg && n <= ColorWhite+ANSIFg:
// note that termbox colors are off by one
a.Fg |= termbox.Attribute(n - ANSIFg + 1)
case n >= ColorBlack+ANSIBg && n <= ColorWhite+ANSIBg:
// note that termbox colors are off by one
a.Bg |= termbox.Attribute(n - ANSIBg + 1)
default:
return nil, 0, ErrNotEscSequence
}
}
if !foundM {
return nil, 0, ErrNotEscSequence
}
skip := strings.Index(esc, "m")
if skip == -1 {
// can't happen
return nil, 0, ErrNotEscSequence
}
skip++ // character past m
return &a, skip, nil
}
// EscapedLen returns total length of all escape sequences in a given string.
func EscapedLen(s string) int {
if len(s) == 0 {
return 0
}
var rw, total int
for i := 0; i < len(s); i += rw {
v, width := utf8.DecodeRuneInString(s[i:])
if v == '\x1b' {
_, skip, err := DecodeColor(s[i:])
if err == nil {
rw = skip
total += skip
continue
}
}
rw = width
}
return total
}
// Unescape returns the unescaped string.
func Unescape(s string) string {
if len(s) == 0 {
return ""
}
var ret string
var rw int
for i := 0; i < len(s); i += rw {
v, width := utf8.DecodeRuneInString(s[i:])
if v == '\x1b' {
_, skip, err := DecodeColor(s[i:])
if err == nil {
rw = skip
continue
}
}
ret += string(v)
rw = width
}
return ret
}
// Cell contains a single screen cell.
// This structure exists in order to mark cells that require rendering.
// This is required in order to only render deltas, this matters over slow
// links.
type Cell struct {
termbox.Cell // anon since we are only adding the dirty bit
dirty bool // like your mom
}
// Attributes represents attributes which are defined as text color, bold,
// blink etc.
type Attributes struct {
Fg termbox.Attribute // foreground
Bg termbox.Attribute // background
}
var (
// ErrAlreadyInitialized is used on reentrant calls of Init.
ErrAlreadyInitialized = errors.New("terminal already initialized")
// terminal
maxX int // max x
maxY int // max y
termRaw bool // true in raw managed window mode
keyHandler bool // true if key handler has been launched
rawMtx sync.Mutex // required for switching terminal modes
// all render and termbox access must go through this channel
work chan func() // render work queue
// windows
lastWindowID int // last used window id
focus *Window // currently focused window
prevFocus *Window // previously focused window
windows map[int]*Window // all managed windows
keyC chan Key // key handler channel
// lookerupper between Windower an *Window
windower2window map[Windower]*Window
// defaults
bg termbox.Attribute // background color
fg termbox.Attribute // foreground color
)
// init sets up all global variables and prepares ttk for use.
func init() {
work = make(chan func(), 32)
keyC = make(chan Key, 1024)
windows = make(map[int]*Window)
windower2window = make(map[Windower]*Window)
// setup render queue
// we do this song and dance in order to be able to deal with slow
// connections where rendering could take a long time
execute := make(chan bool, 1)
fa := make([]func(), 0, 20)
mtx := sync.Mutex{}
var wg sync.WaitGroup
wg.Add(2)
go func() {
wg.Done()
for range execute {
for {
// get work off queue
mtx.Lock()
if len(fa) == 0 {
mtx.Unlock()
break
}
f := fa[0]
fa[0] = nil // just in case to prevent leak
fa = fa[1:]
mtx.Unlock()
// actually do work
f()
}
}
}()
go func() {
wg.Done()
for f := range work {
// queue work
mtx.Lock()
fa = append(fa, f)
mtx.Unlock()
// tell executer there is work
select {
case execute <- true:
default:
}
}
}()
wg.Wait()
}
// initKeyHandler starts the internal key handler.
// Must be called with mutex held and as a go routine.
func initKeyHandler() {
for {
switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventKey:
e := ev
Queue(func() {
var (
widget Widgeter
window Windower
)
if focus != nil {
var used bool
used, window, widget = focus.keyHandler(e)
if used {
flush()
return
}
}
// forward to global application handler
keyC <- Key{
Mod: e.Mod,
Key: e.Key,
Ch: e.Ch,
Window: window,
Widget: widget,
}
// XXX this is a terrible workaround!!
// the app is racing this channel
// we need to somehow block here before doing
// anything else
//time.Sleep(25 * time.Millisecond)
})
case termbox.EventResize:
Queue(func() {
resizeAndRender(focus)
})
case termbox.EventMouse:
case termbox.EventError:
return
}
}
}
// Init switches the terminal to raw mode and commences managed window mode.
// This function shall be called prior to any ttk calls.
func Init() error {
rawMtx.Lock()
defer rawMtx.Unlock()
if termRaw {
return ErrAlreadyInitialized
}
// switch mode
err := termbox.Init()
if err != nil {
return err
}
bg = termbox.ColorDefault
fg = termbox.ColorDefault
termbox.HideCursor()
termbox.SetInputMode(termbox.InputAlt) // this may need to become variable
_ = termbox.Clear(bg, bg)
maxX, maxY = termbox.Size()
_ = termbox.Flush()
// see if we need to launch the key handler
if !keyHandler {
go initKeyHandler()
keyHandler = true
}
termRaw = true // we are now in raw mode
return nil
}
// Deinit switches the terminal back to cooked mode and it terminates managed
// window mode. Init must be called again if a switch is required again.
// Deinit shall be called on application exit; failing to do so may leave the
// terminal corrupted. If that does happen typing "reset" on the shell usually
// fixes this problem.
func Deinit() {
wait := make(chan interface{})
Queue(func() {
termbox.Close()
focus = nil
prevFocus = nil
windows = make(map[int]*Window) // toss all windows
rawMtx.Lock()
termRaw = false
rawMtx.Unlock()
wait <- true
})
<-wait
}
// Queue sends work to the queue and returns almost immediately.
func Queue(f func()) {
work <- f
}
// KeyChannel returns the the Key channel that can be used in the application
// to handle keystrokes.
func KeyChannel() chan Key {
// no need to lock since it never changes
return keyC
}
// NewWindow creates a new window type.
func NewWindow(manager Windower) *Window {
wc := make(chan *Window)
Queue(func() {
w := &Window{
id: lastWindowID,
mgr: manager,
x: maxX,
y: maxY,
focus: -1, // no widget focused
backingStore: make([]Cell, maxX*maxY),
widgets: make([]Widgeter, 0, 16),
}
lastWindowID++
windows[w.id] = w
windower2window[manager] = w
manager.Init(w)
wc <- w
})
return <-wc
}
// ForwardKey must be called from the application to route key strokes to
// windows. The life cycle of keystrokes is as follows: widgets -> global
// application context -> window. Care must be taken in the application to not
// rely on keystrokes that widgets may use.
func ForwardKey(k Key) {
if k.Window == nil {
return
}
k.Window.KeyHandler(windower2window[k.Window], k)
}
// defaultAttributes returns the default attributes.
// defaultAttributes shall be called from queue context.
func defaultAttributes() Attributes {
return Attributes{
Fg: fg,
Bg: bg,
}
}
// DefaultAttributes returns the default attributes.
// This is a blocking call.
func DefaultAttributes() Attributes {
c := make(chan Attributes)
Queue(func() {
c <- defaultAttributes()
})
return <-c
}
// flush copies focused window backing store onto the physical screen.
// flush shall be called from queue context.
func flush() {
if focus == nil {
return
}
for y := 0; y < focus.y; y++ {
for x := 0; x < focus.x; x++ {
c := focus.getCell(x, y)
if c == nil {
// out of range, should not happen
continue
}
if !c.dirty {
// skip unchanged cells
continue
}
c.dirty = false
// this shall be the only spot where
// termbox.SetCell is called!
termbox.SetCell(x, y, c.Ch, c.Fg, c.Bg)
}
}
_ = termbox.Flush()
}
// Flush copies focused window backing store onto the physical screen.
func Flush() {
Queue(func() {
flush()
})
}
// setCursor sets the cursor at the specified location. This will not show
// immediately. setCursor shall be called from queue context.
func setCursor(x, y int) {
termbox.SetCursor(x, y)
}
// focus on provided window. This will implicitly focus on a window widget
// that can have focus. Render and flush it onto the terminal.
// focus shall be called from queue context.
func focusWindow(w *Window) {
if w == nil {
return
}
_, found := windows[w.id]
if !found {
return
}
if focus == w {
return
}
prevFocus = focus
focus = w
resizeAndRender(w)
}
// resizeAndRender resizes a window and renders it.
func resizeAndRender(w *Window) {
// render window
if w != nil {
_ = termbox.Clear(bg, bg)
maxX, maxY = termbox.Size()
w.resize(maxX, maxY)
w.render()
// display all the things
flush()
}
}
// Focus on provided window. This will implicitly focus on a window widget
// that can have focus. Render and flush it onto the terminal.
func Focus(w *Window) {
Queue(func() {
focusWindow(w)
})
}
// FocusPrevious focus on previous focused window. This will implicitly focus
// on a window widget that can have focus. Render and flush it onto the
// terminal.
func FocusPrevious() {
Queue(func() {
focusWindow(prevFocus)
})
}
// Panic application but deinit first so that the terminal will not be corrupt.
func Panic(format string, args ...interface{}) {
termbox.Close()
msg := fmt.Sprintf(format, args...)
panic(msg)
}
// Exit application but deinit first so that the terminal will not be corrupt.
func Exit(format string, args ...interface{}) {
termbox.Close()
fmt.Fprintf(os.Stderr, format+"\n", args...)
os.Exit(1)
}