fix csecho to not strip multuple spaces

This commit is contained in:
Rob Pearce 2022-04-21 18:02:05 +10:00
parent fdcd0eabdc
commit d0991b572b
2 changed files with 54 additions and 25 deletions

View File

@ -1,26 +1,42 @@
#!/usr/bin/env /bin/bash #!/usr/bin/env /bin/bash
true # need a successful command before the sourced script check
$(return 2>/dev/null) export BASHTOOLS_DIR="${HOME}/.bashtools"
rv="$?" if [[ $1 == "install" ]]; then
if [[ $rv -ne 0 ]]; then mkdir -p "$BASHTOOLS_DIR"
echo "ERROR: This script should be sourced ('. $0') rather than run directly." cp -a $0 "$BASHTOOLS_DIR/"
return 2>/dev/null # jist in case echo "Bashtools has been installed in $BASHTOOLS_DIR"
exit 1; $(return 2>/dev/null)
rv="$?"
if [[ $rv -ne 0 ]]; then
exit 1
else
return
fi
fi fi
#true # need a successful command before the sourced script check
#$(return 2>/dev/null)
#rv="$?"
#if [[ $rv -ne 0 ]]; then
# echo "ERROR: This script should be sourced ('. $0') rather than run directly."
# return 2>/dev/null # jist in case
# exit 1;
#fi
if [[ $1 != "reload" && -n $HAVE_BASHTOOLS ]]; then if [[ $1 != "reload" && -n $HAVE_BASHTOOLS ]]; then
return return
fi fi
export HAVE_BASHTOOLS=1 export HAVE_BASHTOOLS=1
export BASHTOOLS_DIR="${HOME}/.bashtools" SPINDELAY=0.05 # delay in ms between spinner frames
SPINNERFRAMES='|/-\' SPINNERFRAMES='|/-\'
SPINNERFILE="$BASHTOOLS_DIR/spinner.pid" SPINNERFILE="$BASHTOOLS_DIR/spinner.pid"
[[ ! -e $BASHTOOLS_DIR ]] && mkdir "$BASHTOOLS_DIR" [[ ! -e $BASHTOOLS_DIR ]] && mkdir "$BASHTOOLS_DIR"
[[ -e $SPINNERFILE ]] && rm -f "${SPINNERFILE}" [[ -e $SPINNERFILE ]] && rm -f "${SPINNERFILE}"
export BOLD="\033[1m" export BOLD="\033[1m"
export PLAIN="\033[0m" export PLAIN="\033[0m"
export ITALIC="\033[3m" export ITALIC="\033[3m"
@ -93,27 +109,34 @@ function cecho() { # [-n] [-s] col boldcol "str"
# use bold version of supplied colour # use bold version of supplied colour
function csecho() { # [-n] col "str" function csecho() { # [-n] col "str"
cecho -s $* local args col
while [[ $# -gt 2 ]]; do
args="$args $1"
shift
done
col="$1"
shift
cecho $args -s "$col" "$*"
} }
alias try=notify alias try=notify
function notify() { function notify() {
#echo -en "${NOTIFYCOLB}* ${NOTIFYCOL}$*...${PLAIN} " #echo -en "${NOTIFYCOLB}* ${NOTIFYCOL}$*...${PLAIN} "
cecho -n "$NOTIFYCOL" "$NOTIFYCOLB" "^b* ^p$*... " cecho -n "$NOTIFYCOL" "$NOTIFYCOLB" "^b* ^p$*... " >/dev/stderr
if [[ $NOSPINNER -ne 1 ]]; then if [[ $NOSPINNER -ne 1 ]]; then
start_spinner & start_spinner &
tput civis tput civis >/dev/stderr
fi fi
innotify=1 innotify=1
} }
function inform() { function inform() {
cecho "$INFORMCOL" "$INFORMCOLB" "^b* ^p$* " cecho "$INFORMCOL" "$INFORMCOLB" "^b* ^p$* " >/dev/stderr
} }
function info() { function info() {
[[ $AUTOYES -eq 1 ]] && return [[ $AUTOYES -eq 1 ]] && return
csecho "${INFOCOL}" "^b>>^p $*" csecho "${INFOCOL}" "^b>>^p $*" >/dev/stderr
} }
function ok() { function ok() {
@ -121,7 +144,7 @@ function ok() {
stop_spinner stop_spinner
[[ $innotify -eq 0 ]] && return 1 [[ $innotify -eq 0 ]] && return 1
innotify=0 innotify=0
echo -e "$GREEN$msg$PLAIN" echo -e "$GREEN$msg$PLAIN" >/dev/stderr
} }
function fail() { function fail() {
@ -129,23 +152,23 @@ function fail() {
stop_spinner stop_spinner
[[ $innotify -eq 0 ]] && return 1 [[ $innotify -eq 0 ]] && return 1
innotify=0 innotify=0
echo -e "$RED$msg$PLAIN" echo -e "$RED$msg$PLAIN" >/dev/stderr
} }
function partial() { function partial() {
local msg=${*:-failed} local msg=${*:-partial}
stop_spinner stop_spinner
[[ $innotify -eq 0 ]] && return 1 [[ $innotify -eq 0 ]] && return 1
innotify=0 innotify=0
echo -e "$YELLOW$msg$PLAIN" echo -e "$YELLOW$msg$PLAIN" >/dev/stderr
} }
function warn() { function warn() {
cecho -s "$YELLOW" "^bWarning: ^p$*" cecho -s "$YELLOW" "^bWarning: ^p$*" >/dev/stderr
} }
function error() { function error() {
cecho -s "$RED" "^bERROR: ^p$*" cecho -s "$RED" "^bERROR: ^p$*" >/dev/stderr
} }
function start_spinner() { function start_spinner() {
@ -153,14 +176,14 @@ function start_spinner() {
( (
len=${#SPINNERFRAMES} len=${#SPINNERFRAMES}
echo -n " " echo -n " " >/dev/stderr
while [ 1 ] ; do while [ 1 ] ; do
echo -en "\b${SPINNERFRAMES:$idx:1}" echo -en "\b${SPINNERFRAMES:$idx:1}" >/dev/stderr
idx=$((idx + 1)) idx=$((idx + 1))
[[ $idx -ge $len ]] && idx=0 [[ $idx -ge $len ]] && idx=0
sleep 0.1 sleep $SPINDELAY
done done
) 2>/dev/null & ) &
spid=$! spid=$!
echo $spid >>"$SPINNERFILE" echo $spid >>"$SPINNERFILE"
} }
@ -179,7 +202,7 @@ function stop_spinner() {
fi fi
done < "$SPINNERFILE" done < "$SPINNERFILE"
rm -f "$SPINNERFILE" rm -f "$SPINNERFILE"
tput cnorm tput cnorm >/dev/stderr
} }
function getsysstats() { function getsysstats() {

6
test.sh Executable file
View File

@ -0,0 +1,6 @@
#!/bin/bash
. ./bashtools.sh
notify "testing, you should see a spinner"
sleep 1
ok