From 6751475a748494e44c1ce709172612a4d47d883a Mon Sep 17 00:00:00 2001 From: Oliver <480930+rivo@users.noreply.github.com> Date: Wed, 27 Dec 2017 16:05:00 +0100 Subject: [PATCH] Added a short demo for each primitive. --- demos/basic.go | 144 --------------------------------------- demos/box/main.go | 12 ++++ demos/button/main.go | 14 ++++ demos/checkbox/main.go | 11 +++ demos/dropdown/main.go | 13 ++++ demos/flex/main.go | 19 ++++++ demos/form/main.go | 22 ++++++ demos/frame/main.go | 21 ++++++ demos/inputfield/main.go | 20 ++++++ demos/list/main.go | 20 ++++++ demos/modal/main.go | 20 ++++++ demos/pages/main.go | 33 +++++++++ demos/table/main.go | 46 +++++++++++++ demos/textview/main.go | 67 ++++++++++++++++++ 14 files changed, 318 insertions(+), 144 deletions(-) delete mode 100644 demos/basic.go create mode 100644 demos/box/main.go create mode 100644 demos/button/main.go create mode 100644 demos/checkbox/main.go create mode 100644 demos/dropdown/main.go create mode 100644 demos/flex/main.go create mode 100644 demos/form/main.go create mode 100644 demos/frame/main.go create mode 100644 demos/inputfield/main.go create mode 100644 demos/list/main.go create mode 100644 demos/modal/main.go create mode 100644 demos/pages/main.go create mode 100644 demos/table/main.go create mode 100644 demos/textview/main.go diff --git a/demos/basic.go b/demos/basic.go deleted file mode 100644 index de154ac..0000000 --- a/demos/basic.go +++ /dev/null @@ -1,144 +0,0 @@ -package main - -import ( - "fmt" - "log" - "os" - "strings" - - "github.com/gdamore/tcell" - "github.com/rivo/tview" -) - -func main() { - l, _ := os.Create("/tmp/tview.log") - defer l.Close() - log.SetOutput(l) - - app := tview.NewApplication() - pages := tview.NewPages() - list := tview.NewList() - - app.SetKeyCapture(tcell.KeyCtrlQ, 0, func(p tview.Primitive) bool { - app.Stop() - return false - }) - - form := tview.NewForm(). - AddInputField("First name", "", 20, nil). - AddInputField("Last name", "", 20, nil). - AddInputField("Age", "", 4, nil). - AddDropDown("Select", []string{"One", "Two", "Three"}, 1, func(text string, index int) { - if text == "Three" { - app.Stop() - } - }). - AddCheckbox("Check", false, nil). - AddButton("Save", func() { - previous := app.GetFocus() - modal := tview.NewModal(). - SetText("Would you really like to save this customer to the database?"). - AddButtons([]string{"Save", "Cancel"}). - SetDoneFunc(func(buttonIndex int, buttonLabel string) { - pages.RemovePage("confirm") - app.SetFocus(previous) - app.Draw() - }) - pages.AddPage("confirm", modal, true) - app.SetFocus(modal) - app.Draw() - }). - AddButton("Cancel", nil). - AddButton("Go to list", func() { app.SetFocus(list) }). - SetCancelFunc(func() { - app.Stop() - }) - form.SetTitle("Customer").SetBorder(true) - - textView := tview.NewTextView() - textView.SetWrap(true). - SetDynamicColors(true). - SetScrollable(true). - SetRegions(true). - SetChangedFunc(func() { app.Draw() }). - SetDoneFunc(func(key tcell.Key) { textView.ScrollToHighlight(); app.SetFocus(list) }) - textView.SetBorder(true).SetTitle("Text view") - go func() { - for i := 0; i < 200; i++ { - fmt.Fprintf(textView, "[\"%d\"]%d\n", i, i) - } - textView.Highlight("199") - }() - - frame := tview.NewFrame(list).AddText("Choose!", true, tview.AlignCenter, tcell.ColorRed) - frame.SetBorder(true) - - table := tview.NewTable().SetBorders(true).SetSeparator(tview.GraphicsVertBar).SetSelectable(false, false) - lorem := strings.Split("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.", " ") - cols, rows := 20, 120 - word := 0 - for r := 0; r < rows; r++ { - for c := 0; c < cols; c++ { - color := tcell.ColorWhite - if c < 2 || r < 2 { - color = tcell.ColorYellow - } - table.SetCell(r, c, &tview.TableCell{ - Text: lorem[word], - Color: color, - Align: tview.AlignCenter, - }) - word++ - if word >= len(lorem) { - word = 0 - } - } - } - table.SetSelected(0, 0).SetFixed(2, 2).SetDoneFunc(func(key tcell.Key) { - if key == tcell.KeyEscape { - app.SetFocus(list) - } - if key == tcell.KeyEnter { - table.SetSelectable(true, true) - } - }).SetSelectedFunc(func(row int, column int) { - cell := table.GetCell(row, column) - cell.Color = tcell.ColorRed - table.SetSelectable(false, false) - }) - table.SetBorder(true).SetBorderPadding(1, 1, 1, 1) - - list.AddItem("Edit a form", "You can do whatever you want", 'e', func() { app.SetFocus(form) }). - AddItem("Navigate text", "Try all the navigations", 't', func() { app.SetFocus(textView) }). - AddItem("Navigate table", "Rows and columns", 'a', func() { app.SetFocus(table) }). - AddItem("Quit the program", "Do it!", 0, func() { app.Stop() }) - - flex := tview.NewFlex(). - AddItem(form, 0, 1). - AddItem(tview.NewFlex(). - SetDirection(tview.FlexRow). - AddItem(frame, 0, 1). - AddItem(textView, 0, 1), 0, 1). - AddItem(table, 0, 1). - AddItem(tview.NewBox().SetBorder(true).SetTitle("Fifth"), 20, 1) - - inputField := tview.NewInputField(). - SetLabel("Type something: "). - SetFieldLength(10). - SetAcceptanceFunc(tview.InputFieldFloat) - inputField.SetBorder(true).SetTitle("Type!") - - final := tview.NewFlex(). - SetFullScreen(true). - SetDirection(tview.FlexRow). - AddItem(flex, 0, 1). - AddItem(inputField, 3, 1) - - pages.AddPage("flex", final, true) - - app.SetRoot(pages, true).SetFocus(list) - - if err := app.Run(); err != nil { - panic(err) - } -} diff --git a/demos/box/main.go b/demos/box/main.go new file mode 100644 index 0000000..ec114ee --- /dev/null +++ b/demos/box/main.go @@ -0,0 +1,12 @@ +package main + +import "github.com/rivo/tview" + +func main() { + box := tview.NewBox(). + SetBorder(true). + SetTitle("Box Demo") + if err := tview.NewApplication().SetRoot(box, true).Run(); err != nil { + panic(err) + } +} diff --git a/demos/button/main.go b/demos/button/main.go new file mode 100644 index 0000000..ba525e5 --- /dev/null +++ b/demos/button/main.go @@ -0,0 +1,14 @@ +package main + +import "github.com/rivo/tview" + +func main() { + app := tview.NewApplication() + button := tview.NewButton("Hit Enter to close").SetSelectedFunc(func() { + app.Stop() + }) + button.SetBorder(true).SetRect(0, 0, 22, 3) + if err := app.SetRoot(button, false).SetFocus(button).Run(); err != nil { + panic(err) + } +} diff --git a/demos/checkbox/main.go b/demos/checkbox/main.go new file mode 100644 index 0000000..e6c4022 --- /dev/null +++ b/demos/checkbox/main.go @@ -0,0 +1,11 @@ +package main + +import "github.com/rivo/tview" + +func main() { + app := tview.NewApplication() + checkbox := tview.NewCheckbox().SetLabel("Hit Enter to check box: ") + if err := app.SetRoot(checkbox, true).SetFocus(checkbox).Run(); err != nil { + panic(err) + } +} diff --git a/demos/dropdown/main.go b/demos/dropdown/main.go new file mode 100644 index 0000000..6ca1d67 --- /dev/null +++ b/demos/dropdown/main.go @@ -0,0 +1,13 @@ +package main + +import "github.com/rivo/tview" + +func main() { + app := tview.NewApplication() + dropdown := tview.NewDropDown(). + SetLabel("Select an option (hit Enter): "). + SetOptions([]string{"First", "Second", "Third", "Fourth", "Fifth"}, nil) + if err := app.SetRoot(dropdown, true).SetFocus(dropdown).Run(); err != nil { + panic(err) + } +} diff --git a/demos/flex/main.go b/demos/flex/main.go new file mode 100644 index 0000000..3450f37 --- /dev/null +++ b/demos/flex/main.go @@ -0,0 +1,19 @@ +package main + +import ( + "github.com/rivo/tview" +) + +func main() { + app := tview.NewApplication() + flex := tview.NewFlex(). + AddItem(tview.NewBox().SetBorder(true).SetTitle("Left (1/2 x width of Top)"), 0, 1). + AddItem(tview.NewFlex().SetDirection(tview.FlexRow). + AddItem(tview.NewBox().SetBorder(true).SetTitle("Top"), 0, 1). + AddItem(tview.NewBox().SetBorder(true).SetTitle("Middle (3 x height of Top)"), 0, 3). + AddItem(tview.NewBox().SetBorder(true).SetTitle("Bottom (5 rows)"), 5, 1), 0, 2). + AddItem(tview.NewBox().SetBorder(true).SetTitle("Right (20 cols)"), 20, 1) + if err := app.SetRoot(flex, true).SetFocus(flex).Run(); err != nil { + panic(err) + } +} diff --git a/demos/form/main.go b/demos/form/main.go new file mode 100644 index 0000000..6bbe73e --- /dev/null +++ b/demos/form/main.go @@ -0,0 +1,22 @@ +package main + +import ( + "github.com/rivo/tview" +) + +func main() { + app := tview.NewApplication() + form := tview.NewForm(). + AddDropDown("Title", []string{"Mr.", "Ms.", "Mrs.", "Dr.", "Prof."}, 0, nil). + AddInputField("First name", "", 20, nil). + AddInputField("Last name", "", 20, nil). + AddCheckbox("Age 18+", false, nil). + AddButton("Save", nil). + AddButton("Quit", func() { + app.Stop() + }) + form.SetBorder(true).SetTitle("Enter some data").SetTitleAlign(tview.AlignLeft) + if err := app.SetRoot(form, true).SetFocus(form).Run(); err != nil { + panic(err) + } +} diff --git a/demos/frame/main.go b/demos/frame/main.go new file mode 100644 index 0000000..021f292 --- /dev/null +++ b/demos/frame/main.go @@ -0,0 +1,21 @@ +package main + +import ( + "github.com/gdamore/tcell" + "github.com/rivo/tview" +) + +func main() { + app := tview.NewApplication() + frame := tview.NewFrame(tview.NewBox().SetBackgroundColor(tcell.ColorBlue)). + SetBorders(2, 2, 2, 2, 4, 4). + AddText("Header left", true, tview.AlignLeft, tcell.ColorWhite). + AddText("Header middle", true, tview.AlignCenter, tcell.ColorWhite). + AddText("Header right", true, tview.AlignRight, tcell.ColorWhite). + AddText("Header second middle", true, tview.AlignCenter, tcell.ColorRed). + AddText("Footer middle", false, tview.AlignCenter, tcell.ColorGreen). + AddText("Footer second middle", false, tview.AlignCenter, tcell.ColorGreen) + if err := app.SetRoot(frame, true).SetFocus(frame).Run(); err != nil { + panic(err) + } +} diff --git a/demos/inputfield/main.go b/demos/inputfield/main.go new file mode 100644 index 0000000..721d68b --- /dev/null +++ b/demos/inputfield/main.go @@ -0,0 +1,20 @@ +package main + +import ( + "github.com/gdamore/tcell" + "github.com/rivo/tview" +) + +func main() { + app := tview.NewApplication() + inputField := tview.NewInputField(). + SetLabel("Enter a number: "). + SetFieldLength(10). + SetAcceptanceFunc(tview.InputFieldInteger). + SetDoneFunc(func(key tcell.Key) { + app.Stop() + }) + if err := app.SetRoot(inputField, true).SetFocus(inputField).Run(); err != nil { + panic(err) + } +} diff --git a/demos/list/main.go b/demos/list/main.go new file mode 100644 index 0000000..a05e56c --- /dev/null +++ b/demos/list/main.go @@ -0,0 +1,20 @@ +package main + +import ( + "github.com/rivo/tview" +) + +func main() { + app := tview.NewApplication() + list := tview.NewList(). + AddItem("List item 1", "Some explanatory text", 'a', nil). + AddItem("List item 2", "Some explanatory text", 'b', nil). + AddItem("List item 3", "Some explanatory text", 'c', nil). + AddItem("List item 4", "Some explanatory text", 'd', nil). + AddItem("Quit", "Press to exit", 'q', func() { + app.Stop() + }) + if err := app.SetRoot(list, true).SetFocus(list).Run(); err != nil { + panic(err) + } +} diff --git a/demos/modal/main.go b/demos/modal/main.go new file mode 100644 index 0000000..03c5bd5 --- /dev/null +++ b/demos/modal/main.go @@ -0,0 +1,20 @@ +package main + +import ( + "github.com/rivo/tview" +) + +func main() { + app := tview.NewApplication() + modal := tview.NewModal(). + SetText("Do you want to quit the application?"). + AddButtons([]string{"Quit", "Cancel"}). + SetDoneFunc(func(buttonIndex int, buttonLabel string) { + if buttonLabel == "Quit" { + app.Stop() + } + }) + if err := app.SetRoot(modal, false).SetFocus(modal).Run(); err != nil { + panic(err) + } +} diff --git a/demos/pages/main.go b/demos/pages/main.go new file mode 100644 index 0000000..d1f6c1d --- /dev/null +++ b/demos/pages/main.go @@ -0,0 +1,33 @@ +package main + +import ( + "fmt" + + "github.com/rivo/tview" +) + +const pageCount = 5 + +func main() { + app := tview.NewApplication() + pages := tview.NewPages() + for page := 0; page < pageCount; page++ { + func(page int) { + pages.AddPage(fmt.Sprintf("page-%d", page), + tview.NewModal(). + SetText(fmt.Sprintf("This is page %d. Choose where to go next.", page+1)). + AddButtons([]string{"Next", "Quit"}). + SetDoneFunc(func(buttonIndex int, buttonLabel string) { + if buttonIndex == 0 { + pages.SwitchToPage(fmt.Sprintf("page-%d", (page+1)%pageCount)) + } else { + app.Stop() + } + }), + page == 0) + }(page) + } + if err := app.SetRoot(pages, true).SetFocus(pages).Run(); err != nil { + panic(err) + } +} diff --git a/demos/table/main.go b/demos/table/main.go new file mode 100644 index 0000000..1b9b972 --- /dev/null +++ b/demos/table/main.go @@ -0,0 +1,46 @@ +package main + +import ( + "strings" + + "github.com/gdamore/tcell" + "github.com/rivo/tview" +) + +func main() { + app := tview.NewApplication() + table := tview.NewTable(). + SetBorders(true) + lorem := strings.Split("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.", " ") + cols, rows := 10, 40 + word := 0 + for r := 0; r < rows; r++ { + for c := 0; c < cols; c++ { + color := tcell.ColorWhite + if c < 1 || r < 1 { + color = tcell.ColorYellow + } + table.SetCell(r, c, &tview.TableCell{ + Text: lorem[word], + Color: color, + Align: tview.AlignCenter, + }) + word = (word + 1) % len(lorem) + } + } + table.SetSelected(0, 0).SetFixed(1, 1).SetDoneFunc(func(key tcell.Key) { + if key == tcell.KeyEscape { + app.Stop() + } + if key == tcell.KeyEnter { + table.SetSelectable(true, true) + } + }).SetSelectedFunc(func(row int, column int) { + cell := table.GetCell(row, column) + cell.Color = tcell.ColorRed + table.SetSelectable(false, false) + }) + if err := app.SetRoot(table, true).SetFocus(table).Run(); err != nil { + panic(err) + } +} diff --git a/demos/textview/main.go b/demos/textview/main.go new file mode 100644 index 0000000..e4bc2a2 --- /dev/null +++ b/demos/textview/main.go @@ -0,0 +1,67 @@ +package main + +import ( + "fmt" + "strconv" + "strings" + "time" + + "github.com/gdamore/tcell" + "github.com/rivo/tview" +) + +const corporate = `Leverage agile frameworks to provide a robust synopsis for high level overviews. Iterative approaches to corporate strategy foster collaborative thinking to further the overall value proposition. Organically grow the holistic world view of disruptive innovation via workplace diversity and empowerment. + +Bring to the table win-win survival strategies to ensure proactive domination. At the end of the day, going forward, a new normal that has evolved from generation X is on the runway heading towards a streamlined cloud solution. User generated content in real-time will have multiple touchpoints for offshoring. + +Capitalize on low hanging fruit to identify a ballpark value added activity to beta test. Override the digital divide with additional clickthroughs from DevOps. Nanotechnology immersion along the information highway will close the loop on focusing solely on the bottom line. + +[yellow]Press Enter, then Tab/Backtab for word selections` + +func main() { + app := tview.NewApplication() + textView := tview.NewTextView(). + SetDynamicColors(true). + SetRegions(true). + SetChangedFunc(func() { + app.Draw() + }) + numSelections := 0 + go func() { + for _, word := range strings.Split(corporate, " ") { + if word == "the" { + word = "[red]the[white]" + } + if word == "to" { + word = fmt.Sprintf(`["%d"]to[""]`, numSelections) + numSelections++ + } + fmt.Fprintf(textView, "%s ", word) + time.Sleep(200 * time.Millisecond) + } + }() + textView.SetDoneFunc(func(key tcell.Key) { + currentSelection := textView.GetHighlights() + if key == tcell.KeyEnter { + if len(currentSelection) > 0 { + textView.Highlight() + } else { + textView.Highlight("0").ScrollToHighlight() + } + } else if len(currentSelection) > 0 { + index, _ := strconv.Atoi(currentSelection[0]) + if key == tcell.KeyTab { + index = (index + 1) % numSelections + } else if key == tcell.KeyBacktab { + index = (index - 1 + numSelections) % numSelections + } else { + return + } + textView.Highlight(strconv.Itoa(index)).ScrollToHighlight() + } + }) + textView.SetBorder(true) + if err := app.SetRoot(textView, true).SetFocus(textView).Run(); err != nil { + panic(err) + } +}