Created
June 30, 2022 18:04
-
-
Save kriansa/919fcc8151d2617fe224c0cfefd65abd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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