Différences
Ci-dessous, les différences entre deux révisions de la page.
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:46] – [Barre de progression] bn8 | informatique: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() { | ||
- | | + | # Define the name of the variable that will store the progress bar ID |
- | declare -n ID=" | + | |
- | ID=$( tr -dc A-Za-z0-9 </ | + | declare -n ID=" |
- | PBARS[" | + | # Generate the progress bar ID |
- | PBARS[" | + | |
- | PBARS[" | + | # Initialize progress bar information |
- | PBARS[" | + | |
- | PBARS[" | + | [ -n " |
- | pbar_draw $ID | + | [ -n " |
+ | [ -n " | ||
+ | PBARS[" | ||
+ | PBARS[" | ||
+ | # Draw the progress bar for a first time | ||
+ | | ||
} | } | ||
+ | # Finish a progress bar | ||
+ | # Arguments: | ||
+ | # - the ID of the progress bar (default: $PBID) | ||
function pbar_finish() { | function pbar_finish() { | ||
- | | + | |
- | unset ' | + | unset ' |
- | unset ' | + | unset ' |
- | unset ' | + | unset ' |
- | unset ' | + | unset ' |
- | unset ' | + | unset ' |
- | 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() { | ||
- | | + | |
- | 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]}/ | + | let PERC=${PBARS[${ID}_CURRENT]}*100/${PBARS[${ID}_TOTAL]} |
- | SPEED=$( echo " | + | |
- | else | + | # Compute duration, total duration, ETA & speed |
- | TOTAL_DURATION=0 | + | let DURATION=NOW-${PBARS[${ID}_START_TIME]} |
- | SPEED="?" | + | if [ ${PBARS[${ID}_CURRENT]} -gt 0 ] |
- | fi | + | then |
- | let ETA=TOTAL_DURATION-DURATION | + | let TOTAL_DURATION=DURATION*${PBARS[${ID}_TOTAL]}/ |
- | let DONE=$PERC*4/10 | + | SPEED=$( echo " |
- | let LEFT=40-$DONE | + | else |
- | DONE=$(printf " | + | TOTAL_DURATION=0 |
- | LEFT=$(printf " | + | SPEED="?" |
+ | fi | ||
+ | let ETA=TOTAL_DURATION-DURATION | ||
+ | |||
+ | # Compute line without the progress bar | ||
+ | LINE=( | ||
+ | | ||
+ | " | ||
+ | " | ||
+ | ) | ||
+ | [ -n " | ||
+ | LINE+=( | ||
+ | "- ETA: $(format_duration $ETA)" | ||
+ | "- $( printf "(%s / %s, %s/s)" " | ||
+ | ) | ||
+ | |||
+ | # 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 " | ||
+ | SIZE=$[ $TERM_WIDTH - $LINE_LENGTH ] | ||
+ | [ $SIZE -lt 5 ] && SIZE=5 | ||
+ | fi | ||
+ | |||
+ | # Set progress bar text | ||
+ | | ||
+ | let LEFT=$SIZE-$DONE | ||
+ | | ||
+ | |||
+ | # Add line padding (if need) | ||
+ | let LINE_PAD=TERM_WIDTH-${# | ||
+ | [ $LINE_PAD -gt 0 ] && LINE+=( " | ||
- | printf "\r%s: [%s%s] - %d/%d - %d%% - ETA: %s (%s / %s, %s/s)" \ | + | # Compute & display line (strip the terminal width) |
- | "${PBARS[${ID}_TITLE]}" | + | LINE="${LINE[*]}" |
- | " | + | echo -en "\r${LINE:0:$TERM_WIDTH}" |
- | | + | |
- | $PERC \ | + | |
- | | + | |
- | " | + | |
- | " | + | |
- | $SPEED | + | |
- | PBARS[${ID}_LAST_UPDATE]=$NOW | + | # Update last progress bar update time |
+ | | ||
} | } | ||
+ | # 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() { | ||
- | | + | |
- | ((PBARS[${ID}_CURRENT]++)) | + | # Compute extra message |
- | pbar_draw $ID | + | if [ -n " |
+ | then | ||
+ | | ||
+ | shift 2 | ||
+ | [ $# -gt 0 ] && EXTRA=$( printf " | ||
+ | else | ||
+ | EXTRA="" | ||
+ | fi | ||
+ | # Increment the progress bar state | ||
+ | | ||
+ | # Draw the progress bar | ||
+ | | ||
} | } | ||
</ | </ | ||
Ligne 240: | Ligne 303: | ||
for i in $( seq 1 20 ) | for i in $( seq 1 20 ) | ||
do | do | ||
- | | + | |
- | sleep 0.1 | + | sleep 0.1 |
done | done | ||
pbar_finish | pbar_finish |