| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"strconv" |
| 6 |
"time" |
| 7 |
|
| 8 |
"github.com/gdamore/tcell/v2" |
| 9 |
"github.com/rivo/tview" |
| 10 |
) |
| 11 |
|
| 12 |
const textView1 = `[green]func[white] [yellow]main[white]() { |
| 13 |
app := tview.[yellow]NewApplication[white]() |
| 14 |
textView := tview.[yellow]NewTextView[white](). |
| 15 |
[yellow]SetTextColor[white](tcell.ColorYellow). |
| 16 |
[yellow]SetScrollable[white](false). |
| 17 |
[yellow]SetChangedFunc[white]([yellow]func[white]() { |
| 18 |
app.[yellow]Draw[white]() |
| 19 |
}) |
| 20 |
[green]go[white] [yellow]func[white]() { |
| 21 |
[green]var[white] n [green]int |
| 22 |
[white] [yellow]for[white] { |
| 23 |
n++ |
| 24 |
fmt.[yellow]Fprintf[white](textView, [red]"%d "[white], n) |
| 25 |
time.[yellow]Sleep[white]([red]200[white] * time.Millisecond) |
| 26 |
} |
| 27 |
}() |
| 28 |
app.[yellow]SetRoot[white](textView, true). |
| 29 |
[yellow]Run[white]() |
| 30 |
}` |
| 31 |
|
| 32 |
|
| 33 |
func TextView1(nextSlide func()) (title string, content tview.Primitive) { |
| 34 |
textView := tview.NewTextView(). |
| 35 |
SetTextColor(tcell.ColorYellow). |
| 36 |
SetScrollable(false). |
| 37 |
SetDoneFunc(func(key tcell.Key) { |
| 38 |
nextSlide() |
| 39 |
}) |
| 40 |
textView.SetChangedFunc(func() { |
| 41 |
if textView.HasFocus() { |
| 42 |
app.Draw() |
| 43 |
} |
| 44 |
}) |
| 45 |
go func() { |
| 46 |
var n int |
| 47 |
for { |
| 48 |
if textView.HasFocus() { |
| 49 |
n++ |
| 50 |
if n > 512 { |
| 51 |
n = 1 |
| 52 |
textView.SetText("") |
| 53 |
} |
| 54 |
|
| 55 |
fmt.Fprintf(textView, "%d ", n) |
| 56 |
time.Sleep(200 * time.Millisecond) |
| 57 |
} else { |
| 58 |
time.Sleep(time.Second) |
| 59 |
} |
| 60 |
} |
| 61 |
}() |
| 62 |
textView.SetBorder(true).SetTitle("TextView implements io.Writer") |
| 63 |
return "Text 1", Code(textView, 36, 13, textView1) |
| 64 |
} |
| 65 |
|
| 66 |
const textView2 = `[green]package[white] main |
| 67 |
|
| 68 |
[green]import[white] ( |
| 69 |
[red]"strconv"[white] |
| 70 |
|
| 71 |
[red]"github.com/gdamore/tcell/v2"[white] |
| 72 |
[red]"github.com/rivo/tview"[white] |
| 73 |
) |
| 74 |
|
| 75 |
[green]func[white] [yellow]main[white]() { |
| 76 |
["0"]textView[""] := tview.[yellow]NewTextView[white]() |
| 77 |
["1"]textView[""].[yellow]SetDynamicColors[white](true). |
| 78 |
[yellow]SetWrap[white](false). |
| 79 |
[yellow]SetRegions[white](true). |
| 80 |
[yellow]SetDoneFunc[white]([yellow]func[white](key tcell.Key) { |
| 81 |
highlights := ["2"]textView[""].[yellow]GetHighlights[white]() |
| 82 |
hasHighlights := [yellow]len[white](highlights) > [red]0 |
| 83 |
[yellow]switch[white] key { |
| 84 |
[yellow]case[white] tcell.KeyEnter: |
| 85 |
[yellow]if[white] hasHighlights { |
| 86 |
["3"]textView[""].[yellow]Highlight[white]() |
| 87 |
} [yellow]else[white] { |
| 88 |
["4"]textView[""].[yellow]Highlight[white]([red]"0"[white]). |
| 89 |
[yellow]ScrollToHighlight[white]() |
| 90 |
} |
| 91 |
[yellow]case[white] tcell.KeyTab: |
| 92 |
[yellow]if[white] hasHighlights { |
| 93 |
current, _ := strconv.[yellow]Atoi[white](highlights[[red]0[white]]) |
| 94 |
next := (current + [red]1[white]) % [red]9 |
| 95 |
["5"]textView[""].[yellow]Highlight[white](strconv.[yellow]Itoa[white](next)). |
| 96 |
[yellow]ScrollToHighlight[white]() |
| 97 |
} |
| 98 |
[yellow]case[white] tcell.KeyBacktab: |
| 99 |
[yellow]if[white] hasHighlights { |
| 100 |
current, _ := strconv.[yellow]Atoi[white](highlights[[red]0[white]]) |
| 101 |
next := (current - [red]1[white] + [red]9[white]) % [red]9 |
| 102 |
["6"]textView[""].[yellow]Highlight[white](strconv.[yellow]Itoa[white](next)). |
| 103 |
[yellow]ScrollToHighlight[white]() |
| 104 |
} |
| 105 |
} |
| 106 |
}) |
| 107 |
fmt.[yellow]Fprint[white](["7"]textView[""], content) |
| 108 |
tview.[yellow]NewApplication[white](). |
| 109 |
[yellow]SetRoot[white](["8"]textView[""], true). |
| 110 |
[yellow]Run[white]() |
| 111 |
}` |
| 112 |
|
| 113 |
|
| 114 |
func TextView2(nextSlide func()) (title string, content tview.Primitive) { |
| 115 |
codeView := tview.NewTextView(). |
| 116 |
SetWrap(false) |
| 117 |
fmt.Fprint(codeView, textView2) |
| 118 |
codeView.SetBorder(true).SetTitle("Raw text") |
| 119 |
|
| 120 |
textView := tview.NewTextView() |
| 121 |
textView.SetDynamicColors(true). |
| 122 |
SetWrap(false). |
| 123 |
SetRegions(true). |
| 124 |
SetDoneFunc(func(key tcell.Key) { |
| 125 |
if key == tcell.KeyEscape { |
| 126 |
nextSlide() |
| 127 |
return |
| 128 |
} |
| 129 |
highlights := textView.GetHighlights() |
| 130 |
hasHighlights := len(highlights) > 0 |
| 131 |
switch key { |
| 132 |
case tcell.KeyEnter: |
| 133 |
if hasHighlights { |
| 134 |
textView.Highlight() |
| 135 |
} else { |
| 136 |
textView.Highlight("0"). |
| 137 |
ScrollToHighlight() |
| 138 |
} |
| 139 |
case tcell.KeyTab: |
| 140 |
if hasHighlights { |
| 141 |
current, _ := strconv.Atoi(highlights[0]) |
| 142 |
next := (current + 1) % 9 |
| 143 |
textView.Highlight(strconv.Itoa(next)). |
| 144 |
ScrollToHighlight() |
| 145 |
} |
| 146 |
case tcell.KeyBacktab: |
| 147 |
if hasHighlights { |
| 148 |
current, _ := strconv.Atoi(highlights[0]) |
| 149 |
next := (current - 1 + 9) % 9 |
| 150 |
textView.Highlight(strconv.Itoa(next)). |
| 151 |
ScrollToHighlight() |
| 152 |
} |
| 153 |
} |
| 154 |
}) |
| 155 |
fmt.Fprint(textView, textView2) |
| 156 |
textView.SetBorder(true).SetTitle("TextView output") |
| 157 |
return "Text 2", tview.NewFlex(). |
| 158 |
AddItem(textView, 0, 1, true). |
| 159 |
AddItem(codeView, 0, 1, false) |
| 160 |
} |