-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.go
85 lines (67 loc) · 1.44 KB
/
ui.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
package main
import (
"log"
"strings"
"github.com/gdamore/tcell/v2"
"github.com/mattn/go-runewidth"
)
type screen struct {
tcell.Screen
x, y int // cursor
}
func (sc *screen) draw(text string, style tcell.Style) {
w, h := sc.Size()
for _, r := range []rune(text) {
rw := runewidth.RuneWidth(r)
// skip oob writes
if sc.x+rw >= w || sc.y >= h {
continue
}
sc.put(r, style)
}
}
func (sc *screen) drawMessage(msg string, x int, y int) {
// upper border
sc.x = x
sc.y = y
sc.draw("+"+strings.Repeat("-", len(msg)+2)+"+", styleGray)
// center with message and front- and back-border
sc.x = x
sc.y = y + 1
sc.draw("| ", styleGray)
sc.draw(msg, styleDefault)
sc.draw(" |", styleGray)
// lower border
sc.x = x
sc.y = y + 2
sc.draw("+"+strings.Repeat("-", len(msg)+2)+"+", styleGray)
}
func (sc *screen) put(r rune, style tcell.Style) {
if !active {
// stop drawing while shutdown to
// avoid a data race with screen.Fini()
return
}
w := runewidth.RuneWidth(r)
// skip all non-printable
if w < 1 {
return
}
sc.SetContent(sc.x, sc.y, r, nil, style)
sc.x = sc.x + w
}
func newScreen() (sc *screen) {
tsc, err := tcell.NewScreen()
if err != nil {
log.Panicln(err.Error())
}
err = tsc.Init()
if err != nil {
log.Panicln(err.Error())
}
tsc.DisablePaste()
tsc.DisableMouse()
tsc.SetStyle(tcell.StyleDefault.Background(tcell.ColorReset).Foreground(tcell.ColorReset))
tsc.Clear()
return &screen{Screen: tsc}
}