gitstack

grindlemire/go-tui code browser

6.3 KB Go 232 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package tui

import (
	"testing"
)

func TestInputOptions_ConfigureFields(t *testing.T) {
	boldStyle := NewStyle().Bold()
	italicStyle := NewStyle().Italic()
	borderGrad := NewGradient(Red, Blue)
	focusGrad := NewGradient(Green, Yellow)

	type tc struct {
		opt   InputOption
		check func(t *testing.T, inp *Input)
	}

	tests := map[string]tc{
		"WithInputWidth sets width": {
			opt: WithInputWidth(42),
			check: func(t *testing.T, inp *Input) {
				if inp.width != 42 {
					t.Errorf("width = %d, want 42", inp.width)
				}
			},
		},
		"WithInputBorder sets border style": {
			opt: WithInputBorder(BorderDouble),
			check: func(t *testing.T, inp *Input) {
				if inp.border != BorderDouble {
					t.Errorf("border = %v, want BorderDouble", inp.border)
				}
			},
		},
		"WithInputTextStyle sets text style": {
			opt: WithInputTextStyle(boldStyle),
			check: func(t *testing.T, inp *Input) {
				if inp.textStyle != boldStyle {
					t.Errorf("textStyle = %+v, want %+v", inp.textStyle, boldStyle)
				}
			},
		},
		"WithInputPlaceholder sets placeholder text": {
			opt: WithInputPlaceholder("type here"),
			check: func(t *testing.T, inp *Input) {
				if inp.placeholder != "type here" {
					t.Errorf("placeholder = %q, want %q", inp.placeholder, "type here")
				}
			},
		},
		"WithInputPlaceholderStyle overrides dim default": {
			opt: WithInputPlaceholderStyle(italicStyle),
			check: func(t *testing.T, inp *Input) {
				if inp.placeholderStyle != italicStyle {
					t.Errorf("placeholderStyle = %+v, want %+v", inp.placeholderStyle, italicStyle)
				}
			},
		},
		"WithInputCursorRune sets cursor rune": {
			opt: WithInputCursorRune('_'),
			check: func(t *testing.T, inp *Input) {
				if inp.cursorRune != '_' {
					t.Errorf("cursorRune = %q, want '_'", inp.cursorRune)
				}
			},
		},
		"WithInputCursor alias sets cursor rune": {
			opt: WithInputCursor('#'),
			check: func(t *testing.T, inp *Input) {
				if inp.cursorRune != '#' {
					t.Errorf("cursorRune = %q, want '#'", inp.cursorRune)
				}
			},
		},
		"WithInputVirtualCursor enables drawn glyph": {
			opt: WithInputVirtualCursor(),
			check: func(t *testing.T, inp *Input) {
				if inp.hideVirtualCursor {
					t.Error("expected hideVirtualCursor to be false after WithInputVirtualCursor()")
				}
			},
		},
		"WithInputFocusColor sets focus color": {
			opt: WithInputFocusColor(Cyan),
			check: func(t *testing.T, inp *Input) {
				if inp.focusColor == nil || *inp.focusColor != Cyan {
					t.Errorf("focusColor = %+v, want Cyan", inp.focusColor)
				}
			},
		},
		"WithInputBorderGradient sets unfocused gradient": {
			opt: WithInputBorderGradient(borderGrad),
			check: func(t *testing.T, inp *Input) {
				if inp.borderGradient == nil || *inp.borderGradient != borderGrad {
					t.Errorf("borderGradient = %+v, want %+v", inp.borderGradient, borderGrad)
				}
			},
		},
		"WithInputFocusGradient sets focused gradient": {
			opt: WithInputFocusGradient(focusGrad),
			check: func(t *testing.T, inp *Input) {
				if inp.focusGradient == nil || *inp.focusGradient != focusGrad {
					t.Errorf("focusGradient = %+v, want %+v", inp.focusGradient, focusGrad)
				}
			},
		},
		"WithInputAutoFocus enables auto focus": {
			opt: WithInputAutoFocus(true),
			check: func(t *testing.T, inp *Input) {
				if !inp.autoFocus {
					t.Error("autoFocus = false, want true")
				}
				root := inp.Render(testApp)
				if !root.IsAutoFocus() {
					t.Error("rendered element should request auto focus")
				}
			},
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			inp := NewInput(tt.opt)
			inp.BindApp(testApp)
			tt.check(t, inp)
		})
	}
}

func TestInputOptions_WithInputValue(t *testing.T) {
	type tc struct {
		initial    string
		wantCursor int
	}

	tests := map[string]tc{
		"empty state starts cursor at zero":  {initial: "", wantCursor: 0},
		"prefilled state puts cursor at end": {initial: "hello", wantCursor: 5},
		"multibyte state counts runes":       {initial: "a界🙂", wantCursor: 3},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			external := NewState(tt.initial)
			inp := NewInput(WithInputValue(external))
			inp.BindApp(testApp)

			if inp.text != external {
				t.Fatal("input should use the external state instance directly")
			}
			if got := inp.Text(); got != tt.initial {
				t.Errorf("Text() = %q, want %q", got, tt.initial)
			}
			if got := inp.cursorPos.Get(); got != tt.wantCursor {
				t.Errorf("cursorPos = %d, want %d", got, tt.wantCursor)
			}
			if got := inp.scrollPos.Get(); got != 0 {
				t.Errorf("scrollPos = %d, want 0", got)
			}

			// Two-way binding: typing into the input updates the external state.
			inp.HandleEvent(KeyEvent{Key: KeyRune, Rune: '!'})
			if got := external.Get(); got != tt.initial+"!" {
				t.Errorf("external state = %q, want %q", got, tt.initial+"!")
			}
		})
	}
}

func TestInputOptions_Callbacks(t *testing.T) {
	type tc struct {
		makeOpt   func(record *string) InputOption
		event     KeyEvent
		preset    string
		wantValue string
	}

	tests := map[string]tc{
		"WithInputOnSubmit fires on enter": {
			makeOpt: func(record *string) InputOption {
				return WithInputOnSubmit(func(s string) { *record = "submit:" + s })
			},
			preset:    "done",
			event:     KeyEvent{Key: KeyEnter},
			wantValue: "submit:done",
		},
		"WithInputOnChange fires on insert": {
			makeOpt: func(record *string) InputOption {
				return WithInputOnChange(func(s string) { *record = "change:" + s })
			},
			preset:    "a",
			event:     KeyEvent{Key: KeyRune, Rune: 'b'},
			wantValue: "change:ab",
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			var record string
			inp := NewInput(tt.makeOpt(&record))
			inp.BindApp(testApp)
			inp.SetText(tt.preset)

			inp.HandleEvent(tt.event)
			if record != tt.wantValue {
				t.Errorf("callback recorded %q, want %q", record, tt.wantValue)
			}
		})
	}
}

func TestInputOptions_OnChangeFiresOnSetTextAndClear(t *testing.T) {
	var changes []string
	inp := NewInput(WithInputOnChange(func(s string) {
		changes = append(changes, s)
	}))
	inp.BindApp(testApp)

	// SetText fires onChange.
	changes = nil
	inp.SetText("hello")
	if len(changes) != 1 || changes[0] != "hello" {
		t.Fatalf("after SetText: changes = %v, want [hello]", changes)
	}

	// Clear fires onChange.
	changes = nil
	inp.Clear()
	if len(changes) != 1 || changes[0] != "" {
		t.Fatalf("after Clear: changes = %v, want ['']", changes)
	}
}