progress_bar with percentage progress

linux fan linuxscratch at gmail.com
Mon Jul 13 21:21:47 PDT 2009


This patch applied to the progress_bar with percentage progress
posted earlier, uses a pound sign # instead of the spinner and
looks good with sleeps of one second or more (even 5 seconds).
Some modifications make the action a little smoother,
and can select any sleep period including fractions.
Also, makes cursory effort to take into account those few SBU estimates
that claim hundredths of an SBU accuracy.
At the end of the day, if it just printed the minutes, seconds and
percentage every few seconds, that might be enough.

*** progress_bar.sh.posted	2009-07-12 12:11:03.000000000 -0400
--- progress_bar.sh	2009-07-13 23:36:14.000000000 -0400
***************
*** 16,18 ****
  #
! # Examples of valid #SBUs: 1, 1.5, 0.1, .5
  #
--- 16,18 ----
  #
! # Examples of valid #SBUs: 1, 1.5, 0.1, .5, 4.09
  #
***************
*** 22,32 ****
  #
! # Anything supplied in $5 makes it slowpoke -- at least 1 second sleep
! # If a number is supplied in $5 -- that many seconds sleep
  # e.g., sh progress_bar.sh foo $PPID .1 540 slow (1 second sleep)
  # e.g., sh progress_bar.sh foo $PPID .1 540 5 (5 second sleep)
  #
! # Note: slowpoke
! #       There are 5 graphic characters per cycle and it sleeps on each one.
! #       e.g., if 1 second sleep, then 1 second between tests for exit,
! #       and 5 seconds between position jumps and time/percentage updates.
  #       Monitored proceess (e.g., Makefile segment) preferably might ensure
--- 22,33 ----
  #
! # $5 if supplied, is the number of seconds to sleep (fractions ok)
! # If $5 is not null and not a number then 1 second sleep is assumed.
  # e.g., sh progress_bar.sh foo $PPID .1 540 slow (1 second sleep)
  # e.g., sh progress_bar.sh foo $PPID .1 540 5 (5 second sleep)
+ # e.g., sh progress_bar.sh foo $PPID .1 540 1.5 (1.5 second sleep)
  #
! # Note: It sleeps on each nonspace single character in graphic_str.
! #       e.g., if 5 characters in graphic_str, and if 1 second sleep,
! #       then 1 second between tests for exit, and 5 seconds between
! #       position jumps and time/percentage updates.
  #       Monitored proceess (e.g., Makefile segment) preferably might ensure
***************
*** 35,36 ****
--- 36,44 ----
  #       although when one ends the other will continue (so no calamity).
+ #
+ # Note: long sleeps
+ #       The process that launches progress_bar.sh can capture its PID
+ #       by getting PROGRESS_BAR_PID=$!
+ #       Then can cause it to stop with
+ #       [ -e /proc/$PROGRESS_BAR_PID ] && kill -1 $PROGRESS_BAR_PID
+ #       if the long sleep would otherwise lead to 2 progress_bars going.

***************
*** 47,50 ****

! declare -a  GRAPHIC_STR="| / - \\ + "
! declare -a  GRAPHIC_STR10="/ | - \\ - "         #backwards STR
  declare -i  SEC=0  # Seconds accumulator
--- 55,61 ----

! declare -a  GRAPHIC_STR="| / - \\ +"
! #declare -a  GRAPHIC_STR1=$GRAPHIC_STR
! #declare -a  GRAPHIC_STR10="/ | - \\ -"         #backwards STR
! declare -a  GRAPHIC_STR1="#"          # This simple char w/ 1 second sleep
! declare -a  GRAPHIC_STR10="."         #backwards STR
  declare -i  SEC=0  # Seconds accumulator
***************
*** 66,68 ****
    if [ "${SBUS%.*}.${SBUS#*.}" == $SBUS ]; then
