tview/demos/presentation/main.go

104 lines
2.4 KiB
Go
Raw Normal View History

/*
2018-01-04 04:13:32 +08:00
A presentation of the tview package, implemented with tview.
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:
- Ctrl-N: Jump to next slide
- Ctrl-P: Jump to previous slide
*/
package main
import (
"fmt"
"strconv"
"github.com/gdamore/tcell/v2"
"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,
Flex,
Grid,
Colors,
End,
}
pages := tview.NewPages()
// The bottom row has some info on where we are.
info := tview.NewTextView().
SetDynamicColors(true).
SetRegions(true).
SetWrap(false).
SetHighlightedFunc(func(added, removed, remaining []string) {
pages.SwitchToPage(added[0])
})
// Create the pages for all slides.
previousSlide := func() {
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()
}
nextSlide := func() {
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()
}
for index, slide := range slides {
title, primitive := slide(nextSlide)
pages.AddPage(strconv.Itoa(index), primitive, true, index == 0)
fmt.Fprintf(info, `%d ["%d"][darkcyan]%s[white][""] `, index+1, index, title)
}
info.Highlight("0")
// 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.
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyCtrlN {
nextSlide()
return nil
} else if event.Key() == tcell.KeyCtrlP {
previousSlide()
return nil
}
return event
})
// Start the application.
if err := app.SetRoot(layout, true).EnableMouse(true).Run(); err != nil {
panic(err)
}
}