Repository navigation
Expand file tree
/
Copy pathdebug.go
More file actions
320 lines (301 loc) · 8.98 KB
/
Copy pathdebug.go
File metadata and controls
320 lines (301 loc) · 8.98 KB
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
// Copyright (c) Liam Stanley <liam@liam.sh>. All rights reserved. Use of
// this source code is governed by the MIT license that can be found in
// the LICENSE file.
package bloom
import (
"fmt"
"image"
"slices"
"strconv"
"strings"
"bloomfx.dev/bloom/fx"
"github.com/charmbracelet/x/ansi"
)
// Debug lays out c in a w×h frame, as [Solve] does, and returns its tree of
// nodes as text, one node per line: what it is, how it is sized, its ID,
// where it was laid out as x,y and width x height, and anything else that
// affects it. A node its container collapsed, or a layer whose anchor is not
// on screen, is marked hidden; one partly on screen is marked with the part
// that is, and one not on screen at all is marked offscreen. Components are
// shown by their type, with the tree they built, and a [View] by the type
// of what it draws. A node with the same ID as one
// before it is marked duplicate: IDs should be unique in a frame, and the
// nodes in two with the same ID, which are known by where they are in them,
// are not told apart either.
//
// Debug is meant for finding out why a layout looks the way it does, such as
// by writing it to a log from View. Its format may change between versions.
//
// Column 0,0 30x5
// ├─ Row 0,0 30x3
// │ ├─ Fixed(10) id="sidebar" 0,0 10x3 collapsible=1
// │ │ └─ Box 0,0 10x3
// │ │ └─ Text "Inbox" 1,1 8x1
// │ └─ Fill id="reader" 10,0 20x3 min=12
// │ └─ main.reader 10,0 20x3
// └─ Text "ready" 0,3 30x1 truncate
func Debug(w, h int, c Child) string {
r, _ := layouts.Get().(*renderer)
defer layouts.Put(r)
root := r.layout(w, h, c)
fw, fh := r.size(root, w, h)
r.cache.EndFrame(fw * fh)
if root < 0 {
return ""
}
var sb strings.Builder
r.debugTree(&sb, root, r.area, "", "", false, make(map[ID]bool))
return sb.String()
}
// debugTree writes box i and the boxes in it, clipped to clip. lead is drawn
// before the box, and indent before the boxes in it. hidden is set inside a
// hidden box, as boxes in one are not laid out. ids are the IDs written so
// far.
func (e *engine) debugTree(sb *strings.Builder, i int, clip image.Rectangle, lead, indent string, hidden bool, ids map[ID]bool) {
b := &e.boxes[i]
kind, attrs := debugNode(b.child)
sb.WriteString(lead + kind)
if id := b.meta.id; !id.IsZero() {
sb.WriteString(" id=" + debugID(id))
if ids[id] {
sb.WriteString(" duplicate")
}
ids[id] = true
}
hidden = hidden || b.hidden
if b.float {
// A layer that floats is clipped to the screen, not to what it is in.
clip = e.area
}
if hidden {
sb.WriteString(" hidden")
} else {
sb.WriteString(" " + debugRect(b.rect))
vis := b.rect.Intersect(clip)
switch {
case b.rect.Empty():
case vis.Empty():
sb.WriteString(" offscreen")
case vis != b.rect:
sb.WriteString(" visible=" + debugRect(vis))
}
clip = vis
}
if b.cramped {
attrs = append(attrs, "cramped")
}
if b.modal {
attrs = append(attrs, "modal")
}
if b.float {
attrs = append(attrs, "float")
}
if b.meta.focusable {
attrs = append(attrs, "focusable")
}
if b.meta.scope {
attrs = append(attrs, "scope")
}
if b.meta.cursor != nil {
attrs = append(attrs, "cursor")
}
for _, a := range attrs {
sb.WriteString(" " + a)
}
sb.WriteByte('\n')
kids := e.kidsOf(i)
if b.expanded >= 0 {
kids = append(slices.Clip(kids), b.expanded)
}
for j, k := range kids {
if j == len(kids)-1 {
e.debugTree(sb, k, clip, indent+"└─ ", indent+" ", hidden, ids)
} else {
e.debugTree(sb, k, clip, indent+"├─ ", indent+"│ ", hidden, ids)
}
}
}
// debugNode returns what c is, and the settings of it that are not their
// defaults.
func debugNode(c Child) (kind string, attrs []string) {
switch n := c.(type) {
case TextNode:
if n.styled {
attrs = append(attrs, "styled")
}
if n.truncate {
attrs = append(attrs, "truncate")
}
if n.nowrap {
attrs = append(attrs, "nowrap")
}
return "Text " + debugText(n.s), attrs
case SlotNode:
return debugSlot(n)
case FlexNode:
kind = "Row"
if n.vertical {
kind = "Column"
}
if n.gap > 0 {
attrs = append(attrs, fmt.Sprintf("gap=%d", n.gap))
}
if n.justify != 0 {
attrs = append(attrs, "justify="+debugName(justifyNames, n.justify))
}
if n.align != Stretch {
attrs = append(attrs, "align="+debugName(alignNames, n.align))
}
return kind, attrs
case GridNode:
return fmt.Sprintf("Grid(%d)", n.cols), debugGap(n.gapX, n.gapY)
case WrapNode:
return "Wrap", debugGap(n.gapX, n.gapY)
case BoxNode:
return "Box", nil
case TransitionNode:
return "Transition", []string{fmt.Sprintf("t=%.2f", n.t), debugEffects(n.effects)}
case CenterNode:
return "Centered", nil
case ScrollNode:
attrs = []string{fmt.Sprintf("offset=%d", n.y)}
if n.horizontal {
attrs = append(attrs, fmt.Sprintf("x=%d", n.x))
}
return "Scroll", attrs
case OverlayNode:
return "Overlay", nil
case LayerNode:
return "Layer", debugLayer(n)
case DrawableNode:
return fmt.Sprintf("Drawable(%T)", n.d), nil
case ScrollbarNode:
return "Scrollbar", nil
case TooltipSinkNode:
return "TooltipSink", nil
case ViewNode:
return fmt.Sprintf("View(%T)", n.v), nil
case ViewportNode:
lines := 0
if n.buf != nil {
lines = n.buf.Len()
}
attrs = []string{fmt.Sprintf("lines=%d", lines)}
switch {
case n.pos.AtEnd():
attrs = append(attrs, "at=end")
case n.pos != ViewportPos{}:
attrs = append(attrs, fmt.Sprintf("at=%d:%d:%d", n.pos.Line, n.pos.Row, n.pos.X))
}
if n.wrap != WrapNone {
attrs = append(attrs, debugName(wrapNames, n.wrap))
}
if n.follow {
attrs = append(attrs, "follow")
}
if n.caret != nil {
attrs = append(attrs, fmt.Sprintf("caret=%d:%d", n.caret.Head.Line, n.caret.Head.Col))
if n.caret.EndOfRow {
attrs = append(attrs, "end-of-row")
}
}
return "Viewport", attrs
default:
return fmt.Sprintf("%T", c), nil
}
}
func debugSlot(n SlotNode) (kind string, attrs []string) {
switch {
case n.sizing == sizeFixed:
kind = fmt.Sprintf("Fixed(%d)", n.n)
case n.sizing == sizeFill && n.n == 1:
kind = "Fill"
case n.sizing == sizeFill:
kind = fmt.Sprintf("Grow(%d)", n.n)
case n.sizing == sizePercent:
kind = fmt.Sprintf("Percent(%d)", n.n)
default:
kind = "Auto"
}
if n.min > 0 {
attrs = append(attrs, fmt.Sprintf("min=%d", n.min))
}
if n.max > 0 {
attrs = append(attrs, fmt.Sprintf("max=%d", n.max))
}
if n.collapse > 0 {
attrs = append(attrs, fmt.Sprintf("collapsible=%d", n.collapse))
}
if n.aligned {
attrs = append(attrs, "align="+debugName(alignNames, n.align))
}
return kind, debugMotion(attrs, n.animation, n.moves)
}
func debugLayer(n LayerNode) (attrs []string) {
switch {
case n.side != 0:
attrs = append(attrs, "anchorTo="+debugID(n.anchor)+"/"+debugName(directionNames, n.side))
case n.x != Center || n.y != Center:
attrs = append(attrs, "anchor="+debugName(alignNames, n.x)+","+debugName(alignNames, n.y))
}
if n.offsetX != 0 || n.offsetY != 0 {
attrs = append(attrs, fmt.Sprintf("offset=%d,%d", n.offsetX, n.offsetY))
}
if n.margin > 0 {
attrs = append(attrs, fmt.Sprintf("margin=%d", n.margin))
}
if n.w > 0 || n.h > 0 {
attrs = append(attrs, fmt.Sprintf("size=%dx%d", n.w, n.h))
}
if n.t < 1 || len(n.effects) > 0 {
attrs = append(attrs, fmt.Sprintf("t=%.2f", n.t), debugEffects(n.effects))
}
return debugMotion(attrs, n.animation, n.moves)
}
// debugMotion appends how a slot or layer animates when layout changes.
func debugMotion(attrs []string, animation *layoutAnimation, moves []fx.Effect) []string {
if animation != nil {
attrs = append(attrs, "animate")
}
if len(moves) > 0 {
attrs = append(attrs, "moveWith="+strings.TrimPrefix(debugEffects(moves), "effects="))
}
return attrs
}
var (
alignNames = []string{"stretch", "start", "center", "end"}
justifyNames = []string{"start", "center", "end", "space-between", "space-around", "space-evenly"}
wrapNames = []string{"nowrap", "wrap=words", "wrap=chars"}
// The zero value of a layer's side is the default, Bottom.
directionNames = []string{"bottom", "bottom", "top", "left", "right", "top-left", "top-right", "bottom-left", "bottom-right"}
)
// debugName returns the name of v in names, or its number if it has none.
func debugName[T ~uint8](names []string, v T) string {
if int(v) < len(names) {
return names[v]
}
return strconv.Itoa(int(v))
}
func debugGap(x, y int) []string {
if x == 0 && y == 0 {
return nil
}
return []string{fmt.Sprintf("gap=%d,%d", x, y)}
}
func debugEffects(effects []fx.Effect) string {
names := make([]string, len(effects))
for i, e := range effects {
names[i] = fmt.Sprintf("%T", e)
}
return "effects=" + strings.Join(names, ",")
}
func debugRect(r image.Rectangle) string {
return fmt.Sprintf("%d,%d %dx%d", r.Min.X, r.Min.Y, r.Dx(), r.Dy())
}
// debugID returns id quoted, kept to one line.
func debugID(id ID) string { return strconv.Quote(string(id)) }
// debugText returns the start of s as one quoted line, without its styles.
func debugText(s string) string {
s = lineBreaks.Replace(ansi.Strip(s))
return strconv.Quote(ansi.Truncate(s, 24, "…"))
}