refactor(a11yprompter): shared method for prompt defaults
This commit is contained in:
parent
2ee68411a7
commit
ab4cfb84d2
2 changed files with 15 additions and 6 deletions
|
|
@ -171,7 +171,7 @@ func TestAccessiblePrompter(t *testing.T) {
|
|||
|
||||
go func() {
|
||||
// Wait for prompt to appear
|
||||
_, err := console.ExpectString("Enter some characters (default: 12345abcdefg)")
|
||||
_, err := console.ExpectString("Enter some characters (default: 12345abcdefg):")
|
||||
require.NoError(t, err)
|
||||
|
||||
// Enter nothing
|
||||
|
|
|
|||
|
|
@ -77,6 +77,19 @@ func (p *accessiblePrompter) newForm(groups ...*huh.Group) *huh.Form {
|
|||
WithOutput(p.stdout)
|
||||
}
|
||||
|
||||
// addDefaultsToPrompt adds default values to the prompt string.
|
||||
func (p *accessiblePrompter) addDefaultsToPrompt(prompt string, defaultValues []string) string {
|
||||
if len(defaultValues) == 1 {
|
||||
prompt = fmt.Sprintf("%s (default: %s):", prompt, defaultValues[0])
|
||||
} else if len(defaultValues) > 1 {
|
||||
prompt = fmt.Sprintf("%s (defaults: %s):", prompt, strings.Join(defaultValues, ", "))
|
||||
} else {
|
||||
prompt = fmt.Sprintf("%s:", prompt)
|
||||
}
|
||||
|
||||
return prompt
|
||||
}
|
||||
|
||||
func (p *accessiblePrompter) Select(prompt, defaultValue string, options []string) (int, error) {
|
||||
var result int
|
||||
formOptions := []huh.Option[int]{}
|
||||
|
|
@ -136,11 +149,7 @@ func (p *accessiblePrompter) MultiSelect(prompt string, defaults []string, optio
|
|||
|
||||
func (p *accessiblePrompter) Input(prompt, defaultValue string) (string, error) {
|
||||
result := defaultValue
|
||||
if defaultValue != "" {
|
||||
prompt = fmt.Sprintf("%s (default: %s)", prompt, defaultValue)
|
||||
} else {
|
||||
prompt = fmt.Sprintf("%s:", prompt)
|
||||
}
|
||||
prompt = p.addDefaultsToPrompt(prompt, []string{defaultValue})
|
||||
form := p.newForm(
|
||||
huh.NewGroup(
|
||||
huh.NewInput().
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue