source: common/progress_bar.sh@ 52cced4

ablfs-more legacy trunk
Last change on this file since 52cced4 was bc2f591, checked in by Pierre Labastie <pierre@…>, 6 years ago

Improve common/progress_bar.sh based on shellcheck:

  • remove useless array declarations
  • remove unused variables
  • remove $ prefix for variable names in arithmetic expressions
  • normalize indentation (2 spaces)
  • Property mode set to 100644
File size: 1.8 KB
RevLine 
[bc2f591]1# shellcheck shell=bash
[a858a78]2# $Id$
3
4set -e
5
[ea81eb2]6# Be sure that we know the target name
[a36805d]7[[ -z $1 ]] && exit
[843c479]8TARGET=$1 # Remember the target build we are looking for
[d44206f]9MAKE_PPID=$2
[a36805d]10
[843c479]11declare -r CSI=$'\e[' # DEC terminology, Control Sequence Introducer
12declare -r CURSOR_OFF=${CSI}$'?25l'
13declare -r CURSOR_ON=${CSI}$'?25h'
14declare -r ERASE_LINE=${CSI}$'2K'
15declare -r FRAME_OPEN=${CSI}$'2G['
16declare -r FRAME_CLOSE=${CSI}$'63G]'
17declare -r TS_POSITION=${CSI}$'65G'
[bc2f591]18declare -r RESET_LINE=${CURSOR_OFF}${ERASE_LINE}${FRAME_OPEN}${FRAME_CLOSE}
[843c479]19
[bc2f591]20declare -r GRAPHIC_STR="| / - \\ + "
[843c479]21declare -i SEC=0 # Seconds accumulator
[ddf5f77]22declare -i PREV_SEC=0
23
[b0ba1f0]24# Prevent segfault on stripping phases
[c180dec]25if [[ "$BASHBIN" = "/tools/bin/bash" ]] ; then
[b0ba1f0]26 SLEEP=/tools/bin/sleep
[ea81eb2]27elif [ -x /bin/sleep ] ; then
[c180dec]28 SLEEP=/bin/sleep
[ea81eb2]29else
30 SLEEP=/usr/bin/sleep
[b0ba1f0]31fi
32
[843c479]33write_or_exit() {
[7dd28ea]34 # make has been killed or failed or run to completion, leave
[d44206f]35 [[ ! -e /proc/${MAKE_PPID} ]] && echo -n "${CURSOR_ON}" && exit
[041f2ed]36
[e2ef100]37 # Target build complete, leave.
38 [[ -f ${TARGET} ]] && echo -n "${CURSOR_ON}" && exit
[041f2ed]39
[843c479]40 # It is safe to write to the screen
41 echo -n "$1"
42}
43
[ddf5f77]44 # initialize screen
[045b2dc]45write_or_exit "${RESET_LINE}${TS_POSITION}0 min. 0 sec"
[ddf5f77]46
47 # loop forever..
48while true ; do
49
[bc2f591]50 # Loop through the animation string
51 for GRAPHIC_CHAR in ${GRAPHIC_STR} ; do
52 write_or_exit "${CSI}$((SEC + 3))G${GRAPHIC_CHAR}"
53 $SLEEP .12 # This value MUST be less than .2 seconds.
54 done
[ddf5f77]55
[bc2f591]56 # A BASH internal variable, the number of seconds the script
57 # has been running. modulo convert to 0-59
58 SEC=$((SECONDS % 60))
[ddf5f77]59
[bc2f591]60 # Detect rollover of the seconds.
61 (( PREV_SEC > SEC )) && write_or_exit "${RESET_LINE}"
62 PREV_SEC=$SEC
[ddf5f77]63
[bc2f591]64 # Display the accumulated time. div minutes.. modulo seconds.
65 write_or_exit "${TS_POSITION}$((SECONDS / 60)) min. $SEC sec"
[843c479]66done
67
[ddf5f77]68exit
Note: See TracBrowser for help on using the repository browser.