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édentesRévision précédente
Prochaine révision
Révision précédente
informatique:outils:bash [2025/07/07 18:04] – [Fonctions utiles] bn8informatique: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: PBID)+# - the name of the variable use to store the progress bar ID (default: PBAR)
 function pbar_create() { function pbar_create() {
-    # Define the name of the variable that will store the progress bar ID +    local id=${4:-PBAR}
-    local pbar_id_var=${4:-}; [[ -z "$pbar_id_var" ]] && pbar_id_var=PBID +
-    local -n id="$pbar_id_var" +
-    # Generate the progress bar ID +
-    id="$( tr -dc A-Za-z0-9 </dev/urandom | head -c 3 )"+
     # Initialize progress bar information     # Initialize progress bar information
     PBARS["${id}_START_TIME"]="$( date +%s )"     PBARS["${id}_START_TIME"]="$( date +%s )"
Ligne 519: Ligne 514:
 # Finish a progress bar # Finish a progress bar
 # Arguments: # Arguments:
-# - the ID of the progress bar (default: $PBID)+# - the ID of the progress bar (default: PBAR)
 function pbar_finish() { function pbar_finish() {
-    local id=${1:-}; [[ -z "$id" ]] && id=$PBID+    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: $PBID)+# - the ID of the progress bar (default: PBAR)
 # - 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 "$id" ]] && id=$PBID+    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: $PBID)+# - the step (default: 1) 
 +# - the ID of the progress bar (default: PBAR)
 # - 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 "$id" ]] && id=$PBID+    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}"
 } }
 </code> </code>
Ligne 646: Ligne 642:
 pbar_create "Test" 20 pbar_create "Test" 20
 for i in $( seq 1 20 ); do for i in $( seq 1 20 ); do
-    pbar_increment "" "%d iteration(s) - %d found(s)" $i $(( i/2 ))+    pbar_increment "" "" "%d iteration(s) - %d found(s)" $i $(( i/2 ))
     sleep 0.1     sleep 0.1
 done done
-pbar_finish "" "%d iteration(s) - %d found(s)" $i $(( i/2 ))+pbar_finish "" "" "%d iteration(s) - %d found(s)" $i $(( i/2 ))
 </code> </code>
  
 <note warning>La fonction [[#format_duration]] est nécessaire.</note> <note warning>La fonction [[#format_duration]] est nécessaire.</note>