Skip to content

change formatting of placeholders in input prompts #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/cmd/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
23 changes: 12 additions & 11 deletions internal/tui/boolInputModel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tui

import (
"errors"
"strings"

"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
Expand All @@ -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()
Expand All @@ -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
}
Expand All @@ -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
}

Expand Down
54 changes: 19 additions & 35 deletions internal/tui/choiceInputModel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand All @@ -56,40 +55,25 @@ 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
}

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 {
Expand Down
4 changes: 2 additions & 2 deletions internal/tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading