charmbracelet / charmbracelet/bubbles

[textinput] navigating suggestions aborts program

Open
#562 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
8.9k
Forks
457
Avg merge
1d 18h
Merged PRs (30d)
5

Description

**Describe the bug**

Using the arrow keys kills the example app.

**Setup**
Please complete the following information along with version numbers, if applicable.
- OS macOS 14.5
- Shell zsh
- Terminal Emulator macOS default
- Terminal Multiplexer none
- Locale LANG=en_US.UTF-8 LC_CTYPE=UTF-8

**To Reproduce**

1. go run
2. type "f"
3. press arrow down a couple of times

**Source Code**

A simpler version of the example code

```
package main

import (
"fmt"
"log"

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

func main() {
p := tea.NewProgram(initialModel())
if _, err := p.Run(); err != nil {
log.Fatal(err)
}
}

type gotReposSuccessMsg []repo
type gotReposErrMsg error

type repo struct {
Name string
}

func getRepos() tea.Msg {

var repos []repo

repos = append(repos, repo{"foo"})
repos = append(repos, repo{"bar"})

return gotReposSuccessMsg(repos)
}

type model struct {
textInput textinput.Model
}

func initialModel() model {
ti := textinput.New()
ti.Placeholder = "repository"
ti.Prompt = "charmbracelet/"
ti.PromptStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("63"))
ti.Cursor.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("63"))
ti.Focus()
ti.CharLimit = 50
ti.Width = 20
ti.ShowSuggestions = true

return model{textInput: ti}
}

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

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyEnter, tea.KeyCtrlC, tea.KeyEsc:
return m, tea.Quit
}
case gotReposSuccessMsg:
var suggestions []string
for _, r := range msg {
suggestions = append(suggestions, r.Name)
}
m.textInput.SetSuggestions(suggestions)
}

var cmd tea.Cmd
m.textInput, cmd = m.textInput.Update(msg)
return m, cmd
}

func (m model) View() string {
return fmt.Sprintf(
"repo: %s\n",
m.textInput.View(),
)
}
```

**Expected behavior**

The arrow keys should navigate within the suggestions and certainly not exit the program.

**Screenshots**

**Additional context**

Maybe I am just holding it wrong - but then it feels like this should be more obvious.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.