gitstack

grindlemire/go-tui code browser

4.2 KB Go 145 lines 2026-06-27 · bdb505e 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
package tui

// TextAreaOption configures a TextArea.
type TextAreaOption func(*TextArea)

// --- Sizing Options ---

// WithTextAreaWidth sets the text area width in characters.
func WithTextAreaWidth(cells int) TextAreaOption {
	return func(t *TextArea) {
		t.width = cells
	}
}

// WithTextAreaMaxHeight sets the maximum height in rows (0 = unlimited).
func WithTextAreaMaxHeight(rows int) TextAreaOption {
	return func(t *TextArea) {
		t.maxHeight = rows
	}
}

// --- Visual Options ---

// WithTextAreaBorder sets the border style.
func WithTextAreaBorder(b BorderStyle) TextAreaOption {
	return func(t *TextArea) {
		t.border = b
	}
}

// WithTextAreaTextStyle sets the text style.
func WithTextAreaTextStyle(s Style) TextAreaOption {
	return func(t *TextArea) {
		t.textStyle = s
	}
}

// WithTextAreaPlaceholder sets placeholder text shown when empty and unfocused.
func WithTextAreaPlaceholder(text string) TextAreaOption {
	return func(t *TextArea) {
		t.placeholder = text
	}
}

// WithTextAreaPlaceholderStyle sets the placeholder text style (defaults to dim).
func WithTextAreaPlaceholderStyle(s Style) TextAreaOption {
	return func(t *TextArea) {
		t.placeholderStyle = s
	}
}

// WithTextAreaCursorRune sets the drawn cursor glyph used in virtual-cursor
// mode (defaults to '▌'). Only takes effect together with
// WithTextAreaVirtualCursor; the default real-cursor mode draws no glyph.
func WithTextAreaCursorRune(r rune) TextAreaOption {
	return func(t *TextArea) {
		t.cursorRune = r
	}
}

// WithTextAreaCursor sets the drawn cursor glyph.
//
// Deprecated: use WithTextAreaCursorRune. Retained as an alias so existing call
// sites keep compiling.
func WithTextAreaCursor(r rune) TextAreaOption {
	return WithTextAreaCursorRune(r)
}

// WithTextAreaVirtualCursor switches the text area to the drawn '▌' cursor glyph
// instead of the framework-driven real terminal cursor. Presence enables it; the
// glyph is customizable via WithTextAreaCursorRune. By default (option absent)
// the real terminal cursor is used and no glyph is drawn.
func WithTextAreaVirtualCursor() TextAreaOption {
	return func(t *TextArea) {
		t.hideVirtualCursor = false
	}
}

// WithTextAreaFocusColor sets the border color when focused.
func WithTextAreaFocusColor(c Color) TextAreaOption {
	return func(t *TextArea) {
		t.focusColor = &c
	}
}

// WithTextAreaBorderGradient sets a gradient for the border color when unfocused.
func WithTextAreaBorderGradient(g Gradient) TextAreaOption {
	return func(t *TextArea) {
		t.borderGradient = &g
	}
}

// WithTextAreaFocusGradient sets a gradient for the border color when focused.
// Takes priority over focusColor when set.
func WithTextAreaFocusGradient(g Gradient) TextAreaOption {
	return func(t *TextArea) {
		t.focusGradient = &g
	}
}

// --- Behavior Options ---

// WithTextAreaSubmitKey sets the key that triggers submit.
// Default is KeyEnter (Enter submits, Ctrl+J inserts newline).
// For long-form text, use a different key (e.g. a function key) so Enter inserts newline.
func WithTextAreaSubmitKey(k Key) TextAreaOption {
	return func(t *TextArea) {
		t.submitKey = k
	}
}

// WithTextAreaValue binds the TextArea to an external State for its text content.
// The TextArea reads from and writes to this state directly, enabling reactive
// two-way binding between the TextArea and the parent component.
func WithTextAreaValue(state *State[string]) TextAreaOption {
	return func(t *TextArea) {
		t.text = state
		t.cursorPos = NewState(len([]rune(state.Get())))
	}
}

// WithTextAreaAutoFocus sets whether the text area should automatically
// receive focus when the element tree is first applied.
func WithTextAreaAutoFocus(auto bool) TextAreaOption {
	return func(t *TextArea) {
		t.autoFocus = auto
	}
}

// WithTextAreaOnSubmit sets the callback called when the submit key is pressed.
func WithTextAreaOnSubmit(fn func(string)) TextAreaOption {
	return func(t *TextArea) {
		t.onSubmit = fn
	}
}

// WithTextAreaOnChange sets the callback called when the text content changes.
// The callback receives the full text content after each change.
// It fires on all user edits (typing, backspace, delete) and programmatic
// writes via SetText/Clear.
func WithTextAreaOnChange(fn func(string)) TextAreaOption {
	return func(t *TextArea) {
		t.onChange = fn
	}
}