forked from dancrowley303/crenshaw-go
/
util.go
97 lines (82 loc) · 1.58 KB
/
util.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
package util
import "github.com/nsf/termbox-go"
var width int
var height int
var xPos int
var yPos int
var screenMap = make(map[int][]rune)
func incrementLine() {
xPos = 0
if yPos == height-1 {
for i := 0; i < height-1; i++ {
tmp := make([]rune, width)
copy(tmp, screenMap[i+1])
screenMap[i] = tmp
}
screenMap[height-1] = make([]rune, width)
} else {
yPos++
}
}
func init() {
err := termbox.Init()
if err != nil {
panic(err)
}
width, height = termbox.Size()
for i := 0; i < height; i++ {
screenMap[i] = make([]rune, width)
}
}
// Read reads a single character from stdin into a rune
func Read() (out rune) {
for {
if ev := termbox.PollEvent(); ev.Type == termbox.EventKey {
switch ev.Key {
case termbox.KeyCtrlZ:
out = 0x1A
case termbox.KeySpace:
out = 0x20
case termbox.KeyTab:
out = 0x09
case termbox.KeyEnter:
out = 0x0D
default:
out = ev.Ch
}
break
}
}
return
}
// WriteBlankLine Writes a blank line to stdout
func WriteBlankLine() {
Write(string(0x0D))
}
// WriteLine Writes a line of content to stdout
func WriteLine(output string) {
Write(output + string(0x0D))
}
// Write writes a string to stdout
func Write(output string) {
for _, r := range output {
if xPos >= width || r == 0x0D {
incrementLine()
if r == 0x0D {
drawScreen()
continue
}
}
screenMap[yPos][xPos] = r
xPos++
}
drawScreen()
}
func drawScreen() {
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
termbox.SetCell(x, y, screenMap[y][x], termbox.ColorDefault, termbox.ColorDefault)
}
}
termbox.Flush()
}