informatique:outils:bash

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
informatique:outils:bash [2024/03/25 13:49] – [Barre de progression] bn8informatique:outils:bash [2024/03/25 16:01] (Version actuelle) – [Barre de progression] bn8
Ligne 173: Ligne 173:
 declare -A PBARS declare -A PBARS
 declare PBID declare PBID
 +
 +# Create a progress bar
 +# Arguments:
 +# - the progress bar title (default: Progress)
 +# - total count (default: 100)
 +# - bar size (default: use all the width of the terminal with a minimum of 5 caracters)
 +# - the name of the variable use to store the progress bar ID (default: PBID)
 function pbar_create() { function pbar_create() {
-    [ -n "$3" ] && PBAR_ID_VAR="$3" || PBAR_ID_VAR=PBID+    # Define the name of the variable that will store the progress bar ID 
 +    [ -n "$4" ] && PBAR_ID_VAR="$4" || PBAR_ID_VAR=PBID
     declare -n ID="$PBAR_ID_VAR"     declare -n ID="$PBAR_ID_VAR"
 +    # Generate the progress bar ID
     ID=$( tr -dc A-Za-z0-9 </dev/urandom | head -c 3 )     ID=$( tr -dc A-Za-z0-9 </dev/urandom | head -c 3 )
 +    # Initialize progress bar information
     PBARS["${ID}_START_TIME"]="$( date +%s )"     PBARS["${ID}_START_TIME"]="$( date +%s )"
-    PBARS["${ID}_TITLE"]="$1" +    [ -n "$1" ] && PBARS["${ID}_TITLE"]="$1" || PBARS["${ID}_TITLE"]="Progress
-    PBARS["${ID}_TOTAL"]="$2"+    [ -n "$2" ] && PBARS["${ID}_TOTAL"]="$2" || PBARS["${ID}_TOTAL"]=100 
 +    [ -n "$3" ] && PBARS["${ID}_SIZE"]="$3" || PBARS["${ID}_SIZE"]=0
     PBARS["${ID}_CURRENT"]=0     PBARS["${ID}_CURRENT"]=0
     PBARS["${ID}_LAST_UPDATE"]=0     PBARS["${ID}_LAST_UPDATE"]=0
 +    # Draw the progress bar for a first time
     pbar_draw $ID     pbar_draw $ID
 } }
  
 +# Finish a progress bar
 +# Arguments:
 +# - the ID of the progress bar (default: $PBID)
 function pbar_finish() { function pbar_finish() {
     [ -n "$1" ] && ID=$1 || ID=$PBID     [ -n "$1" ] && ID=$1 || ID=$PBID
Ligne 195: Ligne 210:
 } }
  
 +# Draw the progress bar
 +# Arguments:
 +# - the ID of the progress bar (default: $PBID)
 +# - extra message to display in the progress bar (before the ETA, optional)
 function pbar_draw() { function pbar_draw() {
     [ -n "$1" ] && ID=$1 || ID=$PBID     [ -n "$1" ] && ID=$1 || ID=$PBID
-    let PERC=${PBARS[${ID}_CURRENT]}*100/${PBARS[${ID}_TOTAL]}+ 
 +    # Only update progress bar one time by second
     NOW=$(date +%s)     NOW=$(date +%s)
     [ $NOW -eq ${PBARS[${ID}_LAST_UPDATE]} ] && return     [ $NOW -eq ${PBARS[${ID}_LAST_UPDATE]} ] && return
 +
 +    # Compute progress percentage
 +    let PERC=${PBARS[${ID}_CURRENT]}*100/${PBARS[${ID}_TOTAL]}
 +
 +    # Compute duration, total duration, ETA & speed
     let DURATION=NOW-${PBARS[${ID}_START_TIME]}     let DURATION=NOW-${PBARS[${ID}_START_TIME]}
     if [ ${PBARS[${ID}_CURRENT]} -gt 0 ]     if [ ${PBARS[${ID}_CURRENT]} -gt 0 ]
Ligne 210: Ligne 235:
     fi     fi
     let ETA=TOTAL_DURATION-DURATION     let ETA=TOTAL_DURATION-DURATION
-    let DONE=$PERC*4/10 
-    let LEFT=40-$DONE 
-    DONE=$(printf "%${DONE}s"|tr ' ' '#') 
-    LEFT=$(printf "%${LEFT}s"|tr ' ' '-') 
  
-    printf "\r%s: [%s%s] - %d/%d - %d%% - ETA: %s (%s / %s, %s/s)" \ +    # Compute line without the progress bar 
-        "${PBARS[${ID}_TITLE]}" \ +    LINE=
-        "${DONE}"${LEFT}" \ +        "${PBARS[${ID}_TITLE]}" 
-        ${PBARS[${ID}_CURRENT]} ${PBARS[${ID}_TOTAL]} +        "[]
-        $PERC \ +        "${PBARS[${ID}_CURRENT]}/${PBARS[${ID}_TOTAL]} (${PERC}%)" 
-        "$(format_duration $ETA)" \ +    ) 
-        "$(format_duration $DURATION)" +    [ -n "$2" ] && LINE+=( "- $2" ) 
-        "$(format_duration $TOTAL_DURATION)" +    LINE+=( 
-        $SPEED+        "- ETA: $(format_duration $ETA)" 
 +        "- $( printf "(%s / %s, %s/s)" "$(format_duration $DURATION)" "$(format_duration $TOTAL_DURATION)" $SPEED )" 
 +    )
  
 +    # Compute progress bar length (if not configured)
 +    read -r TERM_HEIGHT TERM_WIDTH < <(stty size)
 +    SIZE=${PBARS[${ID}_SIZE]}
 +    if [ $SIZE -eq 0 ]
 +    then
 +        LINE_LENGTH=$( echo "${LINE[*]}"|wc -c )
 +        SIZE=$[ $TERM_WIDTH - $LINE_LENGTH ]
 +        [ $SIZE -lt 5 ] && SIZE=5
 +    fi
 +
 +    # Set progress bar text
 +    let DONE=$PERC*$SIZE/100
 +    let LEFT=$SIZE-$DONE
 +    LINE[1]="[$(printf "%${DONE}s"|tr ' ' '#')$(printf "%${LEFT}s"|tr ' ' '-')]"
 +
 +    # Add line padding (if need)
 +    let LINE_PAD=TERM_WIDTH-${#LINE}
 +    [ $LINE_PAD -gt 0 ] && LINE+=( "$(printf "%${LINE_PAD}s")" )
 +
 +    # Compute & display line (strip the terminal width)
 +    LINE="${LINE[*]}"
 +    echo -en "\r${LINE:0:$TERM_WIDTH}"
 +
 +    # Update last progress bar update time
     PBARS[${ID}_LAST_UPDATE]=$NOW     PBARS[${ID}_LAST_UPDATE]=$NOW
 } }
  
 +# Increment the progress bar
 +# Arguments:
 +# - the ID of the progress bar (default: $PBID)
 +# - extra message to display in the progress bar (before the ETA, optional)
 +# - all extra arguments will be use to compute the extra message using printf
 function pbar_increment() { function pbar_increment() {
     [ -n "$1" ] && ID=$1 || ID=$PBID     [ -n "$1" ] && ID=$1 || ID=$PBID
 +    # Compute extra message
 +    if [ -n "$2" ]
 +    then
 +        EXTRA="$2"
 +        shift 2
 +        [ $# -gt 0 ] && EXTRA=$( printf "$EXTRA" "$@" )
 +    else
 +        EXTRA=""
 +    fi
 +    # Increment the progress bar state
     ((PBARS[${ID}_CURRENT]++))     ((PBARS[${ID}_CURRENT]++))
-    pbar_draw $ID+    # Draw the progress bar 
 +    pbar_draw $ID "$EXTRA"
 } }
 </code> </code>
  • informatique/outils/bash.txt
  • Dernière modification : 2024/03/25 16:01
  • de bn8