gitstack

grindlemire/go-tui code browser

12.4 KB Go 462 lines 2026-06-17 · 7d3f10d raw
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package tui

// BorderStyle represents different styles of box borders.
type BorderStyle int

const (
	// BorderNone indicates no border should be drawn.
	BorderNone BorderStyle = iota
	// BorderSingle uses single-line box-drawing characters (─, │, ┌, etc.)
	BorderSingle
	// BorderDouble uses double-line box-drawing characters (═, ║, ╔, etc.)
	BorderDouble
	// BorderRounded uses rounded corner characters (─, │, ╭, ╮, ╰, ╯)
	BorderRounded
	// BorderThick uses thick/heavy box-drawing characters (━, ┃, ┏, etc.)
	BorderThick
)

// BorderChars holds the characters used to draw a box border.
type BorderChars struct {
	TopLeft     rune
	Top         rune
	TopRight    rune
	Left        rune
	Right       rune
	BottomLeft  rune
	Bottom      rune
	BottomRight rune
}

// Chars returns the box-drawing characters for this border style.
func (b BorderStyle) Chars() BorderChars {
	switch b {
	case BorderSingle:
		return BorderChars{
			TopLeft:     '┌',
			Top:         '─',
			TopRight:    '┐',
			Left:        '│',
			Right:       '│',
			BottomLeft:  '└',
			Bottom:      '─',
			BottomRight: '┘',
		}
	case BorderDouble:
		return BorderChars{
			TopLeft:     '╔',
			Top:         '═',
			TopRight:    '╗',
			Left:        '║',
			Right:       '║',
			BottomLeft:  '╚',
			Bottom:      '═',
			BottomRight: '╝',
		}
	case BorderRounded:
		return BorderChars{
			TopLeft:     '╭',
			Top:         '─',
			TopRight:    '╮',
			Left:        '│',
			Right:       '│',
			BottomLeft:  '╰',
			Bottom:      '─',
			BottomRight: '╯',
		}
	case BorderThick:
		return BorderChars{
			TopLeft:     '┏',
			Top:         '━',
			TopRight:    '┓',
			Left:        '┃',
			Right:       '┃',
			BottomLeft:  '┗',
			Bottom:      '━',
			BottomRight: '┛',
		}
	default:
		// BorderNone or unknown - return spaces
		return BorderChars{
			TopLeft:     ' ',
			Top:         ' ',
			TopRight:    ' ',
			Left:        ' ',
			Right:       ' ',
			BottomLeft:  ' ',
			Bottom:      ' ',
			BottomRight: ' ',
		}
	}
}

// DrawBox draws a box border on the buffer at the specified rectangle.
// The box is drawn using the specified border style and style (colors/attributes).
// If the rectangle is smaller than 2x2, the function does nothing.
func DrawBox(buf *Buffer, rect Rect, border BorderStyle, style Style) {
	if rect.Width < 2 || rect.Height < 2 {
		return
	}
	if border == BorderNone {
		return
	}

	chars := border.Chars()

	// Clip rect to buffer bounds
	bufRect := buf.Rect()
	rect = rect.Intersect(bufRect)
	if rect.IsEmpty() || rect.Width < 2 || rect.Height < 2 {
		return
	}

	left := rect.X
	right := rect.Right() - 1
	top := rect.Y
	bottom := rect.Bottom() - 1

	// Draw corners
	buf.SetRune(left, top, chars.TopLeft, style)
	buf.SetRune(right, top, chars.TopRight, style)
	buf.SetRune(left, bottom, chars.BottomLeft, style)
	buf.SetRune(right, bottom, chars.BottomRight, style)

	// Draw top and bottom edges
	for x := left + 1; x < right; x++ {
		buf.SetRune(x, top, chars.Top, style)
		buf.SetRune(x, bottom, chars.Bottom, style)
	}

	// Draw left and right edges
	for y := top + 1; y < bottom; y++ {
		buf.SetRune(left, y, chars.Left, style)
		buf.SetRune(right, y, chars.Right, style)
	}
}

