gitstack

grindlemire/go-tui code browser

3.4 KB Go 124 lines 2026-05-31 · 7614e93 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
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
	}
}

// WithTextAreaCursor sets the cursor character (defaults to '▌').
func WithTextAreaCursor(r rune) TextAreaOption {
	return func(t *TextArea) {
		t.cursorRune = r
	}
}

// WithTextAreaVirtualCursor controls whether the virtual cursor character
// is drawn in the text. When false, lineWithCursor returns the line unchanged
// and applications can position the terminal's hardware cursor instead.
func WithTextAreaVirtualCursor(visible bool) TextAreaOption {
	return func(t *TextArea) {
		t.hideVirtualCursor = !visible
	}
}

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