rivo / tview

Terminal UI library with rich, interactive widgets — written in Golang
MIT License
11.13k stars 575 forks source link

Bug in TextArea Select #958

Closed eeeXun closed 7 months ago

eeeXun commented 8 months ago

Given the following code, you can press C-q to see the selection range. I want to select all text, so textArea.Select(0, len(textArea.GetText()))

package main

import (
    "github.com/gdamore/tcell/v2"
    "github.com/rivo/tview"
)

func main() {
    app := tview.NewApplication()

    textArea := tview.NewTextArea().
        SetPlaceholder("Enter text here...").
        SetSelectedStyle(tcell.StyleDefault.Background(tcell.ColorYellow).Foreground(tcell.ColorRed))
    textArea.SetTitle("Text Area").SetBorder(true)

    textArea.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
        if event.Key() == tcell.KeyCtrlQ {
            textArea.Select(0, len(textArea.GetText()))
            return nil
        }
        return event
    })

    if err := app.SetRoot(textArea,
        true).EnableMouse(true).Run(); err != nil {
        panic(err)
    }
}

image

The bug is when your text is too long to fit on a line in terminal. image

This could not be solved even I increase the value of second parameter in Select. textArea.Select(0, 10000)

naviji commented 8 months ago

I'm able to reproduce this. I'll see if I can fix it.

naviji commented 8 months ago

I see that the Ctrl-L key can also be used to select the entire text.

For some reason selecting via Ctrl-L doesn't seem to trigger the selection issue. Does that help you?

eeeXun commented 8 months ago

@naviji Hi, thanks for your help. Actually, my problem is I want to use TextArea.Replace instead of TextArea.SetText to reserve the undo history. (I want to clear the text, textArea.Replace(0, len(textArea.GetText()), "") )

When I call TextArea.Replace, it produces the similar problem as mentioned above. So I trace the code and find this function calls TextArea.Select.

That's why I report the bug in TextArea.Select

rivo commented 7 months ago

Hi everyone, thanks for reporting this bug. The latest commit should fix this.