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

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

Open
peku-jamf opened this issue Dec 21, 2023 · 2 comments
Open

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

peku-jamf opened this issue Dec 21, 2023 · 2 comments

Comments

@peku-jamf
Copy link

peku-jamf commented Dec 21, 2023

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
Copy link
Author

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
Copy link
Contributor

digitallyserviced commented Jan 1, 2024

@peku-jamf
Easy as fuck my good man.. 😀

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)
	}
}

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

2 participants