Skip to content

saschagrunert/demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

236 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

demo

ci codecov docs

demo

A framework for performing live pre-recorded command line demos in the wild 📼

Recording command line demos can be a difficult topic these days. Doing a video record has the drawback of lacking flexibility and reduced interactivity during the demo. Typing everything by our own is error prone and distracts the audience from the actual topic we want to show them. So we need something in between, which is easy to use...

This framework should solve the issue by provided interactive demos from your command line!

Usage

Every demo is a stand-alone command line application which consist of multiple runs. For example, if we create a demo like this:

package main

import (
	demo "github.com/saschagrunert/demo"
)

func main() {
	demo.New().Run()
}

Then this demo already contains features like auto-play. We can verify this checking the help output of the executable:

NAME:
   main - A new cli application

USAGE:
   main [global options]

GLOBAL OPTIONS:
   --all, -l                     run all demos
   --auto, -a                    run the demo in automatic mode, where every step gets executed automatically
   --dry-run                     run the demo and only prints the commands
   --no-color                    run the demo and output to be without colors
   --auto-timeout auto, -t auto  the timeout to be waited when auto is enabled (default: 1s)
   --with-breakpoints            breakpoint
   --continue-on-error           continue if there a step fails
   --continuously, -c            run the demos continuously without any end
   --hide-descriptions, -d       hide descriptions between the steps
   --immediate, -i               immediately output without the typewriter animation
   --skip-steps int, -s int      skip the amount of initial steps within the demo (default: 0)
   --shell string                define the shell that is used to execute the command(s) (default: bash)
   --typewriter-speed int        maximum milliseconds per character for typewriter animation (default: 40)
   --help, -h                    show help

The application is based on the urfave/cli framework, which means that we have every possibility to change the command before actually running it.

// Create a new demo CLI application
d := demo.New()

// A demo is an usual urfave/cli application, which means
// that we can set its properties as expected:
d.Name = "A demo of something"
d.Usage = "Learn how this framework is being used"

Creating runs inside demos

To have something to show, we need to create a run and add it to the demo. This can be done by using the demo.Add() method:

func main() {
	// Create a new demo CLI application
	d := demo.New()

	// Register the demo run
	d.Add(example(), "demo-0", "just an example demo run")

	// Run the application, which registers all signal handlers and waits for
	// the app to exit
	d.Run()
}

// example is the single demo run for this application
func example() *Run {
	// A new run contains a title and an optional description
	r := NewRun(
		"Demo Title",
		"Some additional",
		"multi-line description",
		"is possible as well!",
	)

	// A single step can consist of a description and a command to be executed
	r.Step(S(
		"This is a possible",
		"description of the following command",
		"to be executed",
	), S(
		"echo hello world",
	))

	// Commands do not need to have a description, so we could set it to `nil`
	r.Step(nil, S(
		"echo without description",
		"but this can be executed in",
		"multiple lines as well",
	))

	// It is also not needed at all to provide a command
	r.Step(S(
		"Just a description without a command",
	), nil)

	return r
}

The example() function creates a new demo run, which itself contains of multiple steps. These steps are executed in order, can contain a description and a command to be executed. Wrapping commands in multiple lines will automatically create a line break in the command line.

Setup and Cleanup functions

It is also possible to do something before or after each run. For this the setup and cleanup functions can be set to the demo:

func main() {
	// Create a new demo CLI application
	d := demo.New()

	// Be able to run a Setup/Cleanup function before/after each run
	d.Setup(setup)
	d.Cleanup(cleanup)
}

// setup will run before every demo
func setup(ctx context.Context, _ *cli.Command) error {
	// EnsureWithContext can be used for easy sequential command execution
	return EnsureWithContext(ctx,
		"echo 'Doing first setup...'",
		"echo 'Doing second setup...'",
		"echo 'Doing third setup...'",
	)
}

// cleanup will run after every demo
func cleanup(ctx context.Context, _ *cli.Command) error {
	return EnsureWithContext(ctx, "echo 'Doing cleanup...'")
}

Working directory

The working directory for command execution can be configured per run or changed between steps:

func example() *Run {
	r := NewRun("Working Directory Demo")

	// Set the initial working directory for all steps
	r.SetWorkDir("/tmp")

	r.Step(S("Show current directory"), S("pwd"))

	// Change the working directory between steps
	r.Chdir("/home")

	r.Step(S("Now in a different directory"), S("pwd"))

	return r
}

SetWorkDir sets the initial working directory for the run. Chdir inserts a step that changes the working directory for all subsequent steps and displays > cd <dir> in the output. If no working directory is set, commands run in the current process directory.

Environment variables

Custom environment variables can be set for all steps in a run:

func example() *Run {
	r := NewRun("Environment Demo")

	r.SetEnv("MY_VAR=hello", "OTHER_VAR=world")

	r.Step(S("Show env"), S("echo $MY_VAR $OTHER_VAR"))

	return r
}

Variables are appended to the current process environment. Multiple calls to SetEnv accumulate variables.

Terminal raw mode

During the typewriter animation and while waiting for user input, the terminal is put into raw mode. This suppresses any visible output from keypresses (such as newlines from pressing Enter) during the animation. When the animation completes, the terminal is restored to its previous state. This only applies when stdin is an interactive terminal; non-terminal input (e.g., in tests) is unaffected.

Contributing

You want to contribute to this project? Wow, thanks! So please just fork it and send me a pull request.

About

A framework for performing live pre-recorded command line demos in the wild 📼

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors