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] navigating suggestions aborts program #562

Open
tcurdt opened this issue Jul 19, 2024 · 0 comments
Open

[textinput] navigating suggestions aborts program #562

tcurdt opened this issue Jul 19, 2024 · 0 comments

Comments

@tcurdt
Copy link

tcurdt commented Jul 19, 2024

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.

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