Skip to content

Commit 9d98828

Browse files
srtaalejzimeg
andauthored
feat: show confirm prompts as a vertical selection (#564)
Co-authored-by: Eden Zimbelman <eden.zimbelman@salesforce.com>
1 parent ae61a10 commit 9d98828

2 files changed

Lines changed: 25 additions & 11 deletions

File tree

internal/iostreams/forms.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,12 @@ func inputForm(io *IOStreams, _ context.Context, message string, cfg InputPrompt
8282

8383
// buildConfirmForm constructs an interactive form for yes/no confirmation prompts.
8484
func buildConfirmForm(io *IOStreams, message string, choice *bool) *huh.Form {
85-
field := huh.NewConfirm().
85+
field := huh.NewSelect[bool]().
8686
Title(message).
87+
Options(
88+
huh.NewOption("Yes", true),
89+
huh.NewOption("No", false),
90+
).
8791
Value(choice)
8892
return newForm(io, field)
8993
}

internal/iostreams/forms_test.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,26 +105,36 @@ func TestConfirmForm(t *testing.T) {
105105
assert.Contains(t, view, "No")
106106
})
107107

108-
t.Run("default value is respected", func(t *testing.T) {
108+
t.Run("default true starts on Yes", func(t *testing.T) {
109109
choice := true
110110
f := buildConfirmForm(nil, "Continue?", &choice)
111111
f.Update(f.Init())
112112

113-
assert.True(t, choice)
113+
view := ansi.Strip(f.View())
114+
assert.Contains(t, view, style.Chevron()+" Yes")
114115
})
115116

116-
t.Run("toggle changes value", func(t *testing.T) {
117+
t.Run("default false starts on No", func(t *testing.T) {
117118
choice := false
118119
f := buildConfirmForm(nil, "Continue?", &choice)
119120
f.Update(f.Init())
120121

121-
// Toggle to Yes
122-
f.Update(tea.KeyPressMsg{Code: tea.KeyLeft})
123-
assert.True(t, choice)
122+
view := ansi.Strip(f.View())
123+
assert.Contains(t, view, style.Chevron()+" No")
124+
})
125+
126+
t.Run("arrow keys change selection", func(t *testing.T) {
127+
choice := true
128+
f := buildConfirmForm(nil, "Continue?", &choice)
129+
f.Update(f.Init())
124130

125-
// Toggle back to No
126-
f.Update(tea.KeyPressMsg{Code: tea.KeyRight})
131+
// Move down to No
132+
f.Update(tea.KeyPressMsg{Code: tea.KeyDown})
127133
assert.False(t, choice)
134+
135+
// Move back up to Yes
136+
f.Update(tea.KeyPressMsg{Code: tea.KeyUp})
137+
assert.True(t, choice)
128138
})
129139
}
130140

@@ -450,12 +460,12 @@ func TestFormsAccessible(t *testing.T) {
450460
assert.Contains(t, out.String(), "Pick one (press Enter for 1)")
451461
})
452462

453-
t.Run("confirm form accepts yes/no input", func(t *testing.T) {
463+
t.Run("confirm form accepts numbered input", func(t *testing.T) {
454464
var choice bool
455465
f := buildConfirmForm(io, "Continue?", &choice)
456466

457467
var out strings.Builder
458-
err := f.WithOutput(&out).WithInput(strings.NewReader("y\n")).Run()
468+
err := f.WithOutput(&out).WithInput(strings.NewReader("1\n")).Run()
459469

460470
assert.NoError(t, err)
461471
assert.True(t, choice)

0 commit comments

Comments
 (0)