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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
package tui
import (
"github.com/rivo/uniseg"
"strings"
"unicode"
)
type Box struct {
Width, Height BoxSize
Dir Direction
Overflow bool
Scroll int
Style *Style
Margins [4]int
text []textRun
computedLines [][]textRun
children []*Box
computedPosition int
computedSize [2]int
computedRect rect
}
type BoxSize int
const (
Fill = -(iota + 1)
Children
TextSize
)
func (s BoxSize) isFlexible() bool {
return s == Fill
}
type Direction int
const (
Down = iota
Up
Right
Left
)
func (d Direction) axis() int {
switch d {
case Down, Up:
return 1
default:
return 0
}
}
func (d Direction) reverse() bool {
switch d {
case Down, Right:
return false
default:
return true
}
}
type textRun struct {
text string
style *Style
}
type rect struct {
min [2]int
max [2]int
}
type BoxEvent struct {
}
type TextEvent struct {
}
var layout struct {
front map[string]*Box
back map[string]*Box
stack []*Box
}
func init() {
layout.front = make(map[string]*Box)
layout.back = make(map[string]*Box)
}
func top() *Box {
return layout.stack[len(layout.stack) - 1]
}
func Push(id string, box Box) BoxEvent {
if len(layout.stack) > 0 {
top().children = append(top().children, &box)
}
layout.stack = append(layout.stack, &box)
layout.front[id] = &box
for axis, value := range box.axes() {
if value >= 0 {
box.computedSize[axis] = int(value)
}
}
return BoxEvent {}
}
func Text(text string, style *Style) TextEvent {
top().text = append(top().text, textRun {text, style})
return TextEvent {}
}
func Pop() {
if len(layout.stack) > 1 {
layout.stack = layout.stack[:len(layout.stack) - 1]
}
}
func (b Box) axes() [2]BoxSize {
return [2]BoxSize {b.Width, b.Height}
}
func (b *Box) marginsSize(axis int) int {
return b.Margins[axis * 2] + b.Margins[axis * 2 + 1]
}
func (b *Box) computeFillSizes(axis int) {
for _, c := range b.children {
if c.axes()[axis] == Fill {
c.computedSize[axis] = b.computedSize[axis]
}
c.computeFillSizes(axis)
}
}
func (b *Box) computeChildrenSizes(axis int) {
size := b.marginsSize(axis)
for _, c := range b.children {
c.computeChildrenSizes(axis)
if b.Dir.axis() == axis {
size += c.computedSize[axis]
} else {
size = max(size, c.computedSize[axis])
}
}
if b.axes()[axis] == Children {
b.computedSize[axis] = size
}
}
func (b *Box) solve(axis int) {
if b.Dir.axis() == axis && !b.Overflow {
size := b.marginsSize(axis)
nFlexible := 0
for _, c := range b.children {
size += c.computedSize[axis]
if c.axes()[axis].isFlexible() {
nFlexible++
}
}
excess := max(size - b.computedSize[axis], 0)
if nFlexible > 0 {
contribution := excess / nFlexible
for _, c := range b.children {
if c.axes()[axis].isFlexible() {
if contribution == excess - 1 {
contribution = excess
}
shave := min(c.computedSize[axis], contribution, excess)
c.computedSize[axis] -= shave
excess -= shave
}
}
}
for i := len(b.children) - 1; i >= 0; i-- {
c := b.children[i]
if excess == 0 {
break
}
shave := min(c.computedSize[axis], excess)
c.computedSize[axis] -= shave
excess -= shave
}
} else {
maxSize := b.computedSize[axis] - b.marginsSize(axis)
for _, c := range b.children {
c.computedSize[axis] = min(c.computedSize[axis], maxSize)
}
}
for _, c := range b.children {
c.solve(axis)
}
}
func (b *Box) computeText(axis int) {
if axis == 0 {
maxLine := 0
line := 0
for _, t := range b.text {
g := uniseg.NewGraphemes(t.text)
for g.Next() {
line += g.Width()
if g.LineBreak() == uniseg.LineMustBreak {
maxLine = max(maxLine, line)
line = 0
}
}
}
if b.Width == TextSize {
b.computedSize[axis] = maxLine + b.marginsSize(axis)
}
} else {
var (
limit = b.computedSize[0]
line []textRun
text, word strings.Builder
lineWidth, wordWidth int
run textRun
)
breakLine := func() {
if text.Len() != 0 {
line = append(line, textRun {text.String(), run.style})
}
text.Reset()
b.computedLines = append(b.computedLines, line)
lineWidth = 0
line = nil
}
flushWord := func() {
if lineWidth + wordWidth > limit {
breakLine()
}
g := uniseg.NewGraphemes(word.String())
for g.Next() {
if g.Width() > limit {
continue
}
if lineWidth == 0 && g.Str() == " " {
continue
}
text.WriteString(g.Str())
if g.Width() + lineWidth > limit {
breakLine()
}
lineWidth += g.Width()
}
wordWidth = 0
word.Reset()
}
for _, run := range b.text {
g := uniseg.NewGraphemes(run.text)
for g.Next() {
if g.LineBreak() == uniseg.LineCanBreak {
flushWord()
}
if lineWidth != 0 || !unicode.IsSpace(g.Runes()[0]) {
word.WriteString(g.Str())
wordWidth += g.Width()
}
_, end := g.Positions()
if end == len(run.text) {
flushWord()
break
}
if g.LineBreak() == uniseg.LineMustBreak {
flushWord()
breakLine()
}
}
if text.Len() != 0 {
line = append(line, textRun {text.String(), run.style})
text.Reset()
}
}
if len(line) != 0 || text.Len() != 0 {
breakLine()
}
if b.Height == TextSize {
b.computedSize[axis] = len(b.computedLines) + b.marginsSize(axis)
}
}
for _, c := range b.children {
c.computeText(axis)
}
}
func (b *Box) computePositions(axis int) {
if b.Dir.axis() == axis {
pos := 0 + b.Margins[axis * 2]
if b.Dir.reverse() {
pos = b.computedSize[axis] - b.Margins[axis * 2 + 1]
}
for _, c := range b.children {
if b.Dir.reverse() {
pos -= c.computedSize[axis]
}
c.computedPosition = pos
c.computedRect.min[axis] = b.computedRect.min[axis] + pos
c.computedRect.max[axis] = c.computedRect.min[axis] + c.computedSize[axis]
if !b.Dir.reverse() {
pos += c.computedSize[axis]
}
c.computePositions(axis)
}
} else {
for _, c := range b.children {
c.computedRect.min[axis] = b.computedRect.min[axis] + b.Margins[axis * 2]
c.computedRect.max[axis] = c.computedRect.min[axis] + c.computedSize[axis]
c.computePositions(axis)
}
}
}
func (b *Box) drawComputed(parentStyle Style) {
style := parentStyle
if b.Style != nil {
style = *b.Style
for y := b.computedRect.min[1]; y < b.computedRect.max[1]; y++ {
for x := b.computedRect.min[0]; x < b.computedRect.max[0]; x++ {
WriteAt(x, y, " ", style)
}
}
}
for i, l := range b.computedLines {
x := b.computedRect.min[0] + b.Margins[0]
y := b.computedRect.min[1] + b.Margins[2] + i
if y > b.computedRect.max[1] - b.Margins[3] {
break
}
for _, t := range l {
var s Style
if t.style != nil {
s = *t.style
} else {
s = style
}
x += WriteAt(x, y, t.text, s)
}
}
for _, c := range b.children {
c.drawComputed(style)
}
}
func DrawLayout() {
defer func() {
layout.back = layout.front
layout.front = make(map[string]*Box)
layout.stack = nil
}()
if len(layout.stack) == 0 {
return
}
b := layout.stack[0]
for axis := 0; axis < 2; axis++ {
b.computeFillSizes(axis)
b.computeText(axis)
b.computeChildrenSizes(axis)
b.solve(axis)
b.computePositions(axis)
}
b.drawComputed(DefaultStyle)
}
|