forked from jojomi/gonsole
-
Notifications
You must be signed in to change notification settings - Fork 4
/
draw.go
135 lines (117 loc) · 3.54 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Higher level helper functions for termbox
// TODO support theming
package gonsole
import (
"github.com/mitchellh/go-wordwrap"
"github.com/nsf/termbox-go"
)
func FillRect(box Box, fg, bg termbox.Attribute) {
for x := box.Left; x <= box.Right(); x++ {
for y := box.Top; y <= box.Bottom(); y++ {
termbox.SetCell(x, y, ' ', fg, bg)
}
}
}
func DrawBorder(box Box, lineType LineType, fg, bg termbox.Attribute) {
right := box.Right()
bottom := box.Bottom()
runes := getLineRunes(lineType)
DrawLineHorizontal(box.Left, box.Top, box.Width, runes[0], fg, bg)
DrawLineHorizontal(box.Left, box.Bottom(), box.Width, runes[0], fg, bg)
DrawLineVertical(box.Left, box.Top, box.Height, runes[1], fg, bg)
DrawLineVertical(box.Right(), box.Top, box.Height, runes[1], fg, bg)
termbox.SetCell(box.Left, box.Top, runes[2], fg, bg)
termbox.SetCell(right, box.Top, runes[3], fg, bg)
termbox.SetCell(box.Left, bottom, runes[4], fg, bg)
termbox.SetCell(right, bottom, runes[5], fg, bg)
}
func DrawShadow(box Box, shadow termbox.Attribute) {
bottom := box.Bottom()
right := box.Right()
DrawLineHorizontal(box.Left+1, bottom, box.Width, ' ', shadow, shadow)
DrawLineVertical(right, box.Top+1, box.Height-1, ' ', shadow, shadow)
}
func DrawLineHorizontal(left, top, width int, ch rune, fg, bg termbox.Attribute) {
for x := left; x < left+width-1; x++ {
termbox.SetCell(x, top, ch, fg, bg)
}
}
func DrawLineVertical(left, top, height int, ch rune, fg, bg termbox.Attribute) {
for y := top; y < top+height-1; y++ {
termbox.SetCell(left, y, ch, fg, bg)
}
}
func ScrollPos(index, count, height int) int {
pos := int(float32(index) / float32(count) * float32(height-2))
return pos
}
func DrawScrollBar(left, top, height, pos int, fg, bg termbox.Attribute) {
runes := []rune("░■▲▼")
termbox.SetCell(left, top, runes[2], fg, bg)
termbox.SetCell(left, top+height-1, runes[3], fg, bg)
if height > 2 {
for y := top + 1; y < top+height-1; y++ {
termbox.SetCell(left, y, runes[0], fg, bg)
}
}
if pos != -1 {
termbox.SetCell(left, top+pos+1, runes[1], fg, bg)
}
}
func DrawCursor(x, y int) {
termbox.SetCursor(x, y)
}
func HideCursor() {
termbox.HideCursor()
}
// TODO support line breaking for multiline strings
// TODO support alignment
func DrawTextBox(text string, box Box, fg, bg termbox.Attribute) {
wrapText := wordwrap.WrapString(text, uint(box.Width))
x := box.Left
y := box.Top
for _, char := range wrapText {
if char == '\n' {
x = box.Left
y++
} else {
termbox.SetCell(x, y, char, fg, bg)
x++
}
}
}
func DrawTextSimple(text string, fill bool, box Box, fg, bg termbox.Attribute) {
index := 0
for _, char := range text {
termbox.SetCell(box.Left+index, box.Top, char, fg, bg)
index++
}
if fill {
for x := index; x < box.Width; x++ {
termbox.SetCell(box.Left+x, box.Top, ' ', bg, bg)
}
}
}
func getLineRunes(lineType LineType) []rune {
// https://en.wikipedia.org/wiki/Box-drawing_character
var runes []rune
switch lineType {
case LineNone:
panic("LineNone is not valid")
case LineTransparent:
runes = []rune{' ', ' ', ' ', ' ', ' ', ' '}
case LineSingle:
runes = []rune{'─', '│', '┌', '┐', '└', '┘'}
case LineSingleCorners:
runes = []rune{' ', ' ', '┌', '┐', '└', '┘'}
case LineDouble:
runes = []rune{'═', '║', '╔', '╗', '╚', '╝'}
case LineDoubleCorners:
runes = []rune{' ', ' ', '╔', '╗', '╚', '╝'}
case LineDashed:
runes = []rune{'╌', '╎', '┌', '┐', '└', '┘'}
case LineDotted:
runes = []rune{'┄', '┆', '┌', '┐', '└', '┘'}
}
return runes
}