From d0991b572bdda6069fabe6db825d48343f3be5ad Mon Sep 17 00:00:00 2001 From: Rob Pearce Date: Thu, 21 Apr 2022 18:02:05 +1000 Subject: [PATCH] fix csecho to not strip multuple spaces --- bashtools.sh | 73 ++++++++++++++++++++++++++++++++++------------------ test.sh | 6 +++++ 2 files changed, 54 insertions(+), 25 deletions(-) create mode 100755 test.sh diff --git a/bashtools.sh b/bashtools.sh index 80ac479..304bca7 100755 --- a/bashtools.sh +++ b/bashtools.sh @@ -1,26 +1,42 @@ #!/usr/bin/env /bin/bash -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; + +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 + 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() { diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..8ce0ef8 --- /dev/null +++ b/test.sh @@ -0,0 +1,6 @@ +#!/bin/bash +. ./bashtools.sh +notify "testing, you should see a spinner" +sleep 1 +ok +