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 | |||
informatique:outils:bash [2024/03/25 13:49] – [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() { | ||
- | [ -n "$3" ] && PBAR_ID_VAR=" | + | |
+ | | ||
declare -n ID=" | declare -n ID=" | ||
+ | # Generate the progress bar ID | ||
ID=$( tr -dc A-Za-z0-9 </ | ID=$( tr -dc A-Za-z0-9 </ | ||
+ | # Initialize progress bar information | ||
PBARS[" | PBARS[" | ||
- | PBARS[" | + | |
- | PBARS[" | + | |
+ | [ -n " | ||
PBARS[" | PBARS[" | ||
PBARS[" | PBARS[" | ||
+ | # 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 " | [ -n " | ||
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 " | [ -n " | ||
- | let PERC=${PBARS[${ID}_CURRENT]}*100/ | + | |
+ | # 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/ | ||
+ | |||
+ | # 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/ | ||
- | let LEFT=40-$DONE | ||
- | DONE=$(printf " | ||
- | LEFT=$(printf " | ||
- | | + | |
- | " | + | LINE=( |
- | "${DONE}" | + | " |
- | ${PBARS[${ID}_CURRENT]} ${PBARS[${ID}_TOTAL]} | + | "[]" |
- | | + | |
- | " | + | ) |
- | " | + | [ -n " |
- | | + | LINE+=( |
- | | + | "- ETA: $(format_duration $ETA)" |
+ | | ||
+ | ) | ||
+ | # 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 DONE=$PERC*$SIZE/ | ||
+ | let LEFT=$SIZE-$DONE | ||
+ | LINE[1]=" | ||
+ | |||
+ | # Add line padding (if need) | ||
+ | let LINE_PAD=TERM_WIDTH-${# | ||
+ | [ $LINE_PAD -gt 0 ] && LINE+=( " | ||
+ | |||
+ | # Compute & display line (strip the terminal width) | ||
+ | LINE=" | ||
+ | echo -en " | ||
+ | |||
+ | # 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 " | [ -n " | ||
+ | # Compute extra message | ||
+ | if [ -n " | ||
+ | then | ||
+ | EXTRA=" | ||
+ | shift 2 | ||
+ | [ $# -gt 0 ] && EXTRA=$( printf " | ||
+ | else | ||
+ | EXTRA="" | ||
+ | fi | ||
+ | # Increment the progress bar state | ||
((PBARS[${ID}_CURRENT]++)) | ((PBARS[${ID}_CURRENT]++)) | ||
- | pbar_draw $ID | + | |
+ | | ||
} | } | ||
</ | </ |