diff --git a/internal/cmd/put.go b/internal/cmd/put.go index f05ea4c..5a8dbf7 100644 --- a/internal/cmd/put.go +++ b/internal/cmd/put.go @@ -187,7 +187,7 @@ func putCmdRun(args []string, opts putOptions) error { if opts.prompt { prompt := fmt.Sprintf("Do you trash %s %q? ", posix.FileType(st), arg) choices := []string{"yes", "no", "all-yes", "quit"} - selected, err := tui.ChoicePrompt(prompt, choices, nil) + selected, err := tui.ChoicePrompt(prompt, choices) if err != nil { // canceled return err diff --git a/internal/cmd/restore.go b/internal/cmd/restore.go index 26625b6..1283a57 100644 --- a/internal/cmd/restore.go +++ b/internal/cmd/restore.go @@ -228,7 +228,7 @@ func doRestore(files []trash.File, restoreTo string, prompt bool) error { choice = []string{"new-name", "skip", "repeat-prev", "quit"} } // TODO: Make the message easy to understand - selected, err = tui.ChoicePrompt(fmt.Sprintf("Conflicted restore path %q\n\tPlease choose one of the following: ", file.OriginalPath), choice, nil) + selected, err = tui.ChoicePrompt(fmt.Sprintf("Conflicted restore path %q\n\tPlease choose one of the following: ", file.OriginalPath), choice) if err != nil { return err } diff --git a/internal/tui/boolInputModel.go b/internal/tui/boolInputModel.go index dff9f33..4a0ab81 100644 --- a/internal/tui/boolInputModel.go +++ b/internal/tui/boolInputModel.go @@ -2,6 +2,7 @@ package tui import ( "errors" + "strings" "github.com/charmbracelet/bubbles/textinput" tea "github.com/charmbracelet/bubbletea" @@ -12,25 +13,25 @@ type boolInputModel struct { confirmed bool } -func yesno(s string) (bool, error) { +func yesno(s string) (bool, string, error) { if s == "" { - return false, errors.New("empty") + return false, "", errors.New("empty") } - switch s[0:1] { + switch strings.ToLower(s[0:1]) { case "y": - return true, nil + return true, "Yes", nil case "n": - return false, nil + return false, "No", nil } - return false, errors.New("unknown") + return false, "", errors.New("unknown") } func newBoolInputModel(prompt string) boolInputModel { textInput := textinput.New() textInput.Prompt = prompt - textInput.Placeholder = "yes/no" + textInput.Placeholder = "(Yes/No)" textInput.Validate = func(value string) error { - _, err := yesno(value) + _, _, err := yesno(value) return err } textInput.Focus() @@ -57,8 +58,9 @@ func (m boolInputModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } var cmd tea.Cmd m.textInput, cmd = m.textInput.Update(msg) - if _, err := yesno(m.textInput.Value()); err == nil { + if _, value, err := yesno(m.textInput.Value()); err == nil { m.textInput.Blur() + m.textInput.SetValue(value) m.confirmed = true return m, tea.Quit } @@ -67,8 +69,7 @@ func (m boolInputModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m boolInputModel) Value() bool { valueStr := m.textInput.Value() - - v, _ := yesno(valueStr) + v, _, _ := yesno(valueStr) return v } diff --git a/internal/tui/choiceInputModel.go b/internal/tui/choiceInputModel.go index 41214d6..f52d00c 100644 --- a/internal/tui/choiceInputModel.go +++ b/internal/tui/choiceInputModel.go @@ -9,38 +9,37 @@ import ( ) type choiceInputModel struct { - textInput textinput.Model - keys map[string]string - defaultValue *string - confirmed bool + textInput textinput.Model + keys map[string]string + confirmed bool } -func newChoiceInputModel(prompt string, choices []string, defaultValue *string) choiceInputModel { +func newChoiceInputModel(prompt string, choices []string) choiceInputModel { textInput := textinput.New() textInput.Prompt = prompt - textInput.Placeholder = strings.Join(choices, "/") - if defaultValue != nil { - textInput.Placeholder += ", default " + *defaultValue + + for i := range choices { + choices[i] = strings.ToUpper(choices[i][:1]) + choices[i][1:] } + textInput.Placeholder = "(" + strings.Join(choices, "/") + ")" keys := make(map[string]string) for _, choice := range choices { - keys[choice[0:1]] = choice + keys[strings.ToLower(choice[0:1])] = choice } textInput.Validate = func(s string) error { - if s == "" && defaultValue != nil { - return nil + if s == "" { + return errors.New("empty") } - if _, ok := keys[s]; ok { + if _, ok := keys[strings.ToLower(s[0:1])]; ok { return nil } return errors.New("unknown") } textInput.Focus() return choiceInputModel{ - textInput: textInput, - keys: keys, - defaultValue: defaultValue, + textInput: textInput, + keys: keys, } } @@ -56,29 +55,17 @@ func (m choiceInputModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { if keyMsg, ok := msg.(tea.KeyMsg); ok { switch keyMsg.Type { case tea.KeyCtrlC, tea.KeyEsc: + m.textInput.Blur() return m, tea.Quit - case tea.KeyEnter: - value := m.textInput.Value() - if value == "" && m.defaultValue != nil { - // Enter pressed - m.textInput.SetValue(*m.defaultValue) - m.confirmed = true - m.textInput.Blur() - return m, tea.Quit - } else if value, ok := m.keys[value]; ok { - m.textInput.SetValue(value) - m.confirmed = true - m.textInput.Blur() - return m, tea.Quit - } } } var cmd tea.Cmd m.textInput, cmd = m.textInput.Update(msg) - if _, ok := m.keys[m.textInput.Value()]; ok { - m.confirmed = true + if value, ok := m.keys[strings.ToLower(m.textInput.Value())]; ok { m.textInput.Blur() + m.textInput.SetValue(value) + m.confirmed = true return m, tea.Quit } return m, cmd @@ -86,10 +73,7 @@ func (m choiceInputModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m choiceInputModel) Value() string { value := m.textInput.Value() - if value == "" && m.defaultValue != nil { - return *m.defaultValue - } - return m.keys[value] + return strings.ToLower(value) } func (m choiceInputModel) View() string { diff --git a/internal/tui/tui.go b/internal/tui/tui.go index 094fabb..53235e3 100644 --- a/internal/tui/tui.go +++ b/internal/tui/tui.go @@ -59,8 +59,8 @@ func BoolPrompt(prompt string) bool { return false } -func ChoicePrompt(prompt string, choices []string, defaultValue *string) (string, error) { - model := newChoiceInputModel(prompt, choices, defaultValue) +func ChoicePrompt(prompt string, choices []string) (string, error) { + model := newChoiceInputModel(prompt, choices) result, err := tea.NewProgram(model).Run() if err != nil { return "", err