-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw.go
60 lines (55 loc) · 1.11 KB
/
draw.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
package main
import (
mattnRuneWidth "github.com/mattn/go-runewidth"
"github.com/nsf/termbox-go"
)
func runewidth(r rune) int {
if r == '\t' {
return 8
}
return mattnRuneWidth.RuneWidth(r)
}
func (ui *ui) draw() error {
termbox.Clear(termbox.ColorDefault, termbox.ColorBlack)
_, h := termbox.Size()
for y, l := range ui.lines {
if y == h {
break
}
if len(l) > 0 && l[0] == '#' {
ui.drawString(l, 0, y, termbox.ColorWhite, termbox.ColorBlack)
} else {
ui.drawString(l, 0, y, termbox.ColorDefault, termbox.ColorBlack)
}
}
return termbox.Flush()
}
func (ui *ui) drawString(s []rune, x, y int, fg, bg termbox.Attribute) {
w, _ := termbox.Size()
for i, ch := range s {
if y == ui.cy && i == ui.cx {
termbox.SetCursor(x, ui.cy)
}
if ch == '\t' {
x += 8
continue
}
if ch == '\r' {
if x+2 >= w {
return
}
termbox.SetCell(x, y, '^', bg, fg)
termbox.SetCell(x+1, y, 'R', bg, fg)
x += 2
continue
}
if x+runewidth(ch) >= w {
return
}
termbox.SetCell(x, y, ch, fg, bg)
x += runewidth(ch)
}
if y == ui.cy && len(s) == ui.cx {
termbox.SetCursor(x, ui.cy)
}
}