Skip to content
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

feat(textinput): allow textinput to show suggestions with empty input #695

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
22 changes: 13 additions & 9 deletions textinput/textinput.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ type Model struct {
// Should the input suggest to complete
ShowSuggestions bool

// Whether we should allow suggestions when input is empty
AllowSuggestionsForEmptyInput bool

// suggestions is a list of suggestions that may be used to complete the
// input.
suggestions [][]rune
Expand All @@ -157,14 +160,15 @@ type Model struct {
// New creates a new model with default settings.
func New() Model {
return Model{
Prompt: "> ",
EchoCharacter: '*',
CharLimit: 0,
PlaceholderStyle: lipgloss.NewStyle().Foreground(lipgloss.Color("240")),
ShowSuggestions: false,
CompletionStyle: lipgloss.NewStyle().Foreground(lipgloss.Color("240")),
Cursor: cursor.New(),
KeyMap: DefaultKeyMap,
Prompt: "> ",
EchoCharacter: '*',
CharLimit: 0,
PlaceholderStyle: lipgloss.NewStyle().Foreground(lipgloss.Color("240")),
ShowSuggestions: false,
AllowSuggestionsForEmptyInput: false,
CompletionStyle: lipgloss.NewStyle().Foreground(lipgloss.Color("240")),
Cursor: cursor.New(),
KeyMap: DefaultKeyMap,

suggestions: [][]rune{},
value: nil,
Expand Down Expand Up @@ -859,7 +863,7 @@ func (m *Model) updateSuggestions() {
return
}

if len(m.value) <= 0 || len(m.suggestions) <= 0 {
if (len(m.value) <= 0 && !m.AllowSuggestionsForEmptyInput) || len(m.suggestions) <= 0 {
m.matchedSuggestions = [][]rune{}
return
}
Expand Down
28 changes: 28 additions & 0 deletions textinput/textinput_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,37 @@ func Test_CurrentSuggestion(t *testing.T) {
textinput.nextSuggestion()
suggestion = textinput.CurrentSuggestion()
expected = "test2"
if suggestion != expected {
t.Fatalf("Error: expected second suggestion but was %s", suggestion)
}
}

func Test_SuggestionWithEmptyInput(t *testing.T) {
textinput := New()
textinput.ShowSuggestions = true
textinput.AllowSuggestionsForEmptyInput = true

suggestion := textinput.CurrentSuggestion()
expected := ""
if suggestion != expected {
t.Fatalf("Error: expected no current suggestion but was %s", suggestion)
}

textinput.SetSuggestions([]string{"test1", "test2", "test3"})
suggestion = textinput.CurrentSuggestion()
expected = "test1"
if suggestion != expected {
t.Fatalf("Error: expected first suggestion but was %s", suggestion)
}

textinput.SetValue("test")
textinput.updateSuggestions()
textinput.nextSuggestion()
suggestion = textinput.CurrentSuggestion()
expected = "test2"
if suggestion != expected {
t.Fatalf("Error: expected second suggestion but was %s", suggestion)
}
}

func Test_SlicingOutsideCap(t *testing.T) {
Expand Down