Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
informatique:outils:bash [2025/07/07 18:04] – [Fonctions utiles] bn8 | informatique:outils:bash [2025/08/21 12:47] (Version actuelle) – [Barre de progression] bn8 | ||
---|---|---|---|
Ligne 491: | Ligne 491: | ||
<code bash> | <code bash> | ||
declare -A PBARS | declare -A PBARS | ||
- | declare PBID | ||
# Create a progress bar | # Create a progress bar | ||
Ligne 498: | Ligne 497: | ||
# - total count (default: 100) | # - total count (default: 100) | ||
# - bar size (default: use all the width of the terminal with a minimum of 5 caracters) | # - 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: | + | # - the name of the variable use to store the progress bar ID (default: |
function pbar_create() { | function pbar_create() { | ||
- | | + | local id=${4:-PBAR} |
- | | + | |
- | local -n id=" | + | |
- | # Generate the progress bar ID | + | |
- | id="$( tr -dc A-Za-z0-9 </ | + | |
# Initialize progress bar information | # Initialize progress bar information | ||
PBARS[" | PBARS[" | ||
Ligne 519: | Ligne 514: | ||
# Finish a progress bar | # Finish a progress bar | ||
# Arguments: | # Arguments: | ||
- | # - the ID of the progress bar (default: | + | # - the ID of the progress bar (default: |
function pbar_finish() { | function pbar_finish() { | ||
- | local id=${1:-}; [[ -z " | + | local id=${1:-PBAR} |
# Force a last update of the progess bar | # Force a last update of the progess bar | ||
Ligne 539: | Ligne 534: | ||
# Draw the progress bar | # Draw the progress bar | ||
# Arguments: | # Arguments: | ||
- | # - the ID of the progress bar (default: | + | # - the ID of the progress bar (default: |
# - extra message to display in the progress bar (before the ETA, optional) | # - 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 | # - all extra arguments will be use to compute the extra message using printf | ||
function pbar_draw() { | function pbar_draw() { | ||
- | local id=${1:-}; [[ -z " | + | local id=${1:-PBAR} |
# Compute extra message | # Compute extra message | ||
Ligne 620: | Ligne 615: | ||
# Increment the progress bar | # Increment the progress bar | ||
# Arguments: | # Arguments: | ||
- | # - the ID of the progress bar (default: | + | # - the step (default: 1) |
+ | # - the ID of the progress bar (default: | ||
# - extra message to display in the progress bar (before the ETA, optional) | # - 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 | # - all extra arguments will be use to compute the extra message using printf | ||
function pbar_increment() { | function pbar_increment() { | ||
- | local id=${1:-}; [[ -z " | + | local step=${1:-1} id=${2:-PBAR} |
# Increment the progress bar state | # Increment the progress bar state | ||
- | ((PBARS[${id}_CURRENT]++)) | + | ((PBARS[${id}_CURRENT]+=step)) |
# Draw the progress bar | # Draw the progress bar | ||
- | pbar_draw " | + | pbar_draw "${@:2}" |
} | } | ||
</ | </ | ||
Ligne 646: | Ligne 642: | ||
pbar_create " | pbar_create " | ||
for i in $( seq 1 20 ); do | for i in $( seq 1 20 ); do | ||
- | pbar_increment "" | + | pbar_increment |
sleep 0.1 | sleep 0.1 | ||
done | done | ||
- | pbar_finish "" | + | pbar_finish |
</ | </ | ||
<note warning> | <note warning> |