Skip to content

Instantly share code, notes, and snippets.

@Jeff-Russ
Last active May 26, 2016 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jeff-Russ/036d13e247aa140751268f115ea6d0f1 to your computer and use it in GitHub Desktop.
Save Jeff-Russ/036d13e247aa140751268f115ea6d0f1 to your computer and use it in GitHub Desktop.

Shell lib: String I/O Functions


fancyprompt for bash

A fancy 'are you sure' style shell prompt

fancyprompt take at least two args, the prompt message and produces an 'are you
sure' style prompt with a timeout of 10 sec and option to cancel with 'c'.
The first arg is always the message as a string (accepts \n \t etc). And the
remaining args are function names that are called depending on the result.

  • when 2 args:

    • arg2 is function called for non-cancels ('c' is not received)
  • when 3 args:

    • arg2 is function called for non-cancels that timeout or [enter] is hit
    • arg2 is function called for non-cancels that have a custom reply
  • when 4 args:

    • arg2 is function called for non-cancels that timeout or [enter] is hit
    • arg2 is function called for cancels ('c' is received)

functions will receive the reply as their argument only when relevent.

TIP 1: You don't want arg 3 but you want arg 4, put the same for arg 2 and 3

__TIP 2: TO abort script for 'c', just put exit as last arg!

TIP 3: Place dummies (empty strings) for args. See demo!

TIP 4: uncomment the last line and run this file


Prompt With Timeout

Default timeout is 10 second which can be changed with a third argument.

NOTE
The read reply must be outside the function or else the prompt won't print until after reply (or timeout).

BEWARE
the prompt_w_timeout function uses a global variable $reply which is emptied at start of execution.
This is so you can have the read outside of the function.

IMPORTANT
Arg 1 takes a string and accepts \n so you'll likely feed it a variable. Be sure to wrap it like this

"$variable"

IN QUOTES or it won't work.

BUT ACTUALLY...
You don't even need to give it any args if you create a variable called $prompt. It will use that if no $1

Usage Example:

prompt="Proceeding to kill you in 10 seconds\nType 'c' and enter to live "
prompt="${prompt}'d' and enter to defend \n or just hit enter to die now"

prompt_w_timeout
read reply

if [[ $reply == 'c' ]]; then 
  echo "You shall live."
elif [[ "$reply" == 'd' ]]; then
  echo "You live. I dead."
else
  echo "You die"
fi

Attention Grabbers

print some ascii dividers without cluttering your scripts. output:

###############################################################
######## ATTENTION BAR

stuff is happening

=============================================================
======== ALERT PIPE

stuff is happening

___________________________________________________________
________ hey... just a line

stuff happens
#!/bin/sh
# fancyprompt take at least two args, the prompt message and produces an 'are you
# sure' style prompt with a timeout of 10 sec and option to cancel with 'c'.
# The first arg is always the message as a string (accepts \n \t etc). And the
# remaining args are function names that are called depending on the result.
# when 2 args:
# arg2 is function called for non-cancels ('c' is not received)
# when 3 args:
# arg2 is function called for non-cancels that timeout or [enter] is hit
# arg2 is function called for non-cancels that have a custom reply
# when 4 args:
# arg2 is function called for non-cancels that timeout or [enter] is hit
# arg2 is function called for cancels ('c' is received)
# functions will receive the reply as their argument only when relevent.
# TIP 1: You don't want arg 3 but you want arg 4, put the same for arg 2 and 3
# TIP 2: TO abort script for 'c', just put exit as last arg!
# TIP 3: Place dummies (empty strings) for args. See demo!
# TIP 4: uncomment the last line and run this file
########## A TEST/DEMO ########################################################
fancyprompt () {
if [[ -z $fp_color ]]; then fp_color=33; fi
local beg="\033[$fp_color;1m"; local end="\033[0m\n"
printf "\033[$fp_color;5;7m\t____/ attention \____\t\t$end"
printf "${beg}${1}${end}"
printf "$beg - 'c'\t\tto skip or\n - [return]\tto continue$end > "
fp_color=33;
local reply=''
if [[ -z $TMOUT ]]; then TMOUT=10; fi
read reply
if [ "$#" -eq 2 ]; then
if [[ "$reply" != 'c' ]]; then $2 $reply # not 'c'. default or custom
fi
elif [ "$#" -eq 3 ]; then
if [[ -z $reply ]]; then $2 # default (timeout or [enter])
elif [[ "$reply" != 'c' ]]; then $3 $reply # not 'c' with custom reply
fi
elif [ "$#" -eq 4 ]; then
if [[ -z $reply ]]; then $2 # default (timeout or [enter])
elif [[ "$reply" != 'c' ]]; then $3 $reply # not 'c' with custom reply
else $4 # reply was 'c'
fi
fi
}
########## BONUS FUNC'S #######################################
fancyfail () {
if [[ -z $fail_color ]]; then fail_color=31; fi
local beg="\033[$fail_color;3m"; local end="\033[0m\n"
printf "\033[$fail_color;1;7m\t____/ attention \____\t\t$end"
printf "${beg}${1}${end}"
}
########## functions used by test/demo #######################################
function not_canceled {
arg=${1:-"no arg"}
echo "not_canceled called with: $arg"
}
function default_action {
echo "default_action gets no arg"
}
function custom_reply {
echo "custom_reply called with: $1"
}
function cancel_action {
echo "cancel_action gets no arg"
}
# TMOUT=1; # uncommenting this will override the 10 second default
# runs the whole thing:
# main
#!/bin/sh
function prompt_w_timeout {
prompt=${3:-$prompt} # use global $prompt if no $1
reply=''
TMOUT=${3:-10} # use default of 10 seconds if no $2
echo `printf "\033[33;5;7m ____/-- ATTENTION --\____ \033[0m"`
printf "\n$prompt\n"
prompt='' # clear prompt to avoid concating later by accident
# you must put `read reply` after calling this function!
}
function bar {
echo
echo '###############################################################'
echo "######## $1"; echo
}
function pipe {
echo
echo '============================================================='
echo "======== $1"; echo
}
function line {
echo
echo '___________________________________________________________'
cprintf () {
STR="$3"
for i in ${@:4}; do
STR="$STR $i"
done
STR=${STR//\\s/ } # replace \s with ' '
printf "\033[$1m\033[$2m${STR}\033[0m"
}
cecho () {
STR="$3"
for i in ${@:4}; do
STR="$STR $i"
done
STR=${STR//\\s/ } # replace \s with ' '
echo "\033[$1m\033[$2m${STR}\033[0m"
}
echo "________ $1"; echo
}
bar "ATTENTION BAR"
echo "stuff is happening"
pipe "ALERT PIPE"
echo "stuff is happening"
line "hey... just a line"
echo "stuff happens"
@Jeff-Russ
Copy link
Author

note to self: "shellshots"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment