#!/usr/bin/env bash # # 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) # color(): allows you to color (second argument, defaults to $clr) some text (first argument) # # Helpers (internal stuff): # simtyping(): let's data piped to it through at rates similar to typing # prompt(): prints the bash PS1 prompt on your system with a nasty hack TYPE_SPEED="${TYPE_SPEED:-15}" PROMPT_TIMEOUT="${PROMPT_TIMEOUT:--1}" # some colors def="\033[0;00m" wht="\033[1;37m" red="\033[0;31m" grn="\033[0;32m" blu="\033[0;34m" clr="$wht" function color() { printf "${2:-$clr}" printf "${1}" printf "${def}" } function simtyping() { pv -qL $(($TYPE_SPEED+$((-2 + RANDOM%5)))); } function type() { printf "$*" | simtyping; } function wait() { [ "$PROMPT_TIMEOUT" == "-1" ] && read -rs || read -rst "$PROMPT_TIMEOUT" } function prompt() { invalidcmd="somethingthatshouldbetotallynotacommandinanygivenenvironment" expPS1=$(echo $invalidcmd | bash --norc -i 2>&1 | grep $invalidcmd | head -n 1 | sed "s/$invalidcmd//g") printf "$expPS1" } function execute() { prompt type "$@" && wait printf "\n" eval "$@" } function cmd() { while : ; do prompt read -e command [ "$command" != "end" ] && eval "${command}" || break [ "$1" ] && break done }