-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
27 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,43 @@ | ||
package ui | ||
|
||
import "github.com/AlecAivazis/survey/v2" | ||
import ( | ||
"errors" | ||
|
||
"github.com/charmbracelet/huh" | ||
) | ||
|
||
// Input shows a text input field with a custom validator. | ||
func Input(msg, help string, validator survey.Validator) (string, error) { | ||
qs := []*survey.Question{ | ||
{ | ||
Name: "value", | ||
Validate: validator, | ||
Prompt: &survey.Input{ | ||
Message: msg, | ||
Help: help, | ||
}, | ||
}, | ||
func Input(msg, help string, validator func(s string) error) (string, error) { | ||
if validator == nil { | ||
validator = noValidation | ||
} | ||
var m = struct { | ||
Value string | ||
}{} | ||
if err := survey.Ask(qs, &m); err != nil { | ||
var resp string | ||
if err := huh.NewText(). | ||
Title(msg). | ||
Description(help). | ||
Validate(validator). | ||
Value(&resp). | ||
Run(); err != nil { | ||
return "", err | ||
} | ||
return m.Value, nil | ||
return resp, nil | ||
} | ||
|
||
// StringRequire requires user to input string. | ||
func StringRequire(msg, help string) (string, error) { | ||
return Input(msg, help, survey.Required) | ||
return Input(msg, help, func(s string) error { | ||
if s == "" { | ||
return errors.New("value is required") | ||
} | ||
return nil | ||
}) | ||
} | ||
|
||
// String asks user to input string, accepts an empty input. | ||
func String(msg, help string) (string, error) { | ||
return Input(msg, help, nil) | ||
return Input(msg, help, noValidation) | ||
} | ||
|
||
func noValidation(s string) error { | ||
return nil | ||
} |