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

[textinput] Calling CurrentSuggestion() when there is no suggestion causes a panic #570

Open
johnknott opened this issue Jul 28, 2024 · 0 comments

Comments

@johnknott
Copy link

Describe the bug

Calling CurrentSuggestion() when there is no suggestion causes a panic, and unless I am being daft, there is no public way to check if there is an available suggestion. I had to get round it with reflection to access a private field (matchedSuggestions).

Setup

  • OS: Arch Linux
  • Shell: fish
  • Terminal Emulator: foot
  • Terminal Multiplexer: none
  • Locale: en_GB.UTF-8

To Reproduce
Steps to reproduce the behavior:

  1. go run <source_file.go>
  2. press <tab> when there are no suggestions
  3. panic

Source Code

package main

import (
	"fmt"

	"github.com/charmbracelet/bubbles/textinput"
	tea "github.com/charmbracelet/bubbletea"
)

type model struct {
	textInput textinput.Model
}

func initialModel() model {
	ti := textinput.New()
	ti.Placeholder = "Start typing..."
	ti.ShowSuggestions = true
	ti.Focus()
	ti.SetSuggestions([]string{"apple", "banana", "cherry"})
	return model{textInput: ti}
}

func (m model) Init() tea.Cmd { return textinput.Blink }

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	var cmd tea.Cmd
	switch msg := msg.(type) {
	case tea.KeyMsg:
		if msg.Type == tea.KeyTab {
                        suggestion := m.textInput.CurrentSuggestion() // Panic here
			m.textInput.SetValue(suggestion)
		} else if msg.Type == tea.KeyCtrlC || msg.Type == tea.KeyEsc {
			return m, tea.Quit
		}
	}
	m.textInput, cmd = m.textInput.Update(msg)
	return m, cmd
}

func (m model) View() string {
	return fmt.Sprintf("Type a fruit:\n\n%s\n\n(esc to quit)\n", m.textInput.View())
}

func main() {
	if _, err := tea.NewProgram(initialModel()).Run(); err != nil {
		fmt.Printf("Error: %v", err)
	}
}

Expected behavior

CurrentSuggestion() to return ""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant