It's not at all obvious to me how to get the value the user inputs for the Name input field, in below simple snippet.
package main
import (
"fmt"
"github.com/rivo/tview"
)
func main() {
app := tview.NewApplication()
form := tview.NewForm()
form.SetTitle(" My Form ")
form.AddInputField("Name", "", 20, nil, nil)
form.AddButton("OK", func() { app.Stop() })
if err := app.SetRoot(form, true).SetFocus(form).Run(); err != nil {
panic(err)
}
fmt.Printf("%s
", form.GetFormItem(0))
}
The form has only two items, one inputfield, one button.
the part
app.SetRoot(form, true).SetFocus(form).Run()
runs the form. Klicking the button continues the execution afterwards by stopping the app-window.
The inputfield 'Name'
is put into the form first. It is accessed by form.GetFormItem(0)
(by it's index).
See https://github.com/rivo/tview/blob/master/form.go row 244ff
// GetFormItem returns the form element at the given position, starting with // index 0. Elements are referenced in the order they were added. Buttons are // not included. func (f *Form) GetFormItem(index int) FormItem { return f.items[index]