Stripping tput off when terminal is not xterm

This commit is contained in:
DarkFeather 2023-01-25 22:44:05 -06:00
parent 16c864e161
commit 8435ff442b
Signed by: DarkFeather
GPG Key ID: 1CC1E3F4ED06F296

View File

@ -6,13 +6,25 @@ pullcmd='wget --timeout=5 -q -O -'
## Visual Functions ## ## Visual Functions ##
# These function creates a visual indicator that a step has happened. # These function creates a visual indicator that a step has happened.
function header () { function header () {
tput setaf 1; tput bold; echo $@; tput sgr0; return if [[ "$TERM" =~ xterm* ]]; then
tput setaf 1; tput bold; echo $@; tput sgr0; return
else
echo $@;
fi
} }
function errorheader () { function errorheader () {
tput setaf 1 1>&2; tput bold 1>&2; echo "ERROR:" $@ 1>&2; tput sgr0 1>&2; return if [[ "$TERM" =~ xterm* ]]; then
tput setaf 1 1>&2; tput bold 1>&2; echo "ERROR:" $@ 1>&2; tput sgr0 1>&2; return
else
echo $@;
fi
} }
function infoheader() { function infoheader() {
tput setaf 3; tput bold; echo $@; tput sgr0; return if [[ "$TERM" =~ xterm* ]]; then
tput setaf 3; tput bold; echo $@; tput sgr0; return
else
echo $@;
fi
} }
function colorstrip() { function colorstrip() {
perl -e 'use strict; use warnings; while(<>) { s/\e\[[\d;]*m//g; print; }' perl -e 'use strict; use warnings; while(<>) { s/\e\[[\d;]*m//g; print; }'
@ -116,14 +128,14 @@ function qaunittest {
$(echo -ne $userinput) $(echo -ne $userinput)
EOM EOM
`" `"
printf "$header"; printf "$header";
printf ":%*s" $(($qapadding - $(echo $header | wc -c) - 1)) '[' printf ":%*s" $(($qapadding - $(echo $header | wc -c) - 1)) '['
if [ "$actualreturn" == "$expectedreturn" ]; then if [ "$actualreturn" == "$expectedreturn" ]; then
tput setaf 2; printf "PASS"; tput sgr0; printf ']\n'; tput setaf 2; printf "PASS"; tput sgr0; printf ']\n';
else else
tput setaf 1; printf "FAIL"; tput sgr0; printf '] -- found %s (expected %s) running '\''%s %s < "%s"'\''\n' "$actualreturn" "$expectedreturn" "$function" "$fnargs" "$userinput"; tput setaf 1; printf "FAIL"; tput sgr0; printf '] -- found %s (expected %s) running '\''%s %s < "%s"'\''\n' "$actualreturn" "$expectedreturn" "$function" "$fnargs" "$userinput";
fi fi
return return
} }
## Prompt functions ## ## Prompt functions ##
@ -133,73 +145,73 @@ EOM
export maxattempts=5 export maxattempts=5
# This function will prompt the user for variable 1 with string 2 until it gets a nonempty answer. # This function will prompt the user for variable 1 with string 2 until it gets a nonempty answer.
function prompt-var() { function prompt-var() {
if [ -z "$1" ] || [ -z "$2" ]; then if [ -z "$1" ] || [ -z "$2" ]; then
echo "Need a variable name (1) and a prompt string (2)" echo "Need a variable name (1) and a prompt string (2)"
return 1; return 1;
fi fi
inputtime="0" inputtime="0"
#echo "$0 is checking for $1 -- currently set to <${!1}>" #echo "$0 is checking for $1 -- currently set to <${!1}>"
while [ -z "${!1}" ] || (echo "${!1}" | grep -v -E '^[-0-9a-zA-Z ,.@]{1,50}$' &>/dev/null); do while [ -z "${!1}" ] || (echo "${!1}" | grep -v -E '^[-0-9a-zA-Z ,.@]{1,50}$' &>/dev/null); do
printf "%s: " "$2"; printf "%s: " "$2";
read $1; read $1;
if [ "${!1}" == "OPT" ] || [ $inputtime -gt $maxattempts ]; then exit 0; fi if [ "${!1}" == "OPT" ] || [ $inputtime -gt $maxattempts ]; then exit 0; fi
inputtime=$(($inputtime + 1)) inputtime=$(($inputtime + 1))
done done
return 0; return 0;
} }
# This function will prompt the user for variable 1 with string 2 until it gets a nonempty answer. # This function will prompt the user for variable 1 with string 2 until it gets a nonempty answer.
function prompt-var-len() { function prompt-var-len() {
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then
echo "Need a variable name (1), prompt string (2), and a length (3)" echo "Need a variable name (1), prompt string (2), and a length (3)"
return 1; return 1;
fi fi
inputtime="0" inputtime="0"
#echo "$0 is checking for $1 -- currently set to <${!1}>" #echo "$0 is checking for $1 -- currently set to <${!1}>"
while [ -z "${!1}" ] || (echo "${!1}" | grep -v -E "^[0-9a-zA-Z]{1,$3}$" &>/dev/null); do while [ -z "${!1}" ] || (echo "${!1}" | grep -v -E "^[0-9a-zA-Z]{1,$3}$" &>/dev/null); do
printf "%s (Max length: %s): " "$2" "$3"; printf "%s (Max length: %s): " "$2" "$3";
read $1; read $1;
if [ "${!1}" == "OPT" ] || [ $inputtime -gt $maxattempts ]; then exit 0; fi if [ "${!1}" == "OPT" ] || [ $inputtime -gt $maxattempts ]; then exit 0; fi
inputtime=$(($inputtime + 1)) inputtime=$(($inputtime + 1))
done done
return 0; return 0;
} }
# This function will prompt the user for variable 1 with string 2 until it gets a nonempty answer. # This function will prompt the user for variable 1 with string 2 until it gets a nonempty answer.
function prompt-password() { function prompt-password() {
if [ -z "$1" ] || [ -z "$2" ]; then if [ -z "$1" ] || [ -z "$2" ]; then
echo "Need a variable name (1) and a prompt string (2)" echo "Need a variable name (1) and a prompt string (2)"
return 1; return 1;
fi fi
inputtime="0" inputtime="0"
#echo "$0 is checking for $1 -- currently set to <${!1}>" #echo "$0 is checking for $1 -- currently set to <${!1}>"
while [ -z "${!1}" ] || [ $(echo ${!1} | wc -c) -gt 50 ]; do while [ -z "${!1}" ] || [ $(echo ${!1} | wc -c) -gt 50 ]; do
printf "%s: " "$2"; printf "%s: " "$2";
read -s $1; read -s $1;
if [ "${!1}" == "OPT" ] || [ $inputtime -gt $maxattempts ]; then exit 0; fi if [ "${!1}" == "OPT" ] || [ $inputtime -gt $maxattempts ]; then exit 0; fi
echo echo
inputtime=$(($inputtime + 1)) inputtime=$(($inputtime + 1))
done done
return 0; return 0;
} }
#This function will prompt the user for a variable 1 with string 2 until it gets an answer between min 3 and max 4. #This function will prompt the user for a variable 1 with string 2 until it gets an answer between min 3 and max 4.
function prompt-num-var() { function prompt-num-var() {
if [ -z "$1" ] || [ -z "$2" ] || [ "$3" -ne "$3" ] || [ "$4" -le "$3" ]; then if [ -z "$1" ] || [ -z "$2" ] || [ "$3" -ne "$3" ] || [ "$4" -le "$3" ]; then
echo "Need a variable name (1), prompt string (2), min (3), and max (4)"; echo "Need a variable name (1), prompt string (2), min (3), and max (4)";
echo 1: $1; echo 1: $1;
echo 2: $2; echo 2: $2;
echo 3: $3; echo 3: $3;
echo 4: $4; echo 4: $4;
return 1; return 1;
fi fi
inputtime="0" inputtime="0"
#echo "$0 is checking for $1 -- currently set to <${!1}>" #echo "$0 is checking for $1 -- currently set to <${!1}>"
while [ -z "${!1}" ] || (echo "${!1}" | grep -v -E "^[0-9]{1,8}$" &>/dev/null) || [ "${!1}" -lt "$3" ] || [ "${!1}" -gt "$4" ]; do while [ -z "${!1}" ] || (echo "${!1}" | grep -v -E "^[0-9]{1,8}$" &>/dev/null) || [ "${!1}" -lt "$3" ] || [ "${!1}" -gt "$4" ]; do
printf "%s (between %s and %s): " "$2" "$3" "$4"; printf "%s (between %s and %s): " "$2" "$3" "$4";
read $1; read $1;
if [ "${!1}" == "OPT" ] || [ $inputtime -gt $maxattempts ]; then exit 0; fi if [ "${!1}" == "OPT" ] || [ $inputtime -gt $maxattempts ]; then exit 0; fi
inputtime=$(($inputtime + 1)) inputtime=$(($inputtime + 1))
done done
return 0; return 0;
} }
## Torrenting functions ## ## Torrenting functions ##