...
1
13 package main
14
15 import (
16 "fmt"
17 "strconv"
18
19 "github.com/gdamore/tcell/v2"
20 "github.com/rivo/tview"
21 )
22
23
24
25
26 type Slide func(nextSlide func()) (title string, content tview.Primitive)
27
28
29 var app = tview.NewApplication()
30
31
32 func main() {
33
34 slides := []Slide{
35 Cover,
36 Introduction,
37 HelloWorld,
38 InputField,
39 Form,
40 TextView1,
41 TextView2,
42 Table,
43 TreeView,
44 Flex,
45 Grid,
46 Colors,
47 End,
48 }
49
50 pages := tview.NewPages()
51
52
53 info := tview.NewTextView().
54 SetDynamicColors(true).
55 SetRegions(true).
56 SetWrap(false).
57 SetHighlightedFunc(func(added, removed, remaining []string) {
58 pages.SwitchToPage(added[0])
59 })
60
61
62 previousSlide := func() {
63 slide, _ := strconv.Atoi(info.GetHighlights()[0])
64 slide = (slide - 1 + len(slides)) % len(slides)
65 info.Highlight(strconv.Itoa(slide)).
66 ScrollToHighlight()
67 }
68 nextSlide := func() {
69 slide, _ := strconv.Atoi(info.GetHighlights()[0])
70 slide = (slide + 1) % len(slides)
71 info.Highlight(strconv.Itoa(slide)).
72 ScrollToHighlight()
73 }
74 for index, slide := range slides {
75 title, primitive := slide(nextSlide)
76 pages.AddPage(strconv.Itoa(index), primitive, true, index == 0)
77 fmt.Fprintf(info, `%d ["%d"][darkcyan]%s[white][""] `, index+1, index, title)
78 }
79 info.Highlight("0")
80
81
82 layout := tview.NewFlex().
83 SetDirection(tview.FlexRow).
84 AddItem(pages, 0, 1, true).
85 AddItem(info, 1, 1, false)
86
87
88 app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
89 if event.Key() == tcell.KeyCtrlN {
90 nextSlide()
91 return nil
92 } else if event.Key() == tcell.KeyCtrlP {
93 previousSlide()
94 return nil
95 }
96 return event
97 })
98
99
100 if err := app.SetRoot(layout, true).EnableMouse(true).Run(); err != nil {
101 panic(err)
102 }
103 }
104
View as plain text