2017-12-30 21:10:13 +00:00
|
|
|
/*
|
2019-12-30 23:12:17 +00:00
|
|
|
A presentation of the cview package, implemented with cview.
|
2018-01-03 20:13:32 +00:00
|
|
|
|
2017-12-30 21:10:13 +00:00
|
|
|
Navigation
|
|
|
|
|
2018-01-03 20:13:32 +00: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-30 21:10:13 +00:00
|
|
|
- Ctrl-N: Jump to next slide
|
|
|
|
- Ctrl-P: Jump to previous slide
|
|
|
|
*/
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-03-25 14:32:57 +00:00
|
|
|
"flag"
|
2017-12-30 21:10:13 +00:00
|
|
|
"fmt"
|
2020-03-25 14:32:57 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
_ "net/http/pprof"
|
2017-12-30 21:10:13 +00:00
|
|
|
"strconv"
|
|
|
|
|
2020-08-30 15:36:03 +00:00
|
|
|
"github.com/gdamore/tcell/v2"
|
2020-01-22 23:28:19 +00:00
|
|
|
"gitlab.com/tslocum/cview"
|
2017-12-30 21:10:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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.
|
2019-12-30 23:12:17 +00:00
|
|
|
type Slide func(nextSlide func()) (title string, content cview.Primitive)
|
2017-12-30 21:10:13 +00:00
|
|
|
|
|
|
|
// The application.
|
2019-12-30 23:12:17 +00:00
|
|
|
var app = cview.NewApplication()
|
2017-12-30 21:10:13 +00:00
|
|
|
|
|
|
|
// Starting point for the presentation.
|
|
|
|
func main() {
|
2020-03-25 14:32:57 +00:00
|
|
|
var debugPort int
|
|
|
|
flag.IntVar(&debugPort, "debug", 0, "port to serve debug info")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if debugPort > 0 {
|
|
|
|
go func() {
|
2020-05-08 23:12:48 +00:00
|
|
|
log.Fatal(http.ListenAndServe(fmt.Sprintf("localhost:%d", debugPort), nil))
|
2020-03-25 14:32:57 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2017-12-30 21:10:13 +00:00
|
|
|
// The presentation slides.
|
|
|
|
slides := []Slide{
|
|
|
|
Cover,
|
|
|
|
Introduction,
|
2020-04-03 15:01:21 +00:00
|
|
|
Colors,
|
2017-12-30 21:10:13 +00:00
|
|
|
TextView1,
|
|
|
|
TextView2,
|
2020-04-03 15:01:21 +00:00
|
|
|
InputField,
|
|
|
|
Form,
|
2017-12-30 21:10:13 +00:00
|
|
|
Table,
|
2018-06-20 08:06:05 +00:00
|
|
|
TreeView,
|
2017-12-30 21:10:13 +00:00
|
|
|
Flex,
|
2018-02-20 10:30:57 +00:00
|
|
|
Grid,
|
2017-12-30 21:10:13 +00:00
|
|
|
End,
|
|
|
|
}
|
|
|
|
|
2020-03-29 19:43:46 +00:00
|
|
|
pages := cview.NewPages()
|
|
|
|
|
2017-12-30 21:10:13 +00:00
|
|
|
// The bottom row has some info on where we are.
|
2020-04-01 15:59:06 +00:00
|
|
|
info := cview.NewTextView()
|
|
|
|
info.
|
2017-12-30 21:10:13 +00:00
|
|
|
SetDynamicColors(true).
|
|
|
|
SetRegions(true).
|
2020-03-29 19:43:46 +00:00
|
|
|
SetWrap(false).
|
|
|
|
SetHighlightedFunc(func(added, removed, remaining []string) {
|
|
|
|
pages.SwitchToPage(added[0])
|
|
|
|
})
|
2017-12-30 21:10:13 +00:00
|
|
|
|
|
|
|
// Create the pages for all slides.
|
|
|
|
previousSlide := func() {
|
2020-03-29 19:43:46 +00:00
|
|
|
slide, _ := strconv.Atoi(info.GetHighlights()[0])
|
|
|
|
slide = (slide - 1 + len(slides)) % len(slides)
|
|
|
|
info.Highlight(strconv.Itoa(slide)).
|
2018-06-20 08:06:05 +00:00
|
|
|
ScrollToHighlight()
|
2017-12-30 21:10:13 +00:00
|
|
|
}
|
|
|
|
nextSlide := func() {
|
2020-03-29 19:43:46 +00:00
|
|
|
slide, _ := strconv.Atoi(info.GetHighlights()[0])
|
|
|
|
slide = (slide + 1) % len(slides)
|
|
|
|
info.Highlight(strconv.Itoa(slide)).
|
2018-06-20 08:06:05 +00:00
|
|
|
ScrollToHighlight()
|
2017-12-30 21:10:13 +00:00
|
|
|
}
|
2020-01-04 02:46:23 +00:00
|
|
|
|
|
|
|
cursor := 0
|
|
|
|
var slideRegions []int
|
2017-12-30 21:10:13 +00:00
|
|
|
for index, slide := range slides {
|
2020-01-04 02:46:23 +00:00
|
|
|
slideRegions = append(slideRegions, cursor)
|
|
|
|
|
2017-12-30 21:10:13 +00:00
|
|
|
title, primitive := slide(nextSlide)
|
2020-03-29 19:43:46 +00:00
|
|
|
pages.AddPage(strconv.Itoa(index), primitive, true, index == 0)
|
2020-04-03 15:01:21 +00:00
|
|
|
fmt.Fprintf(info, `["%d"][darkcyan] %s [white][""]|`, index, title)
|
2020-01-04 02:46:23 +00:00
|
|
|
|
|
|
|
cursor += len(title) + 4
|
2017-12-30 21:10:13 +00:00
|
|
|
}
|
2020-03-29 19:43:46 +00:00
|
|
|
info.Highlight("0")
|
2017-12-30 21:10:13 +00:00
|
|
|
|
|
|
|
// Create the main layout.
|
2019-12-30 23:12:17 +00:00
|
|
|
layout := cview.NewFlex().
|
|
|
|
SetDirection(cview.FlexRow).
|
2017-12-30 21:10:13 +00:00
|
|
|
AddItem(pages, 0, 1, true).
|
|
|
|
AddItem(info, 1, 1, false)
|
|
|
|
|
|
|
|
// Shortcuts to navigate the slides.
|
2018-01-14 20:29:34 +00:00
|
|
|
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
|
|
|
if event.Key() == tcell.KeyCtrlN {
|
|
|
|
nextSlide()
|
|
|
|
} else if event.Key() == tcell.KeyCtrlP {
|
|
|
|
previousSlide()
|
|
|
|
}
|
|
|
|
return event
|
2017-12-30 21:10:13 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Start the application.
|
2020-03-29 19:43:46 +00:00
|
|
|
if err := app.SetRoot(layout, true).EnableMouse(true).Run(); err != nil {
|
2017-12-30 21:10:13 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|