How to get the text value of a function using golang tview

Viewed 194

It is not clear on how to get the value of the functions used inside of a form using https://github.com/rivo/tview/.

package main

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

func main() {

    app := tview.NewApplication()
    form := tview.NewForm()
    form.SetBorder(true).SetTitle("Enter some data").SetTitleAlign(tview.AlignLeft)
    form.AddDropDown("Title", []string{"Mr.", "Ms.", "Mrs.", "Dr.", "Prof."}, 0, nil)
    form.AddInputField("Value1", "", 0, nil, nil)
    form.AddInputField("Value2", "", 0, nil, nil)
    form.AddInputField("Value3", "", 0, nil, nil)
    form.AddInputField("Value4", "", 0, nil, nil)
    form.AddButton("OK", func() { app.Stop() })
    if err := app.SetRoot(form, true).SetFocus(form).Run(); err != nil {
        panic(err)
    }
    fmt.Printf("%s\n", form.GetFormItem(0).(*tview.DropDown))
}

Is it possible to convert the output of the dropdown to text?

1 Answers

To get the selected option (both index and text), use the DropDown.GetCurrentOption() method:

i, s := form.GetFormItem(0).(*tview.DropDown).GetCurrentOption()
fmt.Printf("%d %s\n", i, s)

Example output:

2 Mrs.
Related