Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- ############################################################ GLOBALS
- #
- # Global exit status variables
- #
- export SUCCESS=0
- export FAILURE=1
- # Status indicator message formats for task_begin/task_end
- export FMT_COLS=70
- export FMT_HEAD="%-${FMT_COLS}s"
- export FMT_STAT=" [$ANSI_BLD%s$ANSI_BLD_OFF]\n"
- export MSG_GOOD="${ANSI_GRN} OK $ANSI_NRM"
- export MSG_FAIL="${ANSI_RED}FAILED$ANSI_NRM"
- __cp=0 # reserved for internal use
- ############################################################ FUNCTIONS
- # strip_ansi $text ...
- #
- # Remove ANSI coloring from string arguments or stdin.
- #
- strip_ansi()
- {
- if [ $# -gt 0 ]; then
- echo "$@" | sed -e 's/^[[^m]*m//g'
- else
- sed -e 's/^[[^m]*m//g'
- fi
- }
- # task_begin [ $caption ]
- #
- # Inform the user that you have started a task.
- #
- task_begin()
- {
- local caption="$1"
- local colorless_caption="$( strip_ansi "$caption" )"
- task_end $SUCCESS # end the previous task
- # Padded strings in printf (e.g., "%-70s") do not work properly if the
- # string contains invisibles, such as ANSI color codes. Compensate for
- # ANSI color codes if any.
- #
- local color_gap=$(( ${#caption} - ${#colorless_caption} ))
- local FMT_COLS2=$(( $FMT_COLS + $color_gap ))
- printf "%-${FMT_COLS2}s" "$caption"
- export __task_running=YES
- export __cp=${#colorless_caption}
- }
- # task_end [ $exit_status ]
- #
- # End the currently running task (if any) and display the given exit status.
- #
- task_end()
- {
- local status="$1"
- [ "$__task_running" ] || return # nothing to do
- if [ "$status" ]; then
- #
- # If the caption last-used with task_begin is wider than our
- # preset-limit, output a new header with blank caption, vert-
- # ically aligning the cursor to the column where we need to
- # print the status.
- #
- if [ ${__cp:-0} -ge $FMT_COLS ]; then
- printf "\n$FMT_HEAD"
- export __cp=0
- fi
- case "$status" in
- $SUCCESS) printf "$FMT_STAT" "$MSG_GOOD";;
- *) printf "$FMT_STAT" "$MSG_FAIL";;
- esac
- fi
- export __task_running= # clear task state
- }
- # spinner
- #
- # Throw up a spinner. Spin spin spin until we receive "EXIT:###:" on stdin.
- # Returns the exit status as specified in the "###" portion above.
- #
- spinner()
- {
- local spin="/-\\|"
- local init=${__task_running:+1} n=1 done=
- local line_format="^[[A\n%s^[[K\n${__task_running:+$FMT_HEAD }"
- printf " "
- while [ ! "$DONE" ]; do
- local LINE="$( /bin/sh -c 'IFS=;read -t0 L;echo "$L"' )"
- case "$LINE" in
- *EXIT:*:*)
- DONE="$( echo "$LINE" | sed -e \
- "s/.*EXIT:\([[:digit:]]*\):.*/\1/" )"
- LINE="$( echo "$LINE" | sed -e \
- "s/EXIT:[[:digit:]]*://g" )"
- esac
- if [ "$LINE" ]; then
- printf "${init:+\b \n}$line_format" "$LINE"
- init=
- fi
- printf "\b%s" "$( echo "$spin" | sed -e \
- "s/.\{0,$(( $n % ${#spin} ))\}\(.\).*/\1/" )"
- n=$(( $n + 1 ))
- done
- printf "\b \b"
- return $DONE
- }
- # eval_spin $command [ $args ... ]
- #
- # Execute a command while displaying a rotating spinner.
- #
- eval_spin()
- {
- #
- # Execute the command w/ spinner
- #
- (
- if [ $# -gt 0 ]; then
- # Take commands from positional arguments
- ( eval "$@" ) 2>&1
- echo EXIT:$?:
- else
- # Take commands from standard input
- ( eval "$( cat )" ) 2>&1
- echo EXIT:$?:
- fi
- ) | spinner
- }
- ############################################################ MAIN SOURCE
- task_begin "Some task header..."
- eval_spin "$@"
- task_end $?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement