Skip to content

Commit c794e2f

Browse files
authored
Merge pull request #10 from umlx5h/fix-prompt
change formatting of placeholders in input prompts
2 parents 71d593f + 4cc9069 commit c794e2f

File tree

5 files changed

+35
-50
lines changed

5 files changed

+35
-50
lines changed

internal/cmd/put.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ func putCmdRun(args []string, opts putOptions) error {
187187
if opts.prompt {
188188
prompt := fmt.Sprintf("Do you trash %s %q? ", posix.FileType(st), arg)
189189
choices := []string{"yes", "no", "all-yes", "quit"}
190-
selected, err := tui.ChoicePrompt(prompt, choices, nil)
190+
selected, err := tui.ChoicePrompt(prompt, choices)
191191
if err != nil {
192192
// canceled
193193
return err

internal/cmd/restore.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ func doRestore(files []trash.File, restoreTo string, prompt bool) error {
228228
choice = []string{"new-name", "skip", "repeat-prev", "quit"}
229229
}
230230
// TODO: Make the message easy to understand
231-
selected, err = tui.ChoicePrompt(fmt.Sprintf("Conflicted restore path %q\n\tPlease choose one of the following: ", file.OriginalPath), choice, nil)
231+
selected, err = tui.ChoicePrompt(fmt.Sprintf("Conflicted restore path %q\n\tPlease choose one of the following: ", file.OriginalPath), choice)
232232
if err != nil {
233233
return err
234234
}

internal/tui/boolInputModel.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tui
22

33
import (
44
"errors"
5+
"strings"
56

67
"github.com/charmbracelet/bubbles/textinput"
78
tea "github.com/charmbracelet/bubbletea"
@@ -12,25 +13,25 @@ type boolInputModel struct {
1213
confirmed bool
1314
}
1415

15-
func yesno(s string) (bool, error) {
16+
func yesno(s string) (bool, string, error) {
1617
if s == "" {
17-
return false, errors.New("empty")
18+
return false, "", errors.New("empty")
1819
}
19-
switch s[0:1] {
20+
switch strings.ToLower(s[0:1]) {
2021
case "y":
21-
return true, nil
22+
return true, "Yes", nil
2223
case "n":
23-
return false, nil
24+
return false, "No", nil
2425
}
25-
return false, errors.New("unknown")
26+
return false, "", errors.New("unknown")
2627
}
2728

2829
func newBoolInputModel(prompt string) boolInputModel {
2930
textInput := textinput.New()
3031
textInput.Prompt = prompt
31-
textInput.Placeholder = "yes/no"
32+
textInput.Placeholder = "(Yes/No)"
3233
textInput.Validate = func(value string) error {
33-
_, err := yesno(value)
34+
_, _, err := yesno(value)
3435
return err
3536
}
3637
textInput.Focus()
@@ -57,8 +58,9 @@ func (m boolInputModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
5758
}
5859
var cmd tea.Cmd
5960
m.textInput, cmd = m.textInput.Update(msg)
60-
if _, err := yesno(m.textInput.Value()); err == nil {
61+
if _, value, err := yesno(m.textInput.Value()); err == nil {
6162
m.textInput.Blur()
63+
m.textInput.SetValue(value)
6264
m.confirmed = true
6365
return m, tea.Quit
6466
}
@@ -67,8 +69,7 @@ func (m boolInputModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
6769

6870
func (m boolInputModel) Value() bool {
6971
valueStr := m.textInput.Value()
70-
71-
v, _ := yesno(valueStr)
72+
v, _, _ := yesno(valueStr)
7273
return v
7374
}
7475

internal/tui/choiceInputModel.go

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,37 @@ import (
99
)
1010

1111
type choiceInputModel struct {
12-
textInput textinput.Model
13-
keys map[string]string
14-
defaultValue *string
15-
confirmed bool
12+
textInput textinput.Model
13+
keys map[string]string
14+
confirmed bool
1615
}
1716

18-
func newChoiceInputModel(prompt string, choices []string, defaultValue *string) choiceInputModel {
17+
func newChoiceInputModel(prompt string, choices []string) choiceInputModel {
1918
textInput := textinput.New()
2019
textInput.Prompt = prompt
21-
textInput.Placeholder = strings.Join(choices, "/")
22-
if defaultValue != nil {
23-
textInput.Placeholder += ", default " + *defaultValue
20+
21+
for i := range choices {
22+
choices[i] = strings.ToUpper(choices[i][:1]) + choices[i][1:]
2423
}
2524

25+
textInput.Placeholder = "(" + strings.Join(choices, "/") + ")"
2626
keys := make(map[string]string)
2727
for _, choice := range choices {
28-
keys[choice[0:1]] = choice
28+
keys[strings.ToLower(choice[0:1])] = choice
2929
}
3030
textInput.Validate = func(s string) error {
31-
if s == "" && defaultValue != nil {
32-
return nil
31+
if s == "" {
32+
return errors.New("empty")
3333
}
34-
if _, ok := keys[s]; ok {
34+
if _, ok := keys[strings.ToLower(s[0:1])]; ok {
3535
return nil
3636
}
3737
return errors.New("unknown")
3838
}
3939
textInput.Focus()
4040
return choiceInputModel{
41-
textInput: textInput,
42-
keys: keys,
43-
defaultValue: defaultValue,
41+
textInput: textInput,
42+
keys: keys,
4443
}
4544
}
4645

@@ -56,40 +55,25 @@ func (m choiceInputModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
5655
if keyMsg, ok := msg.(tea.KeyMsg); ok {
5756
switch keyMsg.Type {
5857
case tea.KeyCtrlC, tea.KeyEsc:
58+
m.textInput.Blur()
5959
return m, tea.Quit
60-
case tea.KeyEnter:
61-
value := m.textInput.Value()
62-
if value == "" && m.defaultValue != nil {
63-
// Enter pressed
64-
m.textInput.SetValue(*m.defaultValue)
65-
m.confirmed = true
66-
m.textInput.Blur()
67-
return m, tea.Quit
68-
} else if value, ok := m.keys[value]; ok {
69-
m.textInput.SetValue(value)
70-
m.confirmed = true
71-
m.textInput.Blur()
72-
return m, tea.Quit
73-
}
7460
}
7561
}
7662

7763
var cmd tea.Cmd
7864
m.textInput, cmd = m.textInput.Update(msg)
79-
if _, ok := m.keys[m.textInput.Value()]; ok {
80-
m.confirmed = true
65+
if value, ok := m.keys[strings.ToLower(m.textInput.Value())]; ok {
8166
m.textInput.Blur()
67+
m.textInput.SetValue(value)
68+
m.confirmed = true
8269
return m, tea.Quit
8370
}
8471
return m, cmd
8572
}
8673

8774
func (m choiceInputModel) Value() string {
8875
value := m.textInput.Value()
89-
if value == "" && m.defaultValue != nil {
90-
return *m.defaultValue
91-
}
92-
return m.keys[value]
76+
return strings.ToLower(value)
9377
}
9478

9579
func (m choiceInputModel) View() string {

internal/tui/tui.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ func BoolPrompt(prompt string) bool {
5959
return false
6060
}
6161

62-
func ChoicePrompt(prompt string, choices []string, defaultValue *string) (string, error) {
63-
model := newChoiceInputModel(prompt, choices, defaultValue)
62+
func ChoicePrompt(prompt string, choices []string) (string, error) {
63+
model := newChoiceInputModel(prompt, choices)
6464
result, err := tea.NewProgram(model).Run()
6565
if err != nil {
6666
return "", err

0 commit comments

Comments
 (0)