Skip to content

Commit

Permalink
bash3 support, Xcode bug fix & don't stall dumb terms.
Browse files Browse the repository at this point in the history
[FIXED]     bash 3 couldn't parse the file because of the coproc.
[FIXED]     Xcode has a weird bug where outputting :: X :: breaks it.  This happened with set -x on.
[FIXED]     pinf could hang some dumb terminals due to its spinner.
[FIXED]     removed several references to the removed getArgs.
  • Loading branch information
lhunath committed Nov 21, 2014
1 parent 7f52516 commit 57b00a4
Showing 1 changed file with 17 additions and 90 deletions.
107 changes: 17 additions & 90 deletions bash/bashlib/bashlib
Expand Up @@ -27,7 +27,7 @@

# ______________________________________________________________________
# | |
# | .:: TABLE OF CONTENTS ::. |
# | .: TABLE OF CONTENTS :. |
# |______________________________________________________________________|
#
# chr decimal
Expand Down Expand Up @@ -66,12 +66,6 @@
# readwhile command [args]
# Outputs the characters typed by the user into the terminal's input buffer while running the given command.
#
# pushqueue element ...
# Pushes the given arguments as elements onto the queue.
#
# popqueue
# Pops one element off the queue.
#
# log [format] [arguments...]
# Log an event at a certain importance level.
# The event is expressed as a printf(1) format argument.
Expand All @@ -97,9 +91,6 @@
# fsleep time
# Wait for the given (fractional) amount of seconds.
#
# getArgs [options] optstring [args...]
# Retrieve all options present in the given arguments.
#
# showHelp name description author [option description]...
# Generate a prettily formatted usage description of the application.
#
Expand Down Expand Up @@ -135,7 +126,7 @@ _tocHash=71e13f42e1ea82c1c7019b27a3bc71f3

# ______________________________________________________________________
# | |
# | .:: GLOBAL CONFIGURATION ::. |
# | .: GLOBAL CONFIGURATION :. |
# |______________________________________________________________________|

# Unset all exported functions. Exported functions are evil.
Expand Down Expand Up @@ -180,7 +171,7 @@ genToc() {

# ______________________________________________________________________
# | |
# | .:: GLOBAL DECLARATIONS ::. |
# | .: GLOBAL DECLARATIONS :. |
# |______________________________________________________________________|

# Variables for convenience sequences.
Expand All @@ -193,8 +184,8 @@ runner=( '> >' \

# Variables for terminal requests.
[[ -t 2 && $TERM != dumb ]] && {
COLUMNS=$( tput cols || tput co ) # Columns in a line
LINES=$( tput lines || tput li ) # Lines on screen
COLUMNS=$({ tput cols || tput co;} 2>&3) # Columns in a line
LINES=$({ tput lines || tput li;} 2>&3) # Lines on screen
alt=$( tput smcup || tput ti ) # Start alt display
ealt=$( tput rmcup || tput te ) # End alt display
hide=$( tput civis || tput vi ) # Hide cursor
Expand Down Expand Up @@ -233,15 +224,15 @@ runner=( '> >' \
tput eA; tput as;
tput ac; tput ae; } ) # Drawing characters
back=$'\b'
} 2>/dev/null ||:
} 3>&2 2>/dev/null ||:





# ______________________________________________________________________
# | |
# | .:: FUNCTION DECLARATIONS ::. |
# | .: FUNCTION DECLARATIONS :. |
# |______________________________________________________________________|


