-
-
Notifications
You must be signed in to change notification settings - Fork 112
/
ui.go
77 lines (64 loc) · 1.1 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
package main
import (
"fmt"
"github.com/nsf/termbox-go"
"os"
"unicode/utf8"
)
type Symbol []string
func (s Symbol) width() int {
return utf8.RuneCountInString(s[0])
}
func (s Symbol) height() int {
return len(s)
}
type Text []Symbol
func (t Text) width() int {
w := 0
for _, s := range t {
w += s.width()
}
return w
}
func (t Text) height() int {
return len(t[0])
}
func toText(str string) Text {
symbols := make(Text, 0)
for _, r := range str {
if s, ok := defaultFont[r]; ok {
symbols = append(symbols, s)
}
}
return symbols
}
type Font map[rune]Symbol
func echo(s Symbol, startX, startY int) {
x, y := startX, startY
for _, line := range s {
for _, r := range line {
termbox.SetCell(x, y, r, termbox.ColorDefault, termbox.ColorDefault)
x++
}
x = startX
y++
}
}
func clear() {
err := termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
if err != nil {
panic(err)
}
}
func flush() {
err := termbox.Flush()
if err != nil {
panic(err)
}
}
func stderr(s string, a ...interface{}) {
_, err := fmt.Fprintf(os.Stderr, s, a...)
if err != nil {
panic(err)
}
}