// DrawBoxGradient draws a box border with a gradient applied around the perimeter.
// The gradient is applied based on its direction:
// - Horizontal: left to right along top/bottom edges, top to bottom along left/right edges
// - Vertical: top to bottom along all edges
// - DiagonalDown: top-left to bottom-right
// - DiagonalUp: bottom-left to top-right
func DrawBoxGradient(buf *Buffer, rect Rect, border BorderStyle, g Gradient, baseStyle Style) {
	if rect.Width < 2 || rect.Height < 2 {
		return
	}
	if border == BorderNone {
		return
	}

	chars := border.Chars()

	// Clip rect to buffer bounds
	bufRect := buf.Rect()
	rect = rect.Intersect(bufRect)
	if rect.IsEmpty() || rect.Width < 2 || rect.Height < 2 {
		return
	}

	left := rect.X
	right := rect.Right() - 1
	top := rect.Y
	bottom := rect.Bottom() - 1
	width := float64(rect.Width)
	height := float64(rect.Height)
	perimeter := 2*width + 2*height - 4 // Subtract 4 for corners counted twice

	// Helper to calculate t along the perimeter, mirrored so the gradient
	// goes Start→End over the first half and End→Start over the second half.
	// This avoids a jarring color discontinuity where the perimeter wraps.
	getPerimeterT := func(x, y int) float64 {
		// Calculate position along perimeter: start at top-left, go clockwise
		var pos float64
		if y == top {
			// Top edge
			pos = float64(x - left)
		} else if x == right {
			// Right edge
			pos = width - 1 + float64(y-top)
		} else if y == bottom {
			// Bottom edge (right to left)
			pos = width - 1 + height - 1 + float64(right-x)
		} else {
			// Left edge (bottom to top)
			pos = width - 1 + height - 1 + width - 1 + float64(bottom-y)
		}
		t := pos / perimeter
		// Mirror: 0→1 for first half, 1→0 for second half
		if t <= 0.5 {
			return 2 * t
		}
		return 2 * (1 - t)
	}

	// Draw corners with gradient
	style := baseStyle
	style.Fg = g.At(getPerimeterT(left, top))
	buf.SetRune(left, top, chars.TopLeft, style)

	style.Fg = g.At(getPerimeterT(right, top))
	buf.SetRune(right, top, chars.TopRight, style)

	style.Fg = g.At(getPerimeterT(left, bottom))
	buf.SetRune(left, bottom, chars.BottomLeft, style)

	style.Fg = g.At(getPerimeterT(right, bottom))
	buf.SetRune(right, bottom, chars.BottomRight, style)

	// Draw top and bottom edges with gradient
	for x := left + 1; x < right; x++ {
		style.Fg = g.At(getPerimeterT(x, top))
		buf.SetRune(x, top, chars.Top, style)

		style.Fg = g.At(getPerimeterT(x, bottom))
		buf.SetRune(x, bottom, chars.Bottom, style)
	}

	// Draw left and right edges with gradient
	for y := top + 1; y < bottom; y++ {
		style.Fg = g.At(getPerimeterT(left, y))
		buf.SetRune(left, y, chars.Left, style)

		style.Fg = g.At(getPerimeterT(right, y))
		buf.SetRune(right, y, chars.Right, style)
	}
}

// DrawBoxClipped draws a box border clipped to the given clipRect.
// Positions are computed from the full rect, but only characters within
// clipRect are actually drawn. This enables partial border rendering
// when an element is partially scrolled out of view.
func DrawBoxClipped(buf *Buffer, rect Rect, border BorderStyle, style Style, clipRect Rect) {
	if rect.Width < 2 || rect.Height < 2 {
		return
	}
	if border == BorderNone {
		return
	}

	chars := border.Chars()

	left := rect.X
	right := rect.Right() - 1
	top := rect.Y
	bottom := rect.Bottom() - 1

	// Draw corners (only if within clip region)
	if clipRect.Contains(left, top) {
		buf.SetRune(left, top, chars.TopLeft, style)
	}
	if clipRect.Contains(right, top) {
		buf.SetRune(right, top, chars.TopRight, style)
	}
	if clipRect.Contains(left, bottom) {
		buf.SetRune(left, bottom, chars.BottomLeft, style)
	}
	if clipRect.Contains(right, bottom) {
		buf.SetRune(right, bottom, chars.BottomRight, style)
	}

	// Draw top and bottom edges
	for x := left + 1; x < right; x++ {
		if clipRect.Contains(x, top) {
			buf.SetRune(x, top, chars.Top, style)
		}
		if clipRect.Contains(x, bottom) {
			buf.SetRune(x, bottom, chars.Bottom, style)
		}
	}

	// Draw left and right edges
	for y := top + 1; y < bottom; y++ {
		if clipRect.Contains(left, y) {
			buf.SetRune(left, y, chars.Left, style)
		}
		if clipRect.Contains(right, y) {
			buf.SetRune(right, y, chars.Right, style)
		}
	}
}

