2017-12-31 05:10:13 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2020-10-18 20:15:57 +08:00
|
|
|
"github.com/gdamore/tcell/v2"
|
2017-12-31 05:10:13 +08:00
|
|
|
"github.com/rivo/tview"
|
|
|
|
)
|
|
|
|
|
|
|
|
const textView1 = `[green]func[white] [yellow]main[white]() {
|
2018-11-22 15:49:20 +08:00
|
|
|
app := tview.[yellow]NewApplication[white]()
|
2017-12-31 05:10:13 +08:00
|
|
|
textView := tview.[yellow]NewTextView[white]().
|
|
|
|
[yellow]SetTextColor[white](tcell.ColorYellow).
|
|
|
|
[yellow]SetScrollable[white](false).
|
|
|
|
[yellow]SetChangedFunc[white]([yellow]func[white]() {
|
|
|
|
app.[yellow]Draw[white]()
|
|
|
|
})
|
|
|
|
[green]go[white] [yellow]func[white]() {
|
|
|
|
[green]var[white] n [green]int
|
|
|
|
[white] [yellow]for[white] {
|
|
|
|
n++
|
|
|
|
fmt.[yellow]Fprintf[white](textView, [red]"%d "[white], n)
|
|
|
|
time.[yellow]Sleep[white]([red]200[white] * time.Millisecond)
|
|
|
|
}
|
|
|
|
}()
|
2018-11-26 19:15:57 +08:00
|
|
|
app.[yellow]SetRoot[white](textView, true).
|
2017-12-31 05:10:13 +08:00
|
|
|
[yellow]Run[white]()
|
|
|
|
}`
|
|
|
|
|
|
|
|
// TextView1 demonstrates the basic text view.
|
|
|
|
func TextView1(nextSlide func()) (title string, content tview.Primitive) {
|
|
|
|
textView := tview.NewTextView().
|
|
|
|
SetTextColor(tcell.ColorYellow).
|
|
|
|
SetScrollable(false).
|
2018-10-28 20:42:49 +08:00
|
|
|
SetDoneFunc(func(key tcell.Key) {
|
|
|
|
nextSlide()
|
|
|
|
})
|
|
|
|
textView.SetChangedFunc(func() {
|
|
|
|
if textView.HasFocus() {
|
2017-12-31 05:10:13 +08:00
|
|
|
app.Draw()
|
2018-10-28 20:42:49 +08:00
|
|
|
}
|
2017-12-31 05:10:13 +08:00
|
|
|
})
|
|
|
|
go func() {
|
|
|
|
var n int
|
|
|
|
for {
|
2019-06-30 10:03:04 +08:00
|
|
|
if textView.HasFocus() {
|
|
|
|
n++
|
2020-02-20 05:08:05 +08:00
|
|
|
if n > 512 {
|
|
|
|
n = 1
|
|
|
|
textView.SetText("")
|
|
|
|
}
|
2020-01-07 08:02:31 +08:00
|
|
|
|
2019-06-30 10:03:04 +08:00
|
|
|
fmt.Fprintf(textView, "%d ", n)
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
} else {
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
2017-12-31 05:10:13 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
textView.SetBorder(true).SetTitle("TextView implements io.Writer")
|
|
|
|
return "Text 1", Code(textView, 36, 13, textView1)
|
|
|
|
}
|
|
|
|
|
|
|
|
const textView2 = `[green]package[white] main
|
|
|
|
|
|
|
|
[green]import[white] (
|
|
|
|
[red]"strconv"[white]
|
|
|
|
|
2020-10-18 20:15:57 +08:00
|
|
|
[red]"github.com/gdamore/tcell/v2"[white]
|
2017-12-31 05:10:13 +08:00
|
|
|
[red]"github.com/rivo/tview"[white]
|
|
|
|
)
|
|
|
|
|
|
|
|
[green]func[white] [yellow]main[white]() {
|
|
|
|
["0"]textView[""] := tview.[yellow]NewTextView[white]()
|
|
|
|
["1"]textView[""].[yellow]SetDynamicColors[white](true).
|
|
|
|
[yellow]SetWrap[white](false).
|
|
|
|
[yellow]SetRegions[white](true).
|
|
|
|
[yellow]SetDoneFunc[white]([yellow]func[white](key tcell.Key) {
|
|
|
|
highlights := ["2"]textView[""].[yellow]GetHighlights[white]()
|
|
|
|
hasHighlights := [yellow]len[white](highlights) > [red]0
|
2018-06-20 16:06:05 +08:00
|
|
|
[yellow]switch[white] key {
|
2017-12-31 05:10:13 +08:00
|
|
|
[yellow]case[white] tcell.KeyEnter:
|
|
|
|
[yellow]if[white] hasHighlights {
|
|
|
|
["3"]textView[""].[yellow]Highlight[white]()
|
|
|
|
} [yellow]else[white] {
|
|
|
|
["4"]textView[""].[yellow]Highlight[white]([red]"0"[white]).
|
|
|
|
[yellow]ScrollToHighlight[white]()
|
|
|
|
}
|
|
|
|
[yellow]case[white] tcell.KeyTab:
|
|
|
|
[yellow]if[white] hasHighlights {
|
|
|
|
current, _ := strconv.[yellow]Atoi[white](highlights[[red]0[white]])
|
|
|
|
next := (current + [red]1[white]) % [red]9
|
2018-06-20 16:06:05 +08:00
|
|
|
["5"]textView[""].[yellow]Highlight[white](strconv.[yellow]Itoa[white](next)).
|
2017-12-31 05:10:13 +08:00
|
|
|
[yellow]ScrollToHighlight[white]()
|
|
|
|
}
|
|
|
|
[yellow]case[white] tcell.KeyBacktab:
|
|
|
|
[yellow]if[white] hasHighlights {
|
|
|
|
current, _ := strconv.[yellow]Atoi[white](highlights[[red]0[white]])
|
|
|
|
next := (current - [red]1[white] + [red]9[white]) % [red]9
|
2018-06-20 16:06:05 +08:00
|
|
|
["6"]textView[""].[yellow]Highlight[white](strconv.[yellow]Itoa[white](next)).
|
2017-12-31 05:10:13 +08:00
|
|
|
[yellow]ScrollToHighlight[white]()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
fmt.[yellow]Fprint[white](["7"]textView[""], content)
|
|
|
|
tview.[yellow]NewApplication[white]().
|
|
|
|
[yellow]SetRoot[white](["8"]textView[""], true).
|
|
|
|
[yellow]Run[white]()
|
|
|
|
}`
|
|
|
|
|
|
|
|
// TextView2 demonstrates the extended text view.
|
|
|
|
func TextView2(nextSlide func()) (title string, content tview.Primitive) {
|
|
|
|
codeView := tview.NewTextView().
|
|
|
|
SetWrap(false)
|
|
|
|
fmt.Fprint(codeView, textView2)
|
2018-05-23 22:56:06 +08:00
|
|
|
codeView.SetBorder(true).SetTitle("Buffer content")
|
2017-12-31 05:10:13 +08:00
|
|
|
|
|
|
|
textView := tview.NewTextView()
|
|
|
|
textView.SetDynamicColors(true).
|
|
|
|
SetWrap(false).
|
|
|
|
SetRegions(true).
|
|
|
|
SetDoneFunc(func(key tcell.Key) {
|
|
|
|
if key == tcell.KeyEscape {
|
|
|
|
nextSlide()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
highlights := textView.GetHighlights()
|
|
|
|
hasHighlights := len(highlights) > 0
|
|
|
|
switch key {
|
|
|
|
case tcell.KeyEnter:
|
|
|
|
if hasHighlights {
|
|
|
|
textView.Highlight()
|
|
|
|
} else {
|
|
|
|
textView.Highlight("0").
|
|
|
|
ScrollToHighlight()
|
|
|
|
}
|
|
|
|
case tcell.KeyTab:
|
|
|
|
if hasHighlights {
|
|
|
|
current, _ := strconv.Atoi(highlights[0])
|
2018-01-07 05:49:12 +08:00
|
|
|
next := (current + 1) % 9
|
2017-12-31 05:10:13 +08:00
|
|
|
textView.Highlight(strconv.Itoa(next)).
|
|
|
|
ScrollToHighlight()
|
|
|
|
}
|
|
|
|
case tcell.KeyBacktab:
|
|
|
|
if hasHighlights {
|
|
|
|
current, _ := strconv.Atoi(highlights[0])
|
2018-01-07 05:49:12 +08:00
|
|
|
next := (current - 1 + 9) % 9
|
2017-12-31 05:10:13 +08:00
|
|
|
textView.Highlight(strconv.Itoa(next)).
|
|
|
|
ScrollToHighlight()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
fmt.Fprint(textView, textView2)
|
2018-05-23 22:56:06 +08:00
|
|
|
textView.SetBorder(true).SetTitle("TextView output")
|
2017-12-31 05:10:13 +08:00
|
|
|
return "Text 2", tview.NewFlex().
|
|
|
|
AddItem(textView, 0, 1, true).
|
|
|
|
AddItem(codeView, 0, 1, false)
|
|
|
|
}
|