Skip to content

Instantly share code, notes, and snippets.

@kriansa
Created June 30, 2022 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kriansa/919fcc8151d2617fe224c0cfefd65abd to your computer and use it in GitHub Desktop.
Save kriansa/919fcc8151d2617fe224c0cfefd65abd to your computer and use it in GitHub Desktop.
# This will generate a monospaced progress bar with a progress percentage displayed in the center of it
# Usage: progress-bar <percentage> <width>
# Example output: "———————— 80% —————— "
progress-bar() {
local percentage=$1
local width=$2
local fill_bars; fill_bars=$(( width * percentage / 100 ))
local fill_spaces; fill_spaces=$(( width - fill_bars ))
local bar=""
for (( i = 1; i <= $width; i++ )); do
[ $i -le $fill_bars ] && bar+="—" || bar+=" "
done
local percentage_str=" ${percentage}% "
local str_size=${#percentage_str}
local first_cut=$(( (width - str_size) / 2 ))
local last_cut=$(( first_cut + str_size ))
echo "${bar:0:$first_cut}${percentage_str}${bar:$last_cut}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment