charmbracelet / bubbles

TUI components for Bubble Tea 🫧
MIT License
5.24k stars 247 forks source link

textinput: when `Width` is larger than the capacity of the placeholder, it causes a panic #531

Open MikaelFangel opened 2 months ago

MikaelFangel commented 2 months ago

Describe the bug When you increase the width above 31 and the placeholder capacity is set to 32, the program panics. This bug is causing the bug reported here https://github.com/charmbracelet/gum/issues/591 as far as I can tell.

To Reproduce Steps to reproduce the behavior:

  1. Run the code with a ti.Width of 31
  2. Run the code and see it works just fine
  3. Change the value to 32
  4. Run the code a see the out-of-bounds panic

Source Code

package main

import (
    "fmt"
    "log"

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

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

type model struct {
    textInput textinput.Model
    err       error
}

func initialModel() model {
    ti := textinput.New()
    ti.Placeholder = "作業ディレクトリを指定してください"
    ti.Width = 32

    return model{
        textInput: ti,
        err:       nil,
    }
}

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

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
    return m, nil
}

func (m model) View() string {
    return fmt.Sprintf(
        m.textInput.View(),
    ) + "\n"
}

Expected behavior The placeholder capacity should not cause out-of-bounds panics based on the ti.Width.

Screenshots p getting a default capacity of 32 20240530_11h39m01s_grim

The minWidth exceeds the capacity 20240530_11h40m06s_grim

The out-of-bounds slide happening 20240530_11h41m16s_grim