diff --git a/internal/ui/ask/export.go b/internal/ui/ask/export.go index f3464dff..9efb01e6 100644 --- a/internal/ui/ask/export.go +++ b/internal/ui/ask/export.go @@ -1,28 +1,22 @@ package ask import ( - "github.com/AlecAivazis/survey/v2" + "github.com/charmbracelet/huh" "github.com/rusq/slackdump/v2/export" ) // ExportType asks the user to select an export type. func ExportType() (export.ExportType, error) { - mode := &survey.Select{ - Message: "Export type: ", - Options: []string{export.TMattermost.String(), export.TStandard.String()}, - Description: func(value string, index int) string { - descr := []string{ - "Mattermost bulk upload compatible export (see doc)", - "Standard export format", - } - return descr[index] - }, - } - var resp string - if err := survey.AskOne(mode, &resp); err != nil { + var resp export.ExportType + q := huh.NewSelect[export.ExportType](). + Title("Export type: "). + Options( + huh.NewOption("Mattermost bulk upload compatible export (see doc)", export.TMattermost), + huh.NewOption("Standard export format", export.TStandard), + ). + Value(&resp) + if err := q.Run(); err != nil { return 0, err } - var t export.ExportType - t.Set(resp) - return t, nil + return resp, nil } diff --git a/internal/ui/confirm.go b/internal/ui/confirm.go index c4d1a78f..ce23b726 100644 --- a/internal/ui/confirm.go +++ b/internal/ui/confirm.go @@ -1,18 +1,14 @@ package ui -import "github.com/AlecAivazis/survey/v2" +import ( + "github.com/charmbracelet/huh" +) -func Confirm(msg string, defavlt bool, opt ...Option) (bool, error) { +func Confirm(msg string, _ bool, opt ...Option) (bool, error) { var opts = defaultOpts().apply(opt...) - q := &survey.Confirm{ - Message: msg, - Help: opts.help, - Default: defavlt, - } - var b bool - if err := survey.AskOne(q, &b, opts.surveyOpts()...); err != nil { + if err := huh.NewConfirm().Title(msg).Description(opts.help).Value(&b).Run(); err != nil { return false, err } return b, nil diff --git a/internal/ui/filesystem.go b/internal/ui/filesystem.go index 193f199c..a07e3cd6 100644 --- a/internal/ui/filesystem.go +++ b/internal/ui/filesystem.go @@ -4,9 +4,8 @@ import ( "errors" "fmt" "os" - "path/filepath" - "github.com/AlecAivazis/survey/v2" + "github.com/charmbracelet/huh" ) type fileSelectorOpt struct { @@ -29,43 +28,34 @@ func WithMustExist(b bool) Option { func FileSelector(msg, descr string, opt ...Option) (string, error) { var opts = defaultOpts().apply(opt...) - var q = []*survey.Question{ - { - Name: "filename", - Prompt: &survey.Input{ - Message: msg, - Suggest: func(partname string) []string { - files, _ := filepath.Glob(partname + "*") - return files - }, - Help: descr, - }, - Validate: func(ans interface{}) error { - filename := ans.(string) - if filename == "" { - if opts.defaultFilename == "" { - return errors.New("empty filename") + var resp struct { + Filename string + } + q := huh.NewInput(). + Title(msg). + Description(descr). + Value(&resp.Filename). + Validate(func(ans string) error { + filename := ans + if filename == "" { + if opts.defaultFilename == "" { + return errors.New("empty filename") + } else { + if !opts.mustExist { + return nil } else { - if !opts.mustExist { - return nil - } else { - return checkExists(opts.defaultFilename) - } + return checkExists(opts.defaultFilename) } } - if opts.mustExist { - return checkExists(filename) - } - return nil - }, - }, - } + } + if opts.mustExist { + return checkExists(filename) + } + return nil + }) - var resp struct { - Filename string - } for { - if err := survey.Ask(q, &resp, opts.surveyOpts()...); err != nil { + if err := q.Run(); err != nil { return "", err } if resp.Filename == "" && opts.defaultFilename != "" { diff --git a/internal/ui/input.go b/internal/ui/input.go index 9fff5b97..70fca6ae 100644 --- a/internal/ui/input.go +++ b/internal/ui/input.go @@ -1,21 +1,19 @@ package ui import ( - "errors" - "github.com/charmbracelet/huh" ) // Input shows a text input field with a custom validator. -func Input(msg, help string, validator func(s string) error) (string, error) { - if validator == nil { - validator = noValidation +func Input(msg, help string, validateFn func(s string) error) (string, error) { + if validateFn == nil { + validateFn = NoValidation } var resp string if err := huh.NewText(). Title(msg). Description(help). - Validate(validator). + Validate(validateFn). Value(&resp). Run(); err != nil { return "", err @@ -25,19 +23,10 @@ func Input(msg, help string, validator func(s string) error) (string, error) { // StringRequire requires user to input string. func StringRequire(msg, help string) (string, error) { - return Input(msg, help, func(s string) error { - if s == "" { - return errors.New("value is required") - } - return nil - }) + return Input(msg, help, ValidateNotEmpty) } // String asks user to input string, accepts an empty input. func String(msg, help string) (string, error) { - return Input(msg, help, noValidation) -} - -func noValidation(s string) error { - return nil + return Input(msg, help, NoValidation) } diff --git a/internal/ui/validators.go b/internal/ui/validators.go new file mode 100644 index 00000000..39e03dd2 --- /dev/null +++ b/internal/ui/validators.go @@ -0,0 +1,14 @@ +package ui + +import "errors" + +func NoValidation(s string) error { + return nil +} + +func ValidateNotEmpty(s string) error { + if s == "" { + return errors.New("value is required") + } + return nil +}