gitstack

grindlemire/go-tui code browser

3.3 KB Go 121 lines 2026-06-25 · 64b8b59 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
package tui

// InputOption configures an Input.
type InputOption func(*Input)

// WithInputWidth sets the input width in characters.
func WithInputWidth(cells int) InputOption {
	return func(inp *Input) {
		inp.width = cells
	}
}

// WithInputBorder sets the border style.
func WithInputBorder(b BorderStyle) InputOption {
	return func(inp *Input) {
		inp.border = b
	}
}

// WithInputTextStyle sets the text style.
func WithInputTextStyle(s Style) InputOption {
	return func(inp *Input) {
		inp.textStyle = s
	}
}

// WithInputPlaceholder sets placeholder text shown when empty and unfocused.
func WithInputPlaceholder(text string) InputOption {
	return func(inp *Input) {
		inp.placeholder = text
	}
}

// WithInputPlaceholderStyle sets the placeholder text style (defaults to dim).
func WithInputPlaceholderStyle(s Style) InputOption {
	return func(inp *Input) {
		inp.placeholderStyle = s
	}
}

// WithInputCursorRune sets the drawn cursor glyph used in virtual-cursor mode
// (defaults to '▌'). Only takes effect together with WithInputVirtualCursor; the
// default real-cursor mode draws no glyph.
func WithInputCursorRune(r rune) InputOption {
	return func(inp *Input) {
		inp.cursorRune = r
	}
}

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

// WithInputVirtualCursor switches the input to the drawn '▌' cursor glyph
// instead of the framework-driven real terminal cursor. Presence enables it; the
// glyph is customizable via WithInputCursorRune. By default (option absent) the
// real terminal cursor is used and no glyph is drawn.
func WithInputVirtualCursor() InputOption {
	return func(inp *Input) {
		inp.hideVirtualCursor = false
	}
}

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

// WithInputFocusColor sets the border color when focused.
func WithInputFocusColor(c Color) InputOption {
	return func(inp *Input) {
		inp.focusColor = &c
	}
}

// WithInputBorderGradient sets a gradient for the border color when unfocused.
func WithInputBorderGradient(g Gradient) InputOption {
	return func(inp *Input) {
		inp.borderGradient = &g
	}
}

// WithInputFocusGradient sets a gradient for the border color when focused.
// Takes priority over focusColor when set.
func WithInputFocusGradient(g Gradient) InputOption {
	return func(inp *Input) {
		inp.focusGradient = &g
	}
}

// WithInputAutoFocus sets whether the input should automatically receive
// focus when the element tree is first applied.
func WithInputAutoFocus(auto bool) InputOption {
	return func(inp *Input) {
		inp.autoFocus = auto
	}
}

// WithInputOnSubmit sets the callback called when Enter is pressed.
func WithInputOnSubmit(fn func(string)) InputOption {
	return func(inp *Input) {
		inp.onSubmit = fn
	}
}

// WithInputOnChange sets the callback called when the text changes.
func WithInputOnChange(fn func(string)) InputOption {
	return func(inp *Input) {
		inp.onChange = fn
	}
}