gitstack

grindlemire/go-tui code browser

2.6 KB Go 101 lines 2026-03-09 · e291a1e 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
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
	}
}

// WithInputCursor sets the cursor character (defaults to '▌').
func WithInputCursor(r rune) InputOption {
	return func(inp *Input) {
		inp.cursorRune = r
	}
}

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