Skip to content

Instantly share code, notes, and snippets.

@alifeee
Last active September 1, 2024 11:51
Show Gist options
  • Save alifeee/a6f14aad9caed6b8a861df0e2850aa10 to your computer and use it in GitHub Desktop.
Save alifeee/a6f14aad9caed6b8a861df0e2850aa10 to your computer and use it in GitHub Desktop.
Simple bar chart generation with bash
#!/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