#!/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). # # 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}" PROMPT_TIMEOUT="${PROMPT_TIMEOUT:--1}" 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() { 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 "$@" } function cmd() { while : ; do prompt read command [ "$command" != "end" ] && eval "${command}" || break [ "$1" ] && break done }