2017-10-08 00:55:26 +00:00
|
|
|
#!/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
|
2017-10-20 10:23:02 +00:00
|
|
|
# cmd(): gives control to user until she enters 'end' (or for a single command if you pass an argument)
|
2017-10-20 10:12:03 +00:00
|
|
|
# color(): allows you to color (second argument, defaults to $clr) some text (first argument)
|
2017-10-08 19:25:16 +00:00
|
|
|
#
|
|
|
|
# Helpers (internal stuff):
|
|
|
|
# simtyping(): let's data piped to it through at rates similar to typing
|
2017-10-20 10:23:02 +00:00
|
|
|
# prompt(): prints the bash PS1 prompt on your system with a nasty hack
|
2017-10-08 00:55:26 +00:00
|
|
|
|
|
|
|
TYPE_SPEED="${TYPE_SPEED:-15}"
|
2017-10-08 00:57:44 +00:00
|
|
|
PROMPT_TIMEOUT="${PROMPT_TIMEOUT:--1}"
|
2017-10-08 00:55:26 +00:00
|
|
|
|
2017-10-20 10:12:03 +00:00
|
|
|
# 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}"
|
|
|
|
}
|
|
|
|
|
2017-10-08 00:55:26 +00:00
|
|
|
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()
|
|
|
|
{
|
2017-10-08 19:10:56 +00:00
|
|
|
[ "$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
|
|
|
|
2017-10-20 10:23:02 +00:00
|
|
|
function prompt()
|
2017-10-08 09:46:51 +00:00
|
|
|
{
|
2017-10-20 10:23:02 +00:00
|
|
|
invalidcmd="somethingthatshouldbetotallynotacommandinanygivenenvironment"
|
2017-10-20 10:34:56 +00:00
|
|
|
expPS1=$(echo $invalidcmd |
|
|
|
|
bash --norc -i 2>&1 |
|
|
|
|
grep $invalidcmd |
|
|
|
|
head -n 1 |
|
2017-10-20 10:23:02 +00:00
|
|
|
sed "s/$invalidcmd//g")
|
|
|
|
printf "$expPS1"
|
2017-10-08 09:46:51 +00:00
|
|
|
}
|
2017-10-08 02:22:35 +00:00
|
|
|
|
2017-10-08 01:07:32 +00:00
|
|
|
function execute()
|
2017-10-08 01:44:43 +00:00
|
|
|
{
|
2017-10-08 02:22:35 +00:00
|
|
|
prompt
|
2017-10-08 01:44:43 +00:00
|
|
|
type "$@" && wait
|
2017-10-08 17:49:23 +00:00
|
|
|
printf "\n"
|
2017-10-08 01:44:43 +00:00
|
|
|
eval "$@"
|
|
|
|
}
|
2017-10-08 01:10:27 +00:00
|
|
|
|
|
|
|
function cmd()
|
2017-10-08 17:58:42 +00:00
|
|
|
{
|
2017-10-08 19:02:04 +00:00
|
|
|
while : ; do
|
|
|
|
prompt
|
2017-10-09 12:15:32 +00:00
|
|
|
read -e command
|
2017-10-08 19:02:04 +00:00
|
|
|
[ "$command" != "end" ] && eval "${command}" || break
|
2017-10-08 19:05:21 +00:00
|
|
|
[ "$1" ] && break
|
2017-10-08 19:02:04 +00:00
|
|
|
done
|
2017-10-08 17:58:42 +00:00
|
|
|
}
|