gitstack

grindlemire/go-tui code browser

7.4 KB Go 264 lines 2026-06-27 · af23124 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
package tui

// GetRoot implements Viewable. Returns the element itself.
func (e *Element) GetRoot() *Element { return e }

// GetWatchers implements Viewable. Elements have no standalone watchers.
func (e *Element) GetWatchers() []Watcher { return nil }

// SetStyle updates the layout style and marks the element dirty.
func (e *Element) SetStyle(style LayoutStyle) {
	e.style = style
	e.MarkDirty()
}

// SetHeight updates the element's height after creation and marks it (and its
// ancestors) dirty so layout recomputes on the next frame. Intended for retained
// elements held across renders; a setter on an element recreated each render has
// no lasting effect. Pass tui.Fixed(n), tui.Percent(p), or tui.Auto().
func (e *Element) SetHeight(v Value) {
	e.style.Height = v
	e.MarkDirty()
}

// SetWidth updates the element's width after creation and marks it (and its
// ancestors) dirty so layout recomputes on the next frame. Intended for retained
// elements held across renders; a setter on an element recreated each render has
// no lasting effect. Pass tui.Fixed(n), tui.Percent(p), or tui.Auto().
func (e *Element) SetWidth(v Value) {
	e.style.Width = v
	e.MarkDirty()
}

// Style returns the current layout style.
func (e *Element) Style() LayoutStyle {
	return e.style
}

// Border returns the border style.
func (e *Element) Border() BorderStyle {
	return e.border
}

// SetBorder sets the border style.
func (e *Element) SetBorder(border BorderStyle) {
	e.border = border
}

// BorderStyle returns the style used to render the border.
func (e *Element) BorderStyle() Style {
	return e.borderStyle
}

// FocusBorderStyle returns the border style used when focused, or nil.
func (e *Element) FocusBorderStyle() *Style {
	return e.focusBorderStyle
}

// SetFocusBorderStyle sets the border style used when focused (nil = no change on focus).
func (e *Element) SetFocusBorderStyle(s *Style) {
	e.focusBorderStyle = s
}

// activeBorderStyle returns the border style to use for rendering.
// If the element is focused and a focus border style is set, that is used;
// otherwise the normal border style is the fallback.
func (e *Element) activeBorderStyle() Style {
	if e.focused && e.focusBorderStyle != nil {
		return *e.focusBorderStyle
	}
	return e.borderStyle
}

// SetBorderStyle sets the style used to render the border.
func (e *Element) SetBorderStyle(style Style) {
	e.borderStyle = style
}

// BorderTitle returns the title string drawn in the top border, or "" if none.
func (e *Element) BorderTitle() string {
	return e.borderTitle
}

// SetBorderTitle sets the title string drawn in the top border.
func (e *Element) SetBorderTitle(title string) {
	e.borderTitle = title
}

// BorderTitleAlign returns the alignment of the border title.
func (e *Element) BorderTitleAlign() TextAlign {
	return e.borderTitleAlign
}

// SetBorderTitleAlign sets the alignment of the border title.
func (e *Element) SetBorderTitleAlign(align TextAlign) {
	e.borderTitleAlign = align
}

// BorderTitleStyle returns the title text style, or nil if using borderStyle.
func (e *Element) BorderTitleStyle() *Style {
	return e.borderTitleStyle
}

// SetBorderTitleStyle sets the title text style (nil = use borderStyle).
func (e *Element) SetBorderTitleStyle(s *Style) {
	e.borderTitleStyle = s
}

// titleStyle returns the style to use for the border title.
// If a specific title style was set via WithBorderTitleStyle, it is used;
// otherwise the element's active border style (focus-aware) is the fallback.
func (e *Element) titleStyle() Style {
	if e.borderTitleStyle != nil {
		return *e.borderTitleStyle
	}
	return e.activeBorderStyle()
}

// Background returns the background style, or nil if transparent.
func (e *Element) Background() *Style {
	return e.background
}

// SetBackground sets the background style. Pass nil for transparent.
func (e *Element) SetBackground(style *Style) {
	e.background = style
}

// --- Text API ---

// Text returns the text content.
func (e *Element) Text() string {
	return e.text
}

// SetText updates the text content.
// Width remains Auto so the flex algorithm uses IntrinsicSize(),
// which correctly accounts for text dimensions, padding, and border.
func (e *Element) SetText(content string) {
	e.text = content
	e.richText = nil
	e.MarkDirty()
}

// TextStyle returns the style used to render the text.
func (e *Element) TextStyle() Style {
	return e.textStyle
}

// SetTextStyle sets the style used to render the text.
// Setting this explicitly prevents inheritance from the parent element.
func (e *Element) SetTextStyle(style Style) {
	e.textStyle = style
	e.textStyleSet = true
}

// TextAlign returns the text alignment.
func (e *Element) TextAlign() TextAlign {
	return e.textAlign
}

// SetTextAlign sets the text alignment.
func (e *Element) SetTextAlign(align TextAlign) {
	e.textAlign = align
}

// --- Truncate API ---

// Truncate returns whether text truncation is enabled.
func (e *Element) Truncate() bool {
	return e.truncate
}

// SetTruncate sets whether text should be truncated with ellipsis on overflow.
func (e *Element) SetTruncate(truncate bool) {
	e.truncate = truncate
	e.MarkDirty()
}

// --- Wrap API ---

// wrapsText returns true if this element should wrap text content.
func (e *Element) wrapsText() bool {
	return !e.noWrap
}

// Wrap returns whether text content wraps within this element's width.
// Wrapping is enabled by default.
func (e *Element) Wrap() bool {
	return !e.noWrap
}

// --- Hidden API ---

// Hidden returns whether this element is hidden.
func (e *Element) Hidden() bool {
	return e.hidden
}

// SetHidden sets whether this element is excluded from layout and rendering.
func (e *Element) SetHidden(hidden bool) {
	e.hidden = hidden
	e.MarkDirty()
}

// --- Overlay API ---

// IsOverlay returns whether this element is rendered in the overlay pass.
func (e *Element) IsOverlay() bool {
	return e.overlay
}

// Apply applies option functions to an existing element.
func (e *Element) Apply(opts ...Option) {
	for _, opt := range opts {
		opt(e)
	}
}

// --- Overflow API ---

// Overflow returns the overflow mode.
func (e *Element) Overflow() OverflowMode {
	return e.overflow
}

// SetOverflow sets the overflow mode.
func (e *Element) SetOverflow(mode OverflowMode) {
	e.overflow = mode
	e.MarkDirty()
}

// Component returns the component instance that rendered this element,
// or nil if the element was not created by a mounted component.
func (e *Element) Component() Component {
	return e.component
}

// stringWidth returns the display width of a string in terminal cells, measured
// per grapheme cluster. A flag, ZWJ family emoji, skin-tone emoji, or decomposed
// accented letter counts as the single glyph the terminal paints rather than the
// sum of its code points.
//
// StringWidth is the exported wrapper.
//
// StringWidth is the correct width function for strings and for cursor-column
// math: it measures whole grapheme clusters, so a multi-rune glyph advances the
// column by the cell width the terminal actually paints. Use it (not RuneWidth)
// when computing a cursor position or the width of a span of text. RuneWidth is
// low-level: it reports a single rune's width and is wrong for multi-rune
// clusters (a combining mark or ZWJ joiner counts as 1 there).
func StringWidth(s string) int { return stringWidth(s) }

func stringWidth(s string) int {
	width := 0
	for len(s) > 0 {
		_, cw, size := nextCluster(s)
		if size == 0 {
			break
		}
		width += cw
		s = s[size:]
	}
	return width
}