Debian Codegraph @

demos/textarea/main.go

raw symbols SCIP
1 // Demo code for the TextArea primitive.
2 package main
3
4 import (
5 "fmt"
6
7 "github.com/gdamore/tcell/v2"
8 "github.com/rivo/tview"
9 )
10
11 func main() {
12 app := tview.NewApplication()
13
14 textArea := tview.NewTextArea().
15 SetWrap(false).
16 SetPlaceholder("Enter text here...")
17 textArea.SetTitle("Text Area").SetBorder(true)
18 helpInfo := tview.NewTextView().
19 SetText(" Press F1 for help, press Ctrl-C to exit")
20 position := tview.NewTextView().
21 SetDynamicColors(true).
22 SetTextAlign(tview.AlignRight)
23 pages := tview.NewPages()
24
25 updateInfos := func() {
26 fromRow, fromColumn, toRow, toColumn := textArea.GetCursor()
27 if fromRow == toRow && fromColumn == toColumn {
28 position.SetText(fmt.Sprintf("Row: [yellow]%d[white], Column: [yellow]%d ", fromRow, fromColumn))
29 } else {
30 position.SetText(fmt.Sprintf("[red]From[white] Row: [yellow]%d[white], Column: [yellow]%d[white] - [red]To[white] Row: [yellow]%d[white], To Column: [yellow]%d ", fromRow, fromColumn, toRow, toColumn))
31 }
32 }
33
34 textArea.SetMovedFunc(updateInfos)
35 updateInfos()
36
37 mainView := tview.NewGrid().
38 SetRows(0, 1).
39 AddItem(textArea, 0, 0, 1, 2, 0, 0, true).
40 AddItem(helpInfo, 1, 0, 1, 1, 0, 0, false).
41 AddItem(position, 1, 1, 1, 1, 0, 0, false)
42
43 help1 := tview.NewTextView().
44 SetDynamicColors(true).
45 SetText(`[green]Navigation
46
47 [yellow]Left arrow[white]: Move left.
48 [yellow]Right arrow[white]: Move right.
49 [yellow]Down arrow[white]: Move down.
50 [yellow]Up arrow[white]: Move up.
51 [yellow]Ctrl-A, Home[white]: Move to the beginning of the current line.
52 [yellow]Ctrl-E, End[white]: Move to the end of the current line.
53 [yellow]Ctrl-F, page down[white]: Move down by one page.
54 [yellow]Ctrl-B, page up[white]: Move up by one page.
55 [yellow]Alt-Up arrow[white]: Scroll the page up.
56 [yellow]Alt-Down arrow[white]: Scroll the page down.
57 [yellow]Alt-Left arrow[white]: Scroll the page to the left.
58 [yellow]Alt-Right arrow[white]: Scroll the page to the right.
59 [yellow]Alt-B, Ctrl-Left arrow[white]: Move back by one word.
60 [yellow]Alt-F, Ctrl-Right arrow[white]: Move forward by one word.
61
62 [blue]Press Enter for more help, press Escape to return.`)
63 help2 := tview.NewTextView().
64 SetDynamicColors(true).
65 SetText(`[green]Editing[white]
66
67 Type to enter text.
68 [yellow]Ctrl-H, Backspace[white]: Delete the left character.
69 [yellow]Ctrl-D, Delete[white]: Delete the right character.
70 [yellow]Ctrl-K[white]: Delete until the end of the line.
71 [yellow]Ctrl-W[white]: Delete the rest of the word.
72 [yellow]Ctrl-U[white]: Delete the current line.
73
74 [blue]Press Enter for more help, press Escape to return.`)
75 help3 := tview.NewTextView().
76 SetDynamicColors(true).
77 SetText(`[green]Selecting Text[white]
78
79 Move while holding Shift or drag the mouse.
80 Double-click to select a word.
81 [yellow]Ctrl-L[white] to select entire text.
82
83 [green]Clipboard
84
85 [yellow]Ctrl-Q[white]: Copy.
86 [yellow]Ctrl-X[white]: Cut.
87 [yellow]Ctrl-V[white]: Paste.
88
89 [green]Undo
90
91 [yellow]Ctrl-Z[white]: Undo.
92 [yellow]Ctrl-Y[white]: Redo.
93
94 [blue]Press Enter for more help, press Escape to return.`)
95 help := tview.NewFrame(help1).
96 SetBorders(1, 1, 0, 0, 2, 2)
97 help.SetBorder(true).
98 SetTitle("Help").
99 SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
100 if event.Key() == tcell.KeyEscape {
101 pages.SwitchToPage("main")
102 return nil
103 } else if event.Key() == tcell.KeyEnter {
104 switch {
105 case help.GetPrimitive() == help1:
106 help.SetPrimitive(help2)
107 case help.GetPrimitive() == help2:
108 help.SetPrimitive(help3)
109 case help.GetPrimitive() == help3:
110 help.SetPrimitive(help1)
111 }
112 return nil
113 }
114 return event
115 })
116
117 pages.AddAndSwitchToPage("main", mainView, true).
118 AddPage("help", tview.NewGrid().
119 SetColumns(0, 64, 0).
120 SetRows(0, 22, 0).
121 AddItem(help, 1, 1, 1, 1, 0, 0, true), true, false)
122
123 app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
124 if event.Key() == tcell.KeyF1 {
125 pages.ShowPage("help") //TODO: Check when clicking outside help window with the mouse. Then clicking help again.
126 return nil
127 }
128 return event
129 })
130
131 if err := app.SetRoot(pages, true).EnableMouse(true).EnablePaste(true).Run(); err != nil {
132 panic(err)
133 }
134 }