gitstack

grindlemire/go-tui code browser

4.5 KB Go 185 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
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()
}

// 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
}

// 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
}

// 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.
// Width is measured per grapheme cluster, so 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.
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
}