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
Prochaine révision
Révision précédente
informatique:outils:bash [2024/03/25 13:36] – [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 
-        declare -n ID="$PBAR_ID_VAR" +    [ -n "$4" ] && PBAR_ID_VAR="$4" || PBAR_ID_VAR=PBID 
-        ID=$( tr -dc A-Za-z0-9 </dev/urandom | head -c 3 ) +    declare -n ID="$PBAR_ID_VAR" 
-        PBARS["${ID}_START_TIME"]="$( date +%s )" +    # Generate the progress bar ID 
-        PBARS["${ID}_TITLE"]="$1" +    ID=$( tr -dc A-Za-z0-9 </dev/urandom | head -c 3 ) 
-        PBARS["${ID}_TOTAL"]="$2" +    # Initialize progress bar information 
-        PBARS["${ID}_CURRENT"]=0 +    PBARS["${ID}_START_TIME"]="$( date +%s )" 
-        PBARS["${ID}_LAST_UPDATE"]=0 +    [ -n "$1" ] && PBARS["${ID}_TITLE"]="$1" || PBARS["${ID}_TITLE"]="Progress
-        pbar_draw $ID+    [ -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}_LAST_UPDATE"]=0 
 +    # Draw the progress bar for a first time 
 +    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 
-        unset 'PBARS[${ID}_START_TIME]' +    unset 'PBARS[${ID}_START_TIME]' 
-        unset 'PBARS[${ID}_TITLE]' +    unset 'PBARS[${ID}_TITLE]' 
-        unset 'PBARS[${ID}_TOTAL]' +    unset 'PBARS[${ID}_TOTAL]' 
-        unset 'PBARS[${ID}_CURRENT]' +    unset 'PBARS[${ID}_CURRENT]' 
-        unset 'PBARS[${ID}_LAST_UPDATE]' +    unset 'PBARS[${ID}_LAST_UPDATE]' 
-        echo+    echo
 } }
  
 +# 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]} + 
-        NOW=$(date +%s) +    # Only update progress bar one time by second 
-        [ $NOW -eq ${PBARS[${ID}_LAST_UPDATE]} && return +    NOW=$(date +%s) 
-        let DURATION=NOW-${PBARS[${ID}_START_TIME]} +    [ $NOW -eq ${PBARS[${ID}_LAST_UPDATE]} ] && return 
-        if [ ${PBARS[${ID}_CURRENT]} -gt 0 ] + 
-        then +    # Compute progress percentage 
-                let TOTAL_DURATION=DURATION*${PBARS[${ID}_TOTAL]}/${PBARS[${ID}_CURRENT]} +    let PERC=${PBARS[${ID}_CURRENT]}*100/${PBARS[${ID}_TOTAL]} 
-        else + 
-                TOTAL_DURATION=0 +    # Compute duration, total duration, ETA speed 
-        fi +    let DURATION=NOW-${PBARS[${ID}_START_TIME]} 
-        let ETA=TOTAL_DURATION-DURATION +    if [ ${PBARS[${ID}_CURRENT]} -gt 0 ] 
-        let DONE=$PERC*4/10 +    then 
-        let LEFT=40-$DONE +        let TOTAL_DURATION=DURATION*${PBARS[${ID}_TOTAL]}/${PBARS[${ID}_CURRENT]} 
-        DONE=$(printf "%${DONE}s"|tr ' ' '#') +        SPEED=$( echo "scale=1; ${PBARS[${ID}_CURRENT]}/$DURATION"|bc ) 
-        LEFT=$(printf "%${LEFT}s"|tr ' ' '-')+    else 
 +        TOTAL_DURATION=0 
 +        SPEED="?" 
 +    fi 
 +    let ETA=TOTAL_DURATION-DURATION 
 + 
 +    # Compute line without the progress bar 
 +    LINE=( 
 +        "${PBARS[${ID}_TITLE]}" 
 +        "[]" 
 +        "${PBARS[${ID}_CURRENT]}/${PBARS[${ID}_TOTAL]} (${PERC}%)" 
 +    ) 
 +    [ -n "$2" ] && LINE+=( "- $2" ) 
 +    LINE+=( 
 +        "- 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")" )
  
-        printf "\r%s: [%s%s] - %d/%d - %d%% - ETA: %s (%s / %s)" \ +    # Compute & display line (strip the terminal width
-                "${PBARS[${ID}_TITLE]}" \ +    LINE="${LINE[*]}" 
-                "${DONE}" "${LEFT}" \ +    echo -en "\r${LINE:0:$TERM_WIDTH}"
-                ${PBARS[${ID}_CURRENT]} ${PBARS[${ID}_TOTAL]} \ +
-                $PERC \ +
-                "$(format_duration $ETA)" \ +
-                "$(format_duration $DURATION)"+
-                "$(format_duration $TOTAL_DURATION)"+
  
-        PBARS[${ID}_LAST_UPDATE]=$NOW+    # Update last progress bar update time 
 +    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 
-        ((PBARS[${ID}_CURRENT]++)) +    # Compute extra message 
-        pbar_draw $ID+    if [ -n "$2"
 +    then 
 +        EXTRA="$2" 
 +        shift 2 
 +        [ $# -gt 0 ] && EXTRA=$( printf "$EXTRA" "$@"
 +    else 
 +        EXTRA="" 
 +    fi 
 +    # Increment the progress bar state 
 +    ((PBARS[${ID}_CURRENT]++)) 
 +    # Draw the progress bar 
 +    pbar_draw $ID "$EXTRA"
 } }
 </code> </code>
Ligne 237: Ligne 303:
 for i in $( seq 1 20 ) for i in $( seq 1 20 )
 do do
-        pbar_increment +    pbar_increment 
-        sleep 0.1+    sleep 0.1
 done done
 pbar_finish pbar_finish
  • informatique/outils/bash.1711373775.txt.gz
  • Dernière modification : 2024/03/25 13:36
  • de bn8