rivo / tview

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

Unable to update adjacent view for the first item in the list #930

Open peku-jamf opened 11 months ago

peku-jamf commented 11 months ago

Hello, can you please advice how can I update textView on the right side of the screen in case there is only one item in the list or there is no user interaction (no key pressed), thus no SetSelectedFunc() is triggered?

I assumed setting current item to the very first one would trigger callback, but it doesn't help.

Thanks

package main

import (
    "fmt"

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

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

    textView := tview.NewTextView().
        SetText("Item details").
        SetTextAlign(tview.AlignLeft).
        SetDynamicColors(true)

    list := tview.NewList().ShowSecondaryText(false).
        SetSelectedStyle(tcell.StyleDefault.Background(tcell.ColorDarkSlateGray).Foreground(tcell.ColorWhite)).
        AddItem("List item 1", "Some explanatory text", 'a', nil)

    list.SetSelectedFunc(func(i int, _ string, _ string, _ rune) {
        textView.SetText(fmt.Sprintf("Selected item %d", i))
    })

    list.SetChangedFunc(func(i int, _ string, _ string, _ rune) {
        textView.SetText(fmt.Sprintf("hovered item %d", i))
    })

    f := tview.NewFlex().
        SetDirection(tview.FlexColumn).
        AddItem(list, 0, 1, true).
        AddItem(textView, 0, 2, false)
    f.SetBorder(true)

    if err := app.SetRoot(f, true).SetFocus(list).Run(); err != nil {
        panic(err)
    }
}
peku-jamf commented 11 months ago

I have workarounded the problem by adding an artificial list item, pointing to it, then returning back to first item and removing artificial one. But there must be a better way..

digitallyserviced commented 10 months ago

@peku-jamf Easy as fuck my good man.. :grinning:

Add your items after setting the handlers...

package main

import (
    "fmt"

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

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

    textView := tview.NewTextView().
        SetText("Item details").
        SetTextAlign(tview.AlignLeft).
        SetDynamicColors(true)

    list := tview.NewList().ShowSecondaryText(false).
        SetSelectedStyle(tcell.StyleDefault.Background(tcell.ColorDarkSlateGray).Foreground(tcell.ColorWhite))

    list.SetSelectedFunc(func(i int, _ string, _ string, _ rune) {
        textView.SetText(fmt.Sprintf("Selected item %d", i))
    })

    list.SetChangedFunc(func(i int, _ string, _ string, _ rune) {
        textView.SetText(fmt.Sprintf("hovered item %d", i))
    })
        list.
        AddItem("List item 1", "Some explanatory text", 'a', nil)

    f := tview.NewFlex().
        SetDirection(tview.FlexColumn).
        AddItem(list, 0, 1, true).
        AddItem(textView, 0, 2, false)
    f.SetBorder(true)

    if err := app.SetRoot(f, true).SetFocus(list).Run(); err != nil {
        panic(err)
    }
}