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