Skip to content

Commit 69e2b68

Browse files
maksim-zuev-dbIsaac
andcommitted
Show a blank prompt glyph until invalid input is edited
The prompt glyph ran Validate on every render, so a prompt that rejects empty input, such as the login host prompt, opened with a red cross before anything was typed. The glyph is now blank until the first edit or Enter, then shows a cross while the input is invalid and a checkmark once it is valid. Co-authored-by: Isaac <no-reply@databricks.com>
1 parent ecd576f commit 69e2b68

5 files changed

Lines changed: 64 additions & 6 deletions

File tree

‎libs/cmdio/cmdiotest/prompt_validate_baseline_test.go‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,47 @@ func TestPromptBaseline_Validate(t *testing.T) {
4545
require.NoError(t, err, "raw output: %q", tm.Raw())
4646
assert.Equal(t, "https://example.com", v, "snapshot:\n%s", tm.Snapshot())
4747
}
48+
49+
// TestPromptBaseline_ValidateEnterBeforeEdit pins that Enter on an untouched
50+
// prompt flips the glyph to "✗" along with the error line.
51+
func TestPromptBaseline_ValidateEnterBeforeEdit(t *testing.T) {
52+
t.Parallel()
53+
tm := termtest.NewPrompt(t, cmdio.PromptOptions{
54+
Label: "Workspace host",
55+
Validate: func(s string) error {
56+
if s == "" {
57+
return errors.New("host is required")
58+
}
59+
return nil
60+
},
61+
})
62+
tm.WaitFor("Workspace host")
63+
tm.Type(termtest.KeyEnter)
64+
tm.Golden("01-enter")
65+
66+
tm.Type(termtest.KeyCtrlC)
67+
_, err := tm.Result()
68+
assert.Error(t, err)
69+
}
70+
71+
// TestPromptBaseline_ValidateValidBeforeEdit pins that an untouched prompt
72+
// whose input already passes Validate shows "✔".
73+
func TestPromptBaseline_ValidateValidBeforeEdit(t *testing.T) {
74+
t.Parallel()
75+
tm := termtest.NewPrompt(t, cmdio.PromptOptions{
76+
Label: "Workspace host",
77+
Validate: func(s string) error {
78+
if strings.Contains(s, " ") {
79+
return errors.New("must not contain spaces")
80+
}
81+
return nil
82+
},
83+
})
84+
tm.WaitFor("Workspace host")
85+
tm.Golden("01-initial")
86+
87+
tm.Type(termtest.KeyEnter)
88+
v, err := tm.Result()
89+
require.NoError(t, err, "raw output: %q", tm.Raw())
90+
assert.Empty(t, v)
91+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
✗ Workspace host: █
1+
Workspace host: █
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
✗ Workspace host: █
2+
>> host is required
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
✔ Workspace host: █

‎libs/cmdio/prompt.go‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ type PromptOptions struct {
3333
// Validate, when set, is called on every keystroke. While it returns a
3434
// non-nil error the leading glyph flips from "✔" to "✗" and Enter is
3535
// inert; pressing Enter while invalid surfaces the returned error
36-
// below the prompt until the next edit.
36+
// below the prompt until the next edit. Until the first edit or Enter,
37+
// an invalid input shows a blank glyph instead, so an empty prompt does
38+
// not open as an error.
3739
Validate func(input string) error
3840
}
3941

@@ -73,6 +75,9 @@ type promptModel struct {
7375
deleted bool
7476
submitted bool
7577

78+
// edited is set by the first change to runes.
79+
edited bool
80+
7681
// submitErr is the error from the last failed Enter attempt. Rendered
7782
// below the prompt and cleared on the next edit.
7883
submitErr error
@@ -92,12 +97,16 @@ func (m *promptModel) value() string {
9297
}
9398

9499
// glyph returns the leading status indicator: bold-green ✔ when valid,
95-
// bold-red ✗ when validate rejects the buffer.
100+
// bold-red ✗ when validate rejects the buffer after an edit or Enter, and a
101+
// blank before that.
96102
func (m *promptModel) glyph() string {
97-
if m.validate != nil && m.validate(m.value()) != nil {
98-
return ansiBold + ansiRed + glyphInvalid + ansiReset
103+
if m.validate == nil || m.validate(m.value()) == nil {
104+
return ansiBold + ansiGreen + glyphValid + ansiReset
105+
}
106+
if !m.edited && m.submitErr == nil {
107+
return " "
99108
}
100-
return ansiBold + ansiGreen + glyphValid + ansiReset
109+
return ansiBold + ansiRed + glyphInvalid + ansiReset
101110
}
102111

103112
func (m *promptModel) Init() tea.Cmd { return nil }
@@ -154,6 +163,7 @@ func (m *promptModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
154163
}
155164
m.runes = append(m.runes[:m.cursor-1], m.runes[m.cursor:]...)
156165
m.cursor--
166+
m.edited = true
157167
m.submitErr = nil
158168

159169
case tea.KeyRunes, tea.KeySpace:
@@ -172,6 +182,7 @@ func (m *promptModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
172182
m.runes = append(m.runes[:m.cursor], typed...)
173183
m.runes = append(m.runes, tail...)
174184
m.cursor += len(typed)
185+
m.edited = true
175186
m.submitErr = nil
176187

177188
default:

0 commit comments

Comments
 (0)