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