Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [:~/tmp]└4 $ cat ./progressbar
- progress_bar() {
- local progress="$1"
- local bar_length=48
- # Force progress to 100 if it is >= 100.
- if (( $(echo "$progress >= 100" | bc -l) )); then
- progress=100
- fi
- # Calculate number of filled characters (rounded)
- local filled
- filled=$(printf "%.0f" "$(echo "$progress * $bar_length / 100" | bc -l)")
- local empty=$((bar_length - filled))
- local filled_char="█"
- local empty_char="_"
- # Build the underlying bar
- local bar_filled
- bar_filled=$(printf "%0.s$filled_char" $(seq 1 $filled))
- # if the progress is 0, then the bar should be empty
- if [[ $(echo "$1 == 0" | bc) -eq 1 ]]; then
- bar_filled=""
- fi
- local bar_empty
- bar_empty=$(printf "%0.s$empty_char" $(seq 1 $empty))
- local bar="${bar_filled}${bar_empty}"
- # Format percentage string with 2 decimals and exactly one space before and after.
- local percent_value
- percent_value=$(printf "%.2f%%" "$progress")
- local percent_str=" ${percent_value} "
- local percent_len=${#percent_str}
- # Calculate insertion point (index where the percent_str should start within bar)
- local insert_index=$(( (bar_length - percent_len) / 2 ))
- # Build final bar by replacing a segment of the underlying bar with percent_str.
- local final_bar="${bar:0:insert_index}${percent_str}${bar:insert_index+percent_len}"
- #removing last character from final_bar
- final_bar=${final_bar%?}
- # Enclose with vertical bars.
- echo "${GREEN}|${LIGHT_GREEN}${final_bar}${GREEN}|${NC}"
- }
- for i in 100 {0..110..3}.12; do progress_bar $i; done
- [:~/tmp]└4 $ bash ./progressbar
- |███████████████████ 100.00% ████████████████████|
- |█___________________ 0.12% _____________________|
- |█___________________ 3.12% ____________________|
- |███_________________ 6.12% ____________________|
- |████________________ 9.12% ____________________|
- |██████______________ 12.12% ___________________|
- |███████_____________ 15.12% ___________________|
- |█████████___________ 18.12% ___________________|
- |██████████__________ 21.12% ___________________|
- |████████████________ 24.12% ___________________|
- |█████████████_______ 27.12% ___________________|
- |██████████████______ 30.12% ___________________|
- |████████████████____ 33.12% ___________________|
- |█████████████████___ 36.12% ___________________|
- |███████████████████_ 39.12% ___________________|
- |████████████████████ 42.12% ___________________|
- |████████████████████ 45.12% ___________________|
- |████████████████████ 48.12% ___________________|
- |████████████████████ 51.12% ___________________|
- |████████████████████ 54.12% ___________________|
- |████████████████████ 57.12% ___________________|
- |████████████████████ 60.12% █__________________|
- |████████████████████ 63.12% ██_________________|
- |████████████████████ 66.12% ████_______________|
- |████████████████████ 69.12% █████______________|
- |████████████████████ 72.12% ███████____________|
- |████████████████████ 75.12% ████████___________|
- |████████████████████ 78.12% █████████__________|
- |████████████████████ 81.12% ███████████________|
- |████████████████████ 84.12% ████████████_______|
- |████████████████████ 87.12% ██████████████_____|
- |████████████████████ 90.12% ███████████████____|
- |████████████████████ 93.12% █████████████████__|
- |████████████████████ 96.12% ██████████████████_|
- |████████████████████ 99.12% ████████████████████|
- |███████████████████ 100.00% ████████████████████|
- |███████████████████ 100.00% ████████████████████|
- |███████████████████ 100.00% ████████████████████|
- [:~/tmp]└4 $ cat progressbar2
- progress_bar() {
- local progress_exact="$1"
- local progress="${progress_exact%.*}"
- local bar_length=48
- # Force progress to 100 if it is >= 100.
- (( progress = progress <= 100 ? progress : 100 ))
- # Calculate number of filled characters (rounded)
- local filled empty
- (( filled = progress * bar_length / 100,
- empty = bar_length - filled )) 21:14:04 [21/1970]
- local filled_char="█"
- local empty_char="_"
- # Build the underlying bar
- local bar_filled
- printf -v bar_filled '%*s' $filled ""
- bar_filled=${bar_filled// /$filled_char}
- local bar_empty
- printf -v bar_empty '%*s' $empty ""
- bar_empty=${bar_empty// /$empty_char}
- local bar="${bar_filled}${bar_empty}"
- # Format percentage string with 2 decimals and exactly one space before and after.
- local percent_value=$(printf "%.2f%%" "$progress_exact")
- local percent_str=" ${percent_value} "
- local percent_len=${#percent_str}
- # Calculate insertion point (index where the percent_str should start within bar)
- local insert_index=$(( (bar_length - percent_len) / 2 ))
- # Build final bar by replacing a segment of the underlying bar with percent_str.
- local final_bar="${bar:0:insert_index}${percent_str}${bar:insert_index+percent_len}"
- # Enclose with vertical bars.
- echo "${GREEN}|${LIGHT_GREEN}${final_bar}${GREEN}|${NC}"
- }
- for i in 100 {0..110..3}.12; do progress_bar $i; done
- [:~/tmp]└4 $ bash ./progressbar2
- |███████████████████ 100.00% ████████████████████|
- |____________________ 0.12% _____________________|
- |█___________________ 3.12% _____________________|
- |██__________________ 6.12% _____________________|
- |████________________ 9.12% _____________________|
- |█████_______________ 12.12% ____________________|
- |███████_____________ 15.12% ____________________|
- |████████____________ 18.12% ____________________|
- |██████████__________ 21.12% ____________________|
- |███████████_________ 24.12% ____________________|
- |████████████________ 27.12% ____________________|
- |██████████████______ 30.12% ____________________|
- |███████████████_____ 33.12% ____________________|
- |█████████████████___ 36.12% ____________________|
- |██████████████████__ 39.12% ____________________|
- |████████████████████ 42.12% ____________________|
- |████████████████████ 45.12% ____________________|
- |████████████████████ 48.12% ____________________|
- |████████████████████ 51.12% ____________________|
- |████████████████████ 54.12% ____________________|
- |████████████████████ 57.12% ____________________|
- |████████████████████ 60.12% ____________________|
- |████████████████████ 63.12% ██__________________|
- |████████████████████ 66.12% ███_________________|
- |████████████████████ 69.12% █████_______________|
- |████████████████████ 72.12% ██████______________|
- |████████████████████ 75.12% ████████____________|
- |████████████████████ 78.12% █████████___________|
- |████████████████████ 81.12% ██████████__________|
- |████████████████████ 84.12% ████████████________|
- |████████████████████ 87.12% █████████████_______|
- |████████████████████ 90.12% ███████████████_____|
- |████████████████████ 93.12% ████████████████____|
- |████████████████████ 96.12% ██████████████████__|
- |████████████████████ 99.12% ███████████████████_|
- |███████████████████ 102.12% ████████████████████|
- |███████████████████ 105.12% ████████████████████|
- |███████████████████ 108.12% ████████████████████|
- [:~/tmp]└4 $
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement