Last active
September 1, 2024 11:51
-
-
Save alifeee/a6f14aad9caed6b8a861df0e2850aa10 to your computer and use it in GitHub Desktop.
Simple bar chart generation with bash
This file contains 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
#!/bin/bash | |
# make bar chart from $prog, $total, $n_seg as $1 $2 $3 | |
# example: | |
# > ./bar.sh 25 100 12 | |
# ███░░░░░░░░░ | |
bar () { | |
# $1 $2 $3 are progress total total_segments | |
# shows progress/total using total_segments characters | |
if [ $1 == "0" ] && [ $2 == "0" ]; then | |
awk -v TOTSEG=$3 'BEGIN { | |
for (s = 0; s < TOTSEG; s++) { | |
printf "%s", "░"; | |
} | |
}' | |
else | |
awk -v prog=$1 -v TOTAL=$2 -v TOTSEG=$3 'BEGIN { | |
frac = prog / TOTAL; | |
segs = frac * TOTSEG; | |
segs_int = int(sprintf("%.0f", segs)); | |
for (s=0; s<segs_int; s++) { | |
printf "%s", "█"; | |
} | |
for (s=0; s<(TOTSEG - segs_int); s++) { | |
printf "%s", "░"; | |
} | |
}' | |
fi | |
} | |
if [ -z $1 ]; then | |
bar 25 100 40 | |
echo "" | |
else | |
bar $1 $2 $3 | |
echo "" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment