Skip to content

Commit

Permalink
replace input with huh
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Dec 31, 2023
1 parent ce32b38 commit 3a56e84
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions internal/ui/input.go
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
}

0 comments on commit 3a56e84

Please sign in to comment.