From c6a8ed78a2d0e5be72983423f3509a82458a961a Mon Sep 17 00:00:00 2001 From: Thomas Sindt Date: Sat, 28 Feb 2026 00:00:02 +0100 Subject: [PATCH] umfangreicheres Beispiel --- tvtest/tvtest.go | 80 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 78 insertions(+), 2 deletions(-) diff --git a/tvtest/tvtest.go b/tvtest/tvtest.go index 62d6885..18917a4 100644 --- a/tvtest/tvtest.go +++ b/tvtest/tvtest.go @@ -3,12 +3,88 @@ package main // https://github.com/rivo/tview?tab=readme-ov-file import ( + "github.com/gdamore/tcell/v2" "github.com/rivo/tview" ) +var app = tview.NewApplication() +var form = tview.NewForm() +var pages = tview.NewPages() +var text = tview.NewTextView(). + SetTextColor(tcell.ColorGreen). + SetText("(a) to add a new contact \n(q) to quit") + +var states = []string{"AK", "AL", "AR", "AZ", "CA", "CO", "CT", "DC", "DE", "FL", "GA", + "HI", "IA", "ID", "IL", "IN", "KS", "KY", "LA", "MA", "MD", "ME", + "MI", "MN", "MO", "MS", "MT", "NC", "ND", "NE", "NH", "NJ", "NM", + "NV", "NY", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", + "UT", "VA", "VT", "WA", "WI", "WV", "WY"} + +type Contact struct { + firstName string + lastName string + email string + phoneNumber string + state string + business bool +} + +var contacts []Contact + +func addContactForm() { + contact := Contact{} + + form.AddInputField("First Name", "", 20, nil, func(firstName string) { + contact.firstName = firstName + }) + + form.AddInputField("Last Name", "", 20, nil, func(lastName string) { + contact.lastName = lastName + }) + + form.AddInputField("Email", "", 20, nil, func(email string) { + contact.email = email + }) + + form.AddInputField("Phone", "", 20, nil, func(phone string) { + contact.phoneNumber = phone + }) + + // states is a slice of state abbreviations. Code is in the repo. + form.AddDropDown("State", states, 0, func(state string, index int) { + contact.state = state + }) + + form.AddCheckbox("Business", false, func(business bool) { + contact.business = business + }) + + form.AddButton("Save", func() { + contacts = append(contacts, contact) + }) +} + func main() { - box := tview.NewBox().SetBorder(true).SetTitle("Hello, world!") - if err := tview.NewApplication().SetRoot(box, true).Run(); err != nil { + + app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + if event.Rune() == 113 { + app.Stop() + } else if event.Rune() == 97 { + addContactForm() + pages.SwitchToPage("Add Contact") + } + return event + }) + + pages.AddPage("Menu", text, true, true) + pages.AddPage("Add Contact", form, true, false) + + form.AddButton("Save", func() { + //contacts = append(contacts, contact) + pages.SwitchToPage("Menu") + }) + + if err := app.SetRoot(pages, true).EnableMouse(true).Run(); err != nil { panic(err) } }