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