3

I'm using tview to make my TUI application.

The app uses TextView in Flex, but I can't change the contents of that TextView.

The specific code is as follows.

package main

import (
    "fmt"
    "github.com/rivo/tview"
)

func main() {
    app := tview.NewApplication()
    textView := tview.NewTextView().
        SetRegions(true).SetDynamicColors(true).SetWordWrap(true).SetChangedFunc(func() { app.Draw() })
    flex := tview.NewFlex().
        AddItem(textView.SetBorder(true).SetTitle("title1"), 0, 1, false).
        AddItem(tview.NewFlex().SetDirection(tview.FlexRow).
            AddItem(tview.NewBox().SetBorder(true).SetTitle("title2"), 0, 1, false).
            AddItem(tview.NewBox().SetBorder(true).SetTitle("title3"), 0, 10, false), 0, 2, false)

    fmt.Fprintf(textView, "%s ", "foo")
    fmt.Fprintf(textView, "%s ", "bar")

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

}

I'm expecting TextView to show "foo bar", but I don't see it.

If you use TextView as the root as shown below, "foo bar" will be displayed as expected.

package main

import (
    "fmt"
    "github.com/rivo/tview"
)

func main() {
    app := tview.NewApplication()
    textView := tview.NewTextView().
        SetRegions(true).SetDynamicColors(true).SetWordWrap(true).SetChangedFunc(func() { app.Draw() })
    _ = tview.NewFlex().
        AddItem(textView.SetBorder(true).SetTitle("title1"), 0, 1, false).
        AddItem(tview.NewFlex().SetDirection(tview.FlexRow).
            AddItem(tview.NewBox().SetBorder(true).SetTitle("title2"), 0, 1, false).
            AddItem(tview.NewBox().SetBorder(true).SetTitle("title3"), 0, 10, false), 0, 2, false)

    fmt.Fprintf(textView, "%s ", "foo")
    fmt.Fprintf(textView, "%s ", "bar")

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

}

How can I display "foo bar" in the TextView used in Flex?

Please let me know if you know.

a.kitazawa
  • 53
  • 4
  • 2
    Move the box setting code outside of the AddItem and then pass textView to AddItem directly. e.g. `textView := tview.NewTextView()...` then `textView.SetBorder(true).SetTitle("title1")` and then `flex := tview.NewFlex().AddItem(textView, ...`. – mkopriva Mar 15 '21 at 16:49
  • 1
    Thanks for your comment. If you followed your advice, it worked as expected. Thank you very much for your help !! – a.kitazawa Mar 16 '21 at 14:31

0 Answers0