mirror of
https://github.com/rivo/tview.git
synced 2024-11-07 03:20:39 +00:00
Added a short demo for each primitive.
This commit is contained in:
parent
3670319cd6
commit
6751475a74
144
demos/basic.go
144
demos/basic.go
@ -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)
|
||||
}
|
||||
}
|
12
demos/box/main.go
Normal file
12
demos/box/main.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
14
demos/button/main.go
Normal file
14
demos/button/main.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
11
demos/checkbox/main.go
Normal file
11
demos/checkbox/main.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
13
demos/dropdown/main.go
Normal file
13
demos/dropdown/main.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
19
demos/flex/main.go
Normal file
19
demos/flex/main.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
22
demos/form/main.go
Normal file
22
demos/form/main.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
21
demos/frame/main.go
Normal file
21
demos/frame/main.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
20
demos/inputfield/main.go
Normal file
20
demos/inputfield/main.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
20
demos/list/main.go
Normal file
20
demos/list/main.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
20
demos/modal/main.go
Normal file
20
demos/modal/main.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
33
demos/pages/main.go
Normal file
33
demos/pages/main.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
46
demos/table/main.go
Normal file
46
demos/table/main.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
67
demos/textview/main.go
Normal file
67
demos/textview/main.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user