2024-03-13 10:08:22 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
2024-03-28 09:36:39 +00:00
|
|
|
"errors"
|
2024-03-13 10:08:22 +00:00
|
|
|
"strings"
|
|
|
|
|
2024-03-28 09:36:39 +00:00
|
|
|
"github.com/charmbracelet/huh"
|
2024-03-13 10:08:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Ask(prompt, placeholder string, required bool) (string, error) {
|
2024-03-28 09:36:39 +00:00
|
|
|
var input string
|
|
|
|
err := huh.NewInput().
|
|
|
|
Title(prompt).
|
|
|
|
Value(&input).
|
|
|
|
Placeholder(placeholder).Validate(func(s string) error {
|
|
|
|
if required && strings.TrimSpace(s) == "" {
|
|
|
|
return errors.New("required")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}).Run()
|
2024-03-13 10:08:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2024-03-28 09:36:39 +00:00
|
|
|
return strings.TrimSpace(input), nil
|
2024-03-13 10:08:22 +00:00
|
|
|
}
|