2017-12-31 05:10:13 +08:00
|
|
|
/*
|
2018-01-04 04:13:32 +08:00
|
|
|
A presentation of the tview package, implemented with tview.
|
|
|
|
|
2017-12-31 05:10:13 +08:00
|
|
|
Navigation
|
|
|
|
|
2018-01-04 04:13:32 +08:00
|
|
|
The presentation will advance to the next slide when the primitive demonstrated
|
|
|
|
in the current slide is left (usually by hitting Enter or Escape). Additionally,
|
|
|
|
the following shortcuts can be used:
|
|
|
|
|
2017-12-31 05:10:13 +08:00
|
|
|
- Ctrl-N: Jump to next slide
|
|
|
|
- Ctrl-P: Jump to previous slide
|
|
|
|
*/
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Slide is a function which returns the slide's main primitive and its title.
|
|
|
|
// It receives a "nextSlide" function which can be called to advance the
|
|
|
|
// presentation to the next slide.
|
|
|
|
type Slide func(nextSlide func()) (title string, content tview.Primitive)
|
|
|
|
|
|
|
|
// The application.
|
|
|
|
var app = tview.NewApplication()
|
|
|
|
|
|
|
|
// Starting point for the presentation.
|
|
|
|
func main() {
|
|
|
|
// The presentation slides.
|
|
|
|
slides := []Slide{
|
|
|
|
Cover,
|
|
|
|
Introduction,
|
|
|
|
HelloWorld,
|
|
|
|
InputField,
|
|
|
|
Form,
|
|
|
|
TextView1,
|
|
|
|
TextView2,
|
|
|
|
Table,
|
2018-06-20 16:06:05 +08:00
|
|
|
TreeView,
|
2017-12-31 05:10:13 +08:00
|
|
|
Flex,
|
2018-02-20 18:30:57 +08:00
|
|
|
Grid,
|
2018-01-18 00:13:36 +08:00
|
|
|
Colors,
|
2017-12-31 05:10:13 +08:00
|
|
|
End,
|
|
|
|
}
|
|
|
|
|
2020-03-30 02:53:28 +08:00
|
|
|
pages := tview.NewPages()
|
|
|
|
|
2017-12-31 05:10:13 +08:00
|
|
|
// The bottom row has some info on where we are.
|
|
|
|
info := tview.NewTextView().
|
|
|
|
SetDynamicColors(true).
|
|
|
|
SetRegions(true).
|
2020-03-30 02:53:28 +08:00
|
|
|
SetWrap(false).
|
|
|
|
SetHighlightedFunc(func(added, removed, remaining []string) {
|
|
|
|
pages.SwitchToPage(added[0])
|
|
|
|
})
|
2017-12-31 05:10:13 +08:00
|
|
|
|
|
|
|
// Create the pages for all slides.
|
|
|
|
previousSlide := func() {
|
2020-03-30 02:53:28 +08:00
|
|
|
slide, _ := strconv.Atoi(info.GetHighlights()[0])
|
|
|
|
slide = (slide - 1 + len(slides)) % len(slides)
|
|
|
|
info.Highlight(strconv.Itoa(slide)).
|
2018-06-20 16:06:05 +08:00
|
|
|
ScrollToHighlight()
|
2017-12-31 05:10:13 +08:00
|
|
|
}
|
|
|
|
nextSlide := func() {
|
2020-03-30 02:53:28 +08:00
|
|
|
slide, _ := strconv.Atoi(info.GetHighlights()[0])
|
|
|
|
slide = (slide + 1) % len(slides)
|
|
|
|
info.Highlight(strconv.Itoa(slide)).
|
2018-06-20 16:06:05 +08:00
|
|
|
ScrollToHighlight()
|
2017-12-31 05:10:13 +08:00
|
|
|
}
|
|
|
|
for index, slide := range slides {
|
|
|
|
title, primitive := slide(nextSlide)
|
2020-03-30 02:53:28 +08:00
|
|
|
pages.AddPage(strconv.Itoa(index), primitive, true, index == 0)
|
2018-01-12 16:09:01 +08:00
|
|
|
fmt.Fprintf(info, `%d ["%d"][darkcyan]%s[white][""] `, index+1, index, title)
|
2017-12-31 05:10:13 +08:00
|
|
|
}
|
2020-03-30 02:53:28 +08:00
|
|
|
info.Highlight("0")
|
2017-12-31 05:10:13 +08:00
|
|
|
|
|
|
|
// Create the main layout.
|
|
|
|
layout := tview.NewFlex().
|
|
|
|
SetDirection(tview.FlexRow).
|
|
|
|
AddItem(pages, 0, 1, true).
|
|
|
|
AddItem(info, 1, 1, false)
|
|
|
|
|
|
|
|
// Shortcuts to navigate the slides.
|
2018-01-15 04:29:34 +08:00
|
|
|
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
|
|
|
if event.Key() == tcell.KeyCtrlN {
|
|
|
|
nextSlide()
|
2020-08-18 18:05:43 +08:00
|
|
|
return nil
|
2018-01-15 04:29:34 +08:00
|
|
|
} else if event.Key() == tcell.KeyCtrlP {
|
|
|
|
previousSlide()
|
2020-08-18 18:05:43 +08:00
|
|
|
return nil
|
2018-01-15 04:29:34 +08:00
|
|
|
}
|
|
|
|
return event
|
2017-12-31 05:10:13 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
// Start the application.
|
2020-03-28 01:41:44 +08:00
|
|
|
if err := app.SetRoot(layout, true).EnableMouse(true).Run(); err != nil {
|
2017-12-31 05:10:13 +08:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|