Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Commit

Permalink
(fix): suggestions can be dismissed
Browse files Browse the repository at this point in the history
  • Loading branch information
lucassabreu committed Jun 29, 2021
1 parent b7ee77a commit 2bbbbea
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion input.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Input struct {
Help string
Suggest func(toComplete string) []string
answer string
typedAnswer string
options []core.OptionAnswer
selectedIndex int
showingHelp bool
Expand Down Expand Up @@ -65,6 +66,9 @@ func (i *Input) onRune(config *PromptConfig) terminal.OnRuneFn {
return terminal.OnRuneFn(func(key rune, line []rune) ([]rune, bool, error) {
if i.options != nil && (key == terminal.KeyEnter || key == '\n') {
return []rune(i.answer), true, nil
} else if i.options != nil && key == terminal.KeyEscape {
i.answer = i.typedAnswer
i.options = nil
} else if key == terminal.KeyArrowUp && len(i.options) > 0 {
if i.selectedIndex == 0 {
i.selectedIndex = len(i.options) - 1
Expand All @@ -81,6 +85,7 @@ func (i *Input) onRune(config *PromptConfig) terminal.OnRuneFn {
i.answer = i.options[i.selectedIndex].Value
} else if key == terminal.KeyTab && i.Suggest != nil {
i.answer = string(line)
i.typedAnswer = i.answer
options := i.Suggest(i.answer)
i.selectedIndex = 0
if len(options) == 0 {
Expand All @@ -89,6 +94,7 @@ func (i *Input) onRune(config *PromptConfig) terminal.OnRuneFn {

i.answer = options[0]
if len(options) == 1 {
i.typedAnswer = i.answer
i.options = nil
} else {
i.options = core.OptionAnswerList(options)
Expand All @@ -101,6 +107,7 @@ func (i *Input) onRune(config *PromptConfig) terminal.OnRuneFn {
if key >= terminal.KeySpace {
i.answer += string(key)
}
i.typedAnswer = i.answer

i.options = nil
}
Expand All @@ -123,7 +130,7 @@ func (i *Input) onRune(config *PromptConfig) terminal.OnRuneFn {
err = readLineAgain
}

return []rune(i.answer), true, err
return []rune(i.typedAnswer), true, err
})
}

Expand Down

0 comments on commit 2bbbbea

Please sign in to comment.