Skip to content

Commit

Permalink
survey -> huh
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Dec 31, 2023
1 parent 3a56e84 commit 4616793
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 77 deletions.
28 changes: 11 additions & 17 deletions internal/ui/ask/export.go
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 5 additions & 9 deletions internal/ui/confirm.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
58 changes: 24 additions & 34 deletions internal/ui/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import (
"errors"
"fmt"
"os"
"path/filepath"

"github.com/AlecAivazis/survey/v2"
"github.com/charmbracelet/huh"
)

type fileSelectorOpt struct {
Expand All @@ -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 != "" {
Expand Down
23 changes: 6 additions & 17 deletions internal/ui/input.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
}
14 changes: 14 additions & 0 deletions internal/ui/validators.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 4616793

Please sign in to comment.