-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathbox.go
161 lines (136 loc) · 2.68 KB
/
box.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package termeter
import (
"gopkg.in/gizak/termui.v1"
"github.com/nsf/termbox-go"
)
type Box interface {
GetWidth() int
GetHeight() int
SetWidth(w int)
SetHeight(h int)
Bufferers() []termui.Bufferer
Render(x, y, w, h int)
}
type LayoutBox interface {
Box
AddBoxes(boxes ...Box)
}
type BoxBase struct {
width int
height int
}
func (bb *BoxBase) GetWidth() int {
return bb.width
}
func (bb *BoxBase) GetHeight() int {
return bb.height
}
func (bb *BoxBase) SetWidth(w int) {
bb.width = w
}
func (bb *BoxBase) SetHeight(h int) {
bb.height = h
}
type LayoutBoxBase struct {
BoxBase
boxes []Box
}
func (lb *LayoutBoxBase) AddBoxes(boxes ...Box) {
lb.boxes = append(lb.boxes, boxes...)
}
func (lb *LayoutBoxBase) Bufferers() []termui.Bufferer {
bufs := []termui.Bufferer{}
for _, box := range lb.boxes {
for _, buf := range box.Bufferers() {
bufs = append(bufs, buf)
}
}
return bufs
}
type WidgetBox struct {
BoxBase
Widget Widget
}
func NewWidgetBox() *WidgetBox {
return &WidgetBox{}
}
func (wb *WidgetBox) SetWidget(w Widget) {
wb.Widget = w
}
func (wb *WidgetBox) Bufferers() []termui.Bufferer {
return []termui.Bufferer{wb.Widget}
}
func (wb *WidgetBox) Render(x, y, w, h int) {
if wb.Widget == nil {
return
}
wb.Widget.SetX(x)
wb.Widget.SetY(y)
wb.Widget.SetWidth(w)
wb.Widget.SetHeight(h)
for _, v := range wb.Widget.Buffer() {
termbox.SetCell(v.X, v.Y, v.Ch,
termbox.Attribute(v.Fg),
termbox.Attribute(v.Bg))
}
}
type VBox struct {
LayoutBoxBase
}
func NewVBox() *VBox {
vb := new(VBox)
vb.boxes = []Box{}
return vb
}
func (vb *VBox) Render(x, y, w, h int) {
fixedHeight := 0
numFixedBox := 0
for _, box := range vb.boxes {
if box.GetHeight() > 0 {
fixedHeight += box.GetHeight()
numFixedBox += 1
}
}
boxDefaultHeight := 0
if len(vb.boxes) > numFixedBox && h > fixedHeight {
boxDefaultHeight = (h - fixedHeight) / (len(vb.boxes) - numFixedBox)
}
for _, box := range vb.boxes {
boxHeight := box.GetHeight()
if boxHeight <= 0 {
boxHeight = boxDefaultHeight
}
box.Render(x, y, w, boxHeight)
y += boxHeight
}
}
type HBox struct {
LayoutBoxBase
}
func NewHBox() *HBox {
hb := new(HBox)
hb.boxes = []Box{}
return hb
}
func (hb *HBox) Render(x, y, w, h int) {
fixedWidth := 0
numFixedBox := 0
for _, box := range hb.boxes {
if box.GetWidth() > 0 {
fixedWidth += box.GetWidth()
numFixedBox += 1
}
}
boxDefaultWidth := 0
if len(hb.boxes) > numFixedBox && w > fixedWidth {
boxDefaultWidth = (w - fixedWidth) / (len(hb.boxes) - numFixedBox)
}
for _, box := range hb.boxes {
boxWidth := box.GetWidth()
if boxWidth <= 0 {
boxWidth = boxDefaultWidth
}
box.Render(x, y, boxWidth, h)
x += boxWidth
}
}