github.com/SamarSidharth/kpt@v0.0.0-20231122062228-c7d747ae3ace/demos/demo-magic/demo-magic.sh (about) 1 #!/usr/bin/env bash 2 3 ############################################################################### 4 # 5 # demo-magic.sh 6 # 7 # Copyright (c) 2015 Paxton Hare 8 # 9 # This script lets you script demos in bash. It runs through your demo script when you press 10 # ENTER. It simulates typing and runs commands. 11 # 12 ############################################################################### 13 14 # the speed to "type" the text 15 TYPE_SPEED=20 16 17 # no wait after "p" or "pe" 18 NO_WAIT=false 19 20 # if > 0, will pause for this amount of seconds before automatically proceeding with any p or pe 21 PROMPT_TIMEOUT=${PROMPT_TIMEOUT:-1} 22 23 # don't show command number unless user specifies it 24 SHOW_CMD_NUMS=false 25 26 27 # handy color vars for pretty prompts 28 BLACK="\033[0;30m" 29 BLUE="\033[0;34m" 30 GREEN="\033[0;32m" 31 GREY="\033[0;90m" 32 CYAN="\033[0;36m" 33 RED="\033[0;31m" 34 PURPLE="\033[0;35m" 35 BROWN="\033[0;33m" 36 WHITE="\033[1;37m" 37 COLOR_RESET="\033[0m" 38 39 C_NUM=0 40 41 # prompt and command color which can be overriden 42 DEMO_PROMPT="$ " 43 DEMO_CMD_COLOR=$WHITE 44 DEMO_COMMENT_COLOR=$CYAN 45 46 ## 47 # prints the script usage 48 ## 49 function usage() { 50 echo -e "" 51 echo -e "Usage: $0 [options]" 52 echo -e "" 53 echo -e "\tWhere options is one or more of:" 54 echo -e "\t-h\tPrints Help text" 55 echo -e "\t-d\tDebug mode. Disables simulated typing" 56 echo -e "\t-n\tNo wait" 57 echo -e "\t-w\tWaits max the given amount of seconds before proceeding with demo (e.g. '-w5')" 58 echo -e "" 59 } 60 61 ## 62 # wait for user to press ENTER 63 # if $PROMPT_TIMEOUT > 0 this will be used as the max time for proceeding automatically 64 ## 65 function wait() { 66 if [[ "$PROMPT_TIMEOUT" == "0" ]]; then 67 read -rs 68 else 69 read -rst "$PROMPT_TIMEOUT" 70 fi 71 } 72 73 ## 74 # print command only. Useful for when you want to pretend to run a command 75 # 76 # takes 1 param - the string command to print 77 # 78 # usage: p "ls -l" 79 # 80 ## 81 function p() { 82 if [[ ${1:0:1} == "#" ]]; then 83 cmd=$DEMO_COMMENT_COLOR$1$COLOR_RESET 84 else 85 cmd=$DEMO_CMD_COLOR$1$COLOR_RESET 86 fi 87 88 # render the prompt 89 x=$(PS1="$DEMO_PROMPT" "$BASH" --norc -i </dev/null 2>&1 | sed -n '${s/^\(.*\)exit$/\1/p;}') 90 91 # show command number is selected 92 if $SHOW_CMD_NUMS; then 93 printf "[$((++C_NUM))] $x" 94 else 95 printf "$x" 96 fi 97 98 # wait for the user to press a key before typing the command 99 if !($NO_WAIT); then 100 wait 101 fi 102 103 if [[ -z $TYPE_SPEED ]]; then 104 echo -en "$cmd" 105 else 106 echo -en "$cmd" | pv -qL $[$TYPE_SPEED+(-2 + RANDOM%5)]; 107 fi 108 109 # wait for the user to press a key before moving on 110 if !($NO_WAIT); then 111 wait 112 fi 113 echo "" 114 } 115 116 ## 117 # Prints and executes a command 118 # 119 # takes 1 parameter - the string command to run 120 # 121 # usage: pe "ls -l" 122 # 123 ## 124 function pe() { 125 # print the command 126 p "$@" 127 128 # execute the command 129 eval "$@" 130 } 131 132 ## 133 # Enters script into interactive mode 134 # 135 # and allows newly typed commands to be executed within the script 136 # 137 # usage : cmd 138 # 139 ## 140 function cmd() { 141 # render the prompt 142 x=$(PS1="$DEMO_PROMPT" "$BASH" --norc -i </dev/null 2>&1 | sed -n '${s/^\(.*\)exit$/\1/p;}') 143 printf "$x\033[0m" 144 read command 145 eval "${command}" 146 } 147 148 149 function check_pv() { 150 command -v pv >/dev/null 2>&1 || { 151 152 echo "" 153 echo -e "${RED}##############################################################" 154 echo "# HOLD IT!! I require pv but it's not installed. Aborting." >&2; 155 echo -e "${RED}##############################################################" 156 echo "" 157 echo -e "${COLOR_RESET}Installing pv:" 158 echo "" 159 echo -e "${BLUE}Mac:${COLOR_RESET} $ brew install pv" 160 echo "" 161 echo -e "${BLUE}Other:${COLOR_RESET} http://www.ivarch.com/programs/pv.shtml" 162 echo -e "${COLOR_RESET}" 163 exit 1; 164 } 165 } 166 167 check_pv 168 # 169 # handle some default params 170 # -h for help 171 # -d for disabling simulated typing 172 # 173 while getopts ":dhncw:" opt; do 174 case $opt in 175 h) 176 usage 177 exit 1 178 ;; 179 d) 180 unset TYPE_SPEED 181 ;; 182 n) 183 NO_WAIT=true 184 ;; 185 c) 186 SHOW_CMD_NUMS=true 187 ;; 188 w) 189 PROMPT_TIMEOUT=$OPTARG 190 ;; 191 esac 192 done