...

Source file src/github.com/rivo/tview/demos/presentation/main.go

Documentation: github.com/rivo/tview/demos/presentation

     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  			pages.SwitchToPage(added[0])
    59  		})
    60  
    61  	// Create the pages for all slides.
    62  	previousSlide := func() {
    63  		slide, _ := strconv.Atoi(info.GetHighlights()[0])
    64  		slide = (slide - 1 + len(slides)) % len(slides)
    65  		info.Highlight(strconv.Itoa(slide)).
    66  			ScrollToHighlight()
    67  	}
    68  	nextSlide := func() {
    69  		slide, _ := strconv.Atoi(info.GetHighlights()[0])
    70  		slide = (slide + 1) % len(slides)
    71  		info.Highlight(strconv.Itoa(slide)).
    72  			ScrollToHighlight()
    73  	}
    74  	for index, slide := range slides {
    75  		title, primitive := slide(nextSlide)
    76  		pages.AddPage(strconv.Itoa(index), primitive, true, index == 0)
    77  		fmt.Fprintf(info, `%d ["%d"][darkcyan]%s[white][""]  `, index+1, index, title)
    78  	}
    79  	info.Highlight("0")
    80  
    81  	// Create the main layout.
    82  	layout := tview.NewFlex().
    83  		SetDirection(tview.FlexRow).
    84  		AddItem(pages, 0, 1, true).
    85  		AddItem(info, 1, 1, false)
    86  
    87  	// Shortcuts to navigate the slides.
    88  	app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
    89  		if event.Key() == tcell.KeyCtrlN {
    90  			nextSlide()
    91  			return nil
    92  		} else if event.Key() == tcell.KeyCtrlP {
    93  			previousSlide()
    94  			return nil
    95  		}
    96  		return event
    97  	})
    98  
    99  	// Start the application.
   100  	if err := app.SetRoot(layout, true).EnableMouse(true).Run(); err != nil {
   101  		panic(err)
   102  	}
   103  }
   104  

View as plain text