// DrawBoxGradientClipped draws a gradient box border clipped to the given clipRect.
// Positions and gradient colors are computed from the full rect, but only
// characters within clipRect are actually drawn.
func DrawBoxGradientClipped(buf *Buffer, rect Rect, border BorderStyle, g Gradient, baseStyle Style, clipRect Rect) {
	if rect.Width < 2 || rect.Height < 2 {
		return
	}
	if border == BorderNone {
		return
	}

	chars := border.Chars()

	left := rect.X
	right := rect.Right() - 1
	top := rect.Y
	bottom := rect.Bottom() - 1
	width := float64(rect.Width)
	height := float64(rect.Height)
	perimeter := 2*width + 2*height - 4

	// Mirrored perimeter t: Start→End over first half, End→Start over second half.
	getPerimeterT := func(x, y int) float64 {
		var pos float64
		if y == top {
			pos = float64(x - left)
		} else if x == right {
			pos = width - 1 + float64(y-top)
		} else if y == bottom {
			pos = width - 1 + height - 1 + float64(right-x)
		} else {
			pos = width - 1 + height - 1 + width - 1 + float64(bottom-y)
		}
		t := pos / perimeter
		if t <= 0.5 {
			return 2 * t
		}
		return 2 * (1 - t)
	}

	style := baseStyle

	// Draw corners with gradient (only if within clip region)
	if clipRect.Contains(left, top) {
		style.Fg = g.At(getPerimeterT(left, top))
		buf.SetRune(left, top, chars.TopLeft, style)
	}
	if clipRect.Contains(right, top) {
		style.Fg = g.At(getPerimeterT(right, top))
		buf.SetRune(right, top, chars.TopRight, style)
	}
	if clipRect.Contains(left, bottom) {
		style.Fg = g.At(getPerimeterT(left, bottom))
		buf.SetRune(left, bottom, chars.BottomLeft, style)
	}
	if clipRect.Contains(right, bottom) {
		style.Fg = g.At(getPerimeterT(right, bottom))
		buf.SetRune(right, bottom, chars.BottomRight, style)
	}

	// Draw top and bottom edges with gradient
	for x := left + 1; x < right; x++ {
		if clipRect.Contains(x, top) {
			style.Fg = g.At(getPerimeterT(x, top))
			buf.SetRune(x, top, chars.Top, style)
		}
		if clipRect.Contains(x, bottom) {
			style.Fg = g.At(getPerimeterT(x, bottom))
			buf.SetRune(x, bottom, chars.Bottom, style)
		}
	}

	// Draw left and right edges with gradient
	for y := top + 1; y < bottom; y++ {
		if clipRect.Contains(left, y) {
			style.Fg = g.At(getPerimeterT(left, y))
			buf.SetRune(left, y, chars.Left, style)
		}
		if clipRect.Contains(right, y) {
			style.Fg = g.At(getPerimeterT(right, y))
			buf.SetRune(right, y, chars.Right, style)
		}
	}
}

// DrawBoxWithTitle draws a box border with a title in the top border.
// The title is centered in the top border and truncated if too long.
// If the rectangle is smaller than 2x2, the function does nothing.
func DrawBoxWithTitle(buf *Buffer, rect Rect, border BorderStyle, title string, style Style) {
	if rect.Width < 2 || rect.Height < 2 {
		return
	}
	if border == BorderNone {
		return
	}

	// First draw the box
	DrawBox(buf, rect, border, style)

	// Draw the title
	drawBoxTitle(buf, rect, title, style)
}

// titleCluster is one grapheme cluster of a border title with its display width.
type titleCluster struct {
	text  string
	width int
}

// drawBoxTitle draws a centered title string on the top border line of the box.
// drawBoxTitle does not draw the box itself — use DrawBoxWithTitle for that.
func drawBoxTitle(buf *Buffer, rect Rect, title string, style Style) {
	clusters, startX, ok := prepareTitle(title, rect)
	if !ok {
		return
	}
	x := startX
	for _, c := range clusters {
		buf.setCluster(x, rect.Y, c.text, c.width, style, "")
		x += c.width
	}
}

// drawBoxTitleClipped draws a centered title string on the top border line,
// skipping any cluster outside the given clip rectangle.
func drawBoxTitleClipped(buf *Buffer, rect Rect, title string, style Style, clipRect Rect) {
	// Reject if the title row is outside the clip rect's Y range.
	if rect.Y < clipRect.Y || rect.Y >= clipRect.Y+clipRect.Height {
		return
	}
	clusters, startX, ok := prepareTitle(title, rect)
	if !ok {
		return
	}
	x := startX
	for _, c := range clusters {
		if x >= clipRect.X && x+c.width <= clipRect.X+clipRect.Width {
			buf.setCluster(x, rect.Y, c.text, c.width, style, "")
		}
		x += c.width
	}
}

// prepareTitle truncates and centers a title for the given rectangle, breaking
// on grapheme-cluster boundaries so a cluster is never split at the clip edge.
func prepareTitle(title string, rect Rect) (clusters []titleCluster, startX int, ok bool) {
	availableWidth := rect.Width - 2
	if availableWidth <= 0 {
		return nil, 0, false
	}
	titleWidth := 0
	rest := title
	for len(rest) > 0 {
		cluster, w, size := nextCluster(rest)
		if size == 0 {
			break
		}
		if titleWidth+w > availableWidth {
			break
		}
		clusters = append(clusters, titleCluster{text: cluster, width: w})
		titleWidth += w
		rest = rest[size:]
	}
	if len(clusters) == 0 {
		return nil, 0, false
	}
	startX = rect.X + 1 + (availableWidth-titleWidth)/2
	return clusters, startX, true
}

// FillBox fills the interior of a box (excluding the border) with a character and style.
// This is useful for clearing the interior before drawing content.
func FillBox(buf *Buffer, rect Rect, r rune, style Style) {
	if rect.Width <= 2 || rect.Height <= 2 {
		return
	}

	interior := rect.Inset(EdgeAll(1))
	buf.Fill(interior, r, style)
}