demo.sh/demo.sh

54 lines
1.2 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
2017-10-08 19:25:16 +00:00
#
# A handy collection of tools to create interactive demos:
# type(): simulates typing its arguments
# execute(): simuates typing then executing its arguments in a shell
# wait(): waits for user input with optional timeout
# cmd(): gives control to user until she enters 'end' (or for a single command if you pass an argument).
#
# Helpers (internal stuff):
# simtyping(): let's data piped to it through at rates similar to typing
# prompt(): prints a nice colored prompt
TYPE_SPEED="${TYPE_SPEED:-15}"
2017-10-08 00:57:44 +00:00
PROMPT_TIMEOUT="${PROMPT_TIMEOUT:--1}"
function simtyping()
{ pv -qL $(($TYPE_SPEED+$((-2 + RANDOM%5)))); }
2017-10-08 00:57:44 +00:00
2017-10-08 19:25:16 +00:00
function type()
{ printf "$*" | simtyping; }
2017-10-08 00:57:44 +00:00
function wait()
{
[ "$PROMPT_TIMEOUT" == "-1" ] &&
read -rs ||
2017-10-08 00:57:44 +00:00
read -rst "$PROMPT_TIMEOUT"
}
2017-10-08 01:01:25 +00:00
function prompt()
{
def="\033[0;00m"
blu="\033[0;34m"
cyn="\033[0;36m"
printf "[$cyn$(whoami)$def@$blu$(hostname)$def $(basename `pwd`)]\$ ";
}
function execute()
{
prompt
type "$@" && wait
printf "\n"
eval "$@"
}
2017-10-08 01:10:27 +00:00
function cmd()
{
while : ; do
prompt
read command
[ "$command" != "end" ] && eval "${command}" || break
[ "$1" ] && break
done
}