Debian Codegraph @

demos/presentation/main.go

raw symbols SCIP
1 /*
2 A presentation of the tview package, implemented with tview.
3
4 # Navigation
5
6 The presentation will advance to the next slide when the primitive demonstrated
7 in the current slide is left (usually by hitting Enter or Escape). Additionally,
8 the following shortcuts can be used:
9
10 - Ctrl-N: Jump to next slide
11 - Ctrl-P: Jump to previous slide
12 */
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 // Slide is a function which returns the slide's main primitive and its title.
24 // It receives a "nextSlide" function which can be called to advance the
25 // presentation to the next slide.
26 type Slide func(nextSlide func()) (title string, content tview.Primitive)
27
28 // The application.
29 var app = tview.NewApplication()
30
31 // Starting point for the presentation.
32 func main() {
33 // The presentation slides.
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 // The bottom row has some info on where we are.
53 info := tview.NewTextView().
54 SetDynamicColors(true).
55 SetRegions(true).
56 SetWrap(false).
57 SetHighlightedFunc(func(added, removed, remaining []string) {
58 if len(added) == 0 {
59 return
60 }
61
62 pages.SwitchToPage(added[0])
63 })
64
65 // Create the pages for all slides.
66 previousSlide := func() {
67 slide, _ := strconv.Atoi(info.GetHighlights()[0])
68 slide = (slide - 1 + len(slides)) % len(slides)
69 info.Highlight(strconv.Itoa(slide)).
70 ScrollToHighlight()
71 }
72 nextSlide := func() {
73 slide, _ := strconv.Atoi(info.GetHighlights()[0])
74 slide = (slide + 1) % len(slides)
75 info.Highlight(strconv.Itoa(slide)).
76 ScrollToHighlight()
77 }
78 for index, slide := range slides {
79 title, primitive := slide(nextSlide)
80 pages.AddPage(strconv.Itoa(index), primitive, true, index == 0)
81 fmt.Fprintf(info, `%d ["%d"][darkcyan]%s[white][""] `, index+1, index, title)
82 }
83 info.Highlight("0")
84
85 // Create the main layout.
86 layout := tview.NewFlex().
87 SetDirection(tview.FlexRow).
88 AddItem(pages, 0, 1, true).
89 AddItem(info, 1, 1, false)
90
91 // Shortcuts to navigate the slides.
92 app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
93 if event.Key() == tcell.KeyCtrlN {
94 nextSlide()
95 return nil
96 } else if event.Key() == tcell.KeyCtrlP {
97 previousSlide()
98 return nil
99 }
100 return event
101 })
102
103 // Start the application.
104 if err := app.SetRoot(layout, true).EnableMouse(true).EnablePaste(true).Run(); err != nil {
105 panic(err)
106 }
107 }