-
Notifications
You must be signed in to change notification settings - Fork 3
/
tcell.go
47 lines (43 loc) · 918 Bytes
/
tcell.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
package main
import (
"github.com/gdamore/tcell/v2"
"github.com/mattn/go-runewidth"
)
func initTcell() (s tcell.Screen, err error) {
if s, err = tcell.NewScreen(); err != nil {
return
}
if err = s.Init(); err != nil {
return
}
defStyle := tcell.StyleDefault
defStyle = defStyle.Background(tcell.ColorBlack)
defStyle = defStyle.Foreground(tcell.ColorWhite)
s.SetStyle(defStyle)
s.Clear()
return
}
func emitStr(s tcell.Screen, x, y int, style tcell.Style, str string) {
for _, c := range str {
var comb []rune
w := runewidth.RuneWidth(c)
if w == 0 {
comb = []rune{c}
c = ' '
w = 1
}
s.SetContent(x, y, c, comb, style)
x += w
}
}
func handleTcellEvents(screen tcell.Screen, events chan event) {
for {
switch ev := screen.PollEvent().(type) {
case *tcell.EventResize:
screen.Sync()
events <- redrawEvent
case *tcell.EventKey:
handleKeyEvents(ev, events)
}
}
}