Expand Down Expand Up @@ -468,44 +459,6 @@ readwhile() {



# ___________________________________________________________________________
# |__ pushqueue ______________________________________________________________|
#
# pushqueue element ...
#
# Pushes the given arguments as elements onto the queue.
#
pushqueue() {
[[ $_queue ]] || {
coproc _queue {
while IFS= read -r -d ''; do
printf '%s\0' "$REPLY"
done
}
}

printf '%s\0' "$@" >&"${_queue[1]}"
} # _____________________________________________________________________



# __________________________________________________________________________
# |__ popqueue ______________________________________________________________|
#
# popqueue
#
# Pops one element off the queue.
# If no elements are available on the queue, this command fails with exit code 1.
#
popqueue() {
local REPLY
[[ $_queue ]] && read -t0 <&"${_queue[0]}" || return
IFS= read -r -d '' <&"${_queue[0]}"
printf %s "$REPLY"
} # _____________________________________________________________________



# ______________________________________________________________________
# |__ Latest ____________________________________________________________|
#
Expand Down Expand Up @@ -638,7 +591,7 @@ log() {
FTL) (( supported = 1 ))
logLevelColor=$_logFtlColor ;;
*)
log FTL "Log level %s does not exist" "$level"
log FTL 'Log level %s does not exist' "$level"
exit 1 ;;
esac
(( ! supported )) && return "$exitcode"
Expand All @@ -651,7 +604,7 @@ log() {
if (( _logColor )); then
colorFormat=$(sed ${reset:+-e "s/$(requote "$reset")/$reset$logColor/g"} -e "s/%[^a-z]*[a-z]/$reset$bold$logColor&$reset$logColor/g" <<< "$format")
colorArgs=("${args[@]//$reset/$reset$bold$logColor}")
printf -v conMsg "$reset[${date:+%s }$logColor%-3s$reset] $logColor$colorFormat$reset$black\$$reset$end$save" ${date:+"$date"} "$level" "${colorArgs[@]}"
printf -v conMsg "$reset[${date:+%s }$logColor$bold%-3s$reset] $logColor$colorFormat$reset$black\$$reset$end$save" ${date:+"$date"} "$level" "${colorArgs[@]}"
else
conMsg=$logMsg
fi
Expand Down Expand Up @@ -718,15 +671,15 @@ log() {
&& printf >> "$_logFile" '%s' "$logMsg"

# Start the spinner.
if [[ $type = startProgress && ! $_logSpinner ]]; then
if [[ $type = startProgress && ! $_logSpinner && $TERM != dumb ]]; then
{
set +m
trap 'printf %s "$show"' EXIT
printf %s "$hide"
while printf "$eel$blue$bold[$reset%s$reset$blue$bold]$reset\b\b\b" "${spinner[s++ % ${#spinner[@]}]}" && sleep .1
trap 'printf >&2 %s "$show"' EXIT
printf >&2 %s "$hide"
while printf >&2 "$eel$blue$bold[$reset%s$reset$blue$bold]$reset\b\b\b" "${spinner[s++ % ${#spinner[@]}]}" && sleep .1
do :; done
} & _logSpinner=$!
fi 2>/dev/null
fi

return $exitcode
}
Expand Down Expand Up @@ -788,12 +741,6 @@ _logTrcColor=$grey _logDbgColor=$blue _logInfColor=$white _logWrnColor=$yellow _
#
ask() {

# Check usage.
(( ! $# )) || getArgs -q :h "$@" && {
wrn 'Please specify a question as argument.'
return 1
}

# Initialize the vars.
local opt arg
local option=
Expand Down Expand Up @@ -830,7 +777,7 @@ ask() {

# Ask the question.
format=$1; shift
level=${level:-WRN} log -n "$format${option:+ [%s]}${options:+ [%s]} " "$@" ${option:+"$option"} ${options:+"$options"}
level=${level:-WRN} log -n "$format${option:+ [%s]}${options:+ [%s]}" "$@" ${option:+"$option"} ${options:+"$options"}

# Read the reply.
exec 8<&0; [[ -t 8 ]] || exec 8</dev/tty
Expand Down Expand Up @@ -1211,13 +1158,6 @@ options() {
#
showHelp() {

# Check usage.
(( $# < 3 )) || getArgs -q :h "$@" && {
wrn 'Please provide the name, description, author and options' \
'of the application.'
return 1
}

# Parse the options.
local appName=$1; shift
local appDesc=${1//+([[:space:]])/ }; shift
Expand Down Expand Up @@ -1340,12 +1280,6 @@ requote() {
#
shorten() {

# Check usage.
(( $# < 1 )) || getArgs -q :h "$@" && {
wrn 'Please provide the path to shorten.'
return 1
}

# Parse the options.
local suffix path pwd=$PWD
[[ $1 = -p ]] && { pwd=$2; shift 2; }
Expand Down Expand Up @@ -1459,13 +1393,6 @@ buildarray() {
#
inArray() {

# Check usage.
(( $# < 1 )) || getArgs -q :h "$@" && {
wrn 'Please provide the element to search for and the array' \
'to search through.'
return 1
}

# Parse the options.
local element
local search=$1; shift
Expand Down Expand Up @@ -1616,7 +1543,7 @@ stackTrace() {

# ______________________________________________________________________
# | |
# | .:: ENTRY POINT ::. |
# | .: ENTRY POINT :. |
# |______________________________________________________________________|

# Make sure this file is sourced and not executed.
Expand All @@ -1636,6 +1563,6 @@ stackTrace() {
}

:
: .:: END SOURCING ::.
: .: END SOURCING :.
: ______________________________________________________________________
:

0 comments on commit 57b00a4

Please sign in to comment.