cview/demos/presentation/main.go

131 lines
3.2 KiB
Go
Raw Normal View History

/*
A presentation of the cview package, implemented with cview.
2018-01-03 20:13:32 +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:
- Ctrl-N: Jump to next slide
- Ctrl-P: Jump to previous slide
*/
package main
import (
2020-03-25 14:32:57 +00:00
"flag"
"fmt"
2020-03-25 14:32:57 +00:00
"log"
"net/http"
_ "net/http/pprof"
"strconv"
"code.rocketnine.space/tslocum/cview"
2020-08-30 15:36:03 +00:00
"github.com/gdamore/tcell/v2"
)
const (
appInfo = "Next slide: Ctrl-N Previous: Ctrl-P Exit: Ctrl-C (Navigate with your keyboard or mouse)"
listInfo = "Next item: J, Down Previous item: K, Up Open context menu: Alt+Enter"
textViewInfo = "Scroll down: J, Down, PageDown Scroll up: K, Up, PageUp"
sliderInfo = "Decrease: H, J, Left, Down Increase: K, L, Right, Up"
formInfo = "Next field: Tab Previous field: Shift+Tab Select: Enter"
windowInfo = "Windows may be dragged an resized using the mouse."
)
// Slide is a function which returns the slide's title, any applicable
// information and its main primitive, its. It receives a "nextSlide" function
// which can be called to advance the presentation to the next slide.
type Slide func(nextSlide func()) (title string, info string, content cview.Primitive)
// The application.
var app = cview.NewApplication()
// 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
}()
}
app.EnableMouse(true)
// The presentation slides.
slides := []Slide{
Cover,
Introduction,
2020-04-03 15:01:21 +00:00
Colors,
TextView1,
TextView2,
2020-04-03 15:01:21 +00:00
InputField,
2020-10-11 20:39:04 +00:00
Slider,
2020-04-03 15:01:21 +00:00
Form,
Table,
2018-06-20 08:06:05 +00:00
TreeView,
Flex,
Grid,
2020-09-24 15:09:55 +00:00
Window,
End,
}
2020-10-16 01:50:10 +00:00
panels := cview.NewTabbedPanels()
// Create the pages for all slides.
previousSlide := func() {
2020-10-16 01:50:10 +00:00
slide, _ := strconv.Atoi(panels.GetCurrentTab())
slide = (slide - 1 + len(slides)) % len(slides)
2020-10-16 01:50:10 +00:00
panels.SetCurrentTab(strconv.Itoa(slide))
}
nextSlide := func() {
2020-10-16 01:50:10 +00:00
slide, _ := strconv.Atoi(panels.GetCurrentTab())
slide = (slide + 1) % len(slides)
2020-10-16 01:50:10 +00:00
panels.SetCurrentTab(strconv.Itoa(slide))
}
cursor := 0
var slideRegions []int
for index, slide := range slides {
slideRegions = append(slideRegions, cursor)
title, info, primitive := slide(nextSlide)
h := cview.NewTextView()
if info != "" {
h.SetDynamicColors(true)
h.SetText(" [" + cview.ColorHex(cview.Styles.SecondaryTextColor) + "]Info:[-] " + info)
}
// Create a Flex layout that centers the logo and subtitle.
f := cview.NewFlex()
f.SetDirection(cview.FlexRow)
f.AddItem(h, 1, 1, false)
f.AddItem(primitive, 0, 1, true)
panels.AddTab(strconv.Itoa(index), title, f)
cursor += len(title) + 4
}
2020-10-16 01:50:10 +00:00
panels.SetCurrentTab("0")
// Shortcuts to navigate the slides.
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyCtrlN {
nextSlide()
} else if event.Key() == tcell.KeyCtrlP {
previousSlide()
}
return event
})
// Start the application.
2020-10-16 01:50:10 +00:00
app.SetRoot(panels, true)
if err := app.Run(); err != nil {
panic(err)
}
}