!     SBUS10=${SBUS%.*}${SBUS#*.} # multiply by 10: bash math has no decimal
    else
--- 77,89 ----
    if [ "${SBUS%.*}.${SBUS#*.}" == $SBUS ]; then
!     HUNS=${SBUS#*.}
!     TENS=${HUNS:0:1}
!     HUNS=${HUNS:1:1}
!     if [ -n "$HUNS" ]; then
!       # chop or round hundredths (is it really that accurate)
!       # anyway, if ETA10 gets too small (e.g., 0) it does the old way
!       if [ $HUNS -gt 4 ] && [ $TENS -lt 9 ]; then
!         TENS=$(( $TENS + 1 ))
!       fi
!     fi
!     SBUS10=${SBUS%.*}${TENS} # multiply by 10: bash math has no decimal
    else
***************
*** 85,90 ****
        SCREENPOS=$(( $SCREENPOS + $TOFRO ))
!       write_or_exit "${CSI}${SCREENPOS}G-"
      elif [ $TOFRO -eq 1 ] && [ $SCREENPOS -lt 62 ]; then
        SCREENPOS=$(( $SCREENPOS + $TOFRO ))
!       write_or_exit "${CSI}${SCREENPOS}G+"
      fi
--- 106,111 ----
        SCREENPOS=$(( $SCREENPOS + $TOFRO ))
!       write_or_exit "${CSI}${SCREENPOS}G${EGSTRING}"
      elif [ $TOFRO -eq 1 ] && [ $SCREENPOS -lt 62 ]; then
        SCREENPOS=$(( $SCREENPOS + $TOFRO ))
!       write_or_exit "${CSI}${SCREENPOS}G${EGSTRING}"
      fi
***************
*** 101,112 ****
  TOFRO=1
  SLOWPOKE=$5
  if [ -n "$SLOWPOKE" ]; then
!   # maybe got a number
!   SLOWPOKE=${SLOWPOKE##*[^0-9]}
!   SLOWPOKE=${SLOWPOKE%%[^0-9]*}
!   SLOWPOKE="0${SLOWPOKE}"
!   SLOWPOKE=$(( 10#$SLOWPOKE + 0 ))
!   if [ $SLOWPOKE -eq 0 ]; then
!     SLOWPOKE=1 # slowpokes have to go a least 1 second
    fi
  fi
--- 122,138 ----
  TOFRO=1
+ REVERSED=0
  SLOWPOKE=$5
  if [ -n "$SLOWPOKE" ]; then
!   # Let them go at their chosen speed (but force NAN to 1).
!   SLOWPOKE=${SLOWPOKE##*[^0-9.]}
!   SLOWPOKE=${SLOWPOKE%%[^0-9.]*}
!   while [ "${SLOWPOKE:0:1}" == "0" ]; do
!     SLOWPOKE=${SLOWPOKE:1}
!   done
!   if [ "$SLOWPOKE" == "" ]; then
!     SLOWPOKE=1
    fi
+   SLEEP_TIME=$SLOWPOKE # for some like it slow (or fast)
+ else
+   SLEEP_TIME=.1
  fi
***************
*** 133,135 ****
        if [ $TOFRO -eq 1 ]; then
!         GSTRING=${GRAPHIC_STR}
        else
--- 159,161 ----
        if [ $TOFRO -eq 1 ]; then
!         GSTRING=${GRAPHIC_STR1}
        else
***************
*** 140,141 ****
--- 166,168 ----
      fi
+     EGSTRING=${GSTRING:$(( ${#GSTRING} - 1 ))} # char GSTRING ends with

***************
*** 143,153 ****
      for GRAPHIC_CHAR in ${GSTRING}; do
-       SLEEP_TIME=.1
        if [ $ETA10 -gt 0 ]; then
          write_or_exit "${CSI}${SCREENPOS}G${GRAPHIC_CHAR}"
-         if [ $ETA10 -lt 400 ]; then
-           SLEEP_TIME=.05 # unlikely
-         fi
-         if [ -n "$SLOWPOKE" ]; then
-           SLEEP_TIME=$SLOWPOKE # for some like it slow
-         fi
        else
--- 170,173 ----
***************
*** 155,158 ****
        fi
-       #$SLEEP .12 # This value MUST be less than .2 seconds.
-       #$SLEEP .1 # Sleep time is also part of the fudgework
        $SLEEP $SLEEP_TIME # Sleep time is also part of the fudgework
--- 175,176 ----
***************
*** 190,191 ****
--- 208,210 ----
          TOFRO=$(( $TOFRO * -1 ))
+         REVERSED=1
        fi
***************
*** 199,211 ****

!       if [ $(( $PCNT % 100 )) -gt $SCRN_PCNT ]; then
          # advance if stays in bounds
!         if [ -n "$SLOWPOKE" ]; then
!           catch_up_slowpoke $(( $PCNT % 100 )) $SCRN_PCNT
!         fi
!         if [ $TOFRO -eq -1 ] && [ $SCREENPOS -gt 3 ]; then
!           SCREENPOS=$(( $SCREENPOS + $TOFRO ))
!         elif [ $TOFRO -eq 1 ] && [ $SCREENPOS -lt 62 ]; then
!           SCREENPOS=$(( $SCREENPOS + $TOFRO ))
          fi
        fi

--- 218,232 ----

!       if [ $MOD_PCNT -gt $SCRN_PCNT ] && [ $REVERSED -eq 0 ]; then
          # advance if stays in bounds
!         if [ $MOD_PCNT -gt $(( $SCRN_PCNT + 1 )) ]; then
!           catch_up_slowpoke $MOD_PCNT $SCRN_PCNT
!         else
!           if [ $TOFRO -eq -1 ] && [ $SCREENPOS -gt 3 ]; then
!             SCREENPOS=$(( $SCREENPOS + $TOFRO ))
!           elif [ $TOFRO -eq 1 ] && [ $SCREENPOS -lt 62 ]; then
!             SCREENPOS=$(( $SCREENPOS + $TOFRO ))
!           fi
          fi
        fi
+       REVERSED=0



More information about the alfs-discuss mailing list