Skip to content

Commit

Permalink
feat: allow textinput to show suggestions with empty input
Browse files Browse the repository at this point in the history
  • Loading branch information
atinylittleshell committed Jan 1, 2025
1 parent 8624776 commit eacc729
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
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

0 comments on commit eacc729

Please sign in to comment.