charmbracelet / bubbles

TUI components for Bubble Tea 🫧
MIT License
5.62k stars 263 forks source link

Bubble tea text area does not work with large json files #671

Closed tyler-mairose-sp closed 4 days ago

tyler-mairose-sp commented 4 days ago

Describe the bug

I am attempting to create a json path evaluator project using a combination of the bubble tea text input and two text area editors like the split-editors example.

When I load a json file into the text area it does not properly produce the whole string in the text area.

Ultimately, I would like to propose this as a bubble tea example project that others can build from. I thought it was a cool use of the split editors plus a text input.

Setup Please complete the following information along with version numbers, if applicable.

To Reproduce Steps to reproduce the behavior:

  1. Download the project here: https://github.com/tyler-mairose-sp/json-path-evaluator/tree/main
  2. Run go mod tidy
  3. Run go run main.go in the terminal
  4. The json is cut off when loaded into the text area.
  5. You can tab back and forth between the json path query and the json editor.

Source Code https://github.com/tyler-mairose-sp/json-path-evaluator/tree/main

Expected behavior I would expect the json to be loaded properly into the text area. in reading the docs if textarea.charLimit is set to 0 there should be no limit.

meowgorithm commented 4 days ago

Thanks for filing this issue. I'm able to reproduce the bug.

Here's a minimal example to reproduce the behavior:

package main

import (
    "os"

    "github.com/charmbracelet/bubbles/textarea"
    tea "github.com/charmbracelet/bubbletea"
    "github.com/charmbracelet/log"
)

type model struct {
    input textarea.Model
}

func (m model) Init() tea.Cmd {
    return m.input.Focus()
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
    switch msg := msg.(type) {
    case tea.WindowSizeMsg:
        m.input.SetWidth(msg.Width)
        m.input.SetHeight(msg.Height)
    case tea.KeyMsg:
        switch msg.String() {
        case "ctrl+c", "q", "esc":
            return m, tea.Quit
        }
    }

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

func (m model) View() string {
    return m.input.View()
}

func main() {
    var data []byte
    var err error
    filepath := "textdata.json"

    data, err = os.ReadFile(filepath)
    if err != nil {
        log.Fatal(err)
    }

    t := textarea.New()
    t.CharLimit = 0
    t.SetValue(string(data))
    t.Focus()
    m := model{input: t}

    if _, err := tea.NewProgram(m, tea.WithAltScreen()).Run(); err != nil {
        log.Fatal(err)
    }
}
tyler-mairose-sp commented 4 days ago

@meowgorithm I appreciate the quick turn around. I'll be watching your PR in order to test it out 😄

meowgorithm commented 4 days ago

@meowgorithm I appreciate the quick turn around. I'll be watching your PR in order to test it out 😄

You got it! Just want to get another pair of eyes on it before merging it. There's also a change that needs to happen if you own code: PR here.

tyler-mairose-sp commented 4 days ago

Yes thank you! Just out of curiosity what does it do when you set the value first, then the charLimit?

meowgorithm commented 4 days ago

The default character limit is quite small, so if you set the value before removing the character limit the value gets truncated.

We should make a two changes to avoid this pitfall:

  1. Remove the character limit by default, or set the limit very high.
  2. Make the character limit a getter/setter as getting and setting needs to affect the value internals (but in v2, because it will incur an API change)
meowgorithm commented 4 days ago

Alright, this is fixed. We also removed the default character limit. Let us know if you encounter any more issues.

Cheers!