/
ui_spec.go
72 lines (63 loc) · 1.33 KB
/
ui_spec.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
package ev
import "github.com/gdamore/tcell/v2"
type CellSpec struct {
Rune rune
Style tcell.Style
}
func (c CellSpec) Draw(screen tcell.Screen, x, y, width, height int) int {
screen.SetContent(x, y, c.Rune, nil, c.Style)
return runeDisplayWidth(c.Rune)
}
type RowSpec struct {
Cells []CellSpec
}
func (r *RowSpec) AppendStr(str string, style tcell.Style) {
for _, ru := range str {
r.Cells = append(r.Cells, CellSpec{
Rune: ru,
Style: style,
})
}
}
func (r RowSpec) Draw(screen tcell.Screen, x, y, width, height int) int {
originX := x
for _, cell := range r.Cells {
x += cell.Draw(screen, x, y, width, height)
if x >= width {
return x - originX - 1
}
}
return x - originX
}
type BoxSpec struct {
Rows []RowSpec
}
func (b BoxSpec) Draw(screen tcell.Screen, x, y, width, height int) (dx, dy int) {
originY := y
for _, row := range b.Rows {
n := row.Draw(screen, x, y, width, height)
if n > dx {
dx = n
}
if y >= height {
return dx, y - originY
}
y++
}
return dx, y - originY
}
func (b BoxSpec) DrawLeftBottom(screen tcell.Screen, x, y, width, height int) (dx, dy int) {
originY := y
for i := len(b.Rows) - 1; i >= 0; i-- {
row := b.Rows[i]
n := row.Draw(screen, x, y, width, height)
if n > dx {
dx = n
}
y--
if y < 0 {
return dx, originY - y
}
}
return dx, originY - y
}