dotfiles/.local/bin/genbar

57 lines
1.1 KiB
Plaintext
Raw Normal View History

2024-06-23 06:44:57 -04:00
#!/bin/sh
2024-07-03 19:59:32 -04:00
LOW_USED_COLOR="#a3be8c"
MEDIUM_USED_COLOR="#ebcb8b"
HIGH_USED_COLOR="#bf616a"
DEFAULT_USED_COLOR="#81a1c1"
UNUSED_COLOR="#5e81ac"
2024-06-23 06:44:57 -04:00
usage=$1
width=$2
usage_color=$3
command -v "$1" >/dev/null && usage=$($1)
low_used=$((width * 30 / 100))
medium_used=$((width * 60 / 100))
2024-07-03 19:59:32 -04:00
num_used_chars=$(printf 'scale=0; (%s * %s) / 100\n' "$usage" "$width" | bc -l)
2024-06-23 06:44:57 -04:00
if [ -n "$usage_color" ]; then
case "$num_used_chars" in
2024-07-03 19:59:32 -04:00
[0-$low_used]) used_color="$LOW_USED_COLOR" ;;
[$low_used-$medium_used]) used_color="$MEDIUM_USED_COLOR" ;;
*) used_color="$HIGH_USED_COLOR" ;;
2024-06-23 06:44:57 -04:00
esac
else
used_color="$DEFAULT_USED_COLOR"
fi
2024-07-03 19:59:32 -04:00
prev_opened=0
any_used_width=0
2024-06-23 06:44:57 -04:00
i=1
while [ "$i" -le "$width" ]; do
if [ "$i" -le "$num_used_chars" ]; then
2024-07-03 19:59:32 -04:00
if [ "$prev_opened" -eq 0 ]; then
bar="$bar<fc=$used_color>"
prev_opened=1
any_used_width=1
fi
bar="$bar#"
2024-06-23 06:44:57 -04:00
else
2024-07-03 19:59:32 -04:00
if [ "$any_used_width" -eq 1 ]; then
bar="$bar</fc>"
prev_opened=0
any_used_width=0
fi
if [ "$prev_opened" -eq 0 ]; then
bar="$bar<fc=$UNUSED_COLOR>"
prev_opened=1
fi
bar="$bar:"
2024-06-23 06:44:57 -04:00
fi
i=$((i + 1))
done
2024-07-03 19:59:32 -04:00
bar="$bar</fc>"
2024-06-23 06:44:57 -04:00
2024-07-03 19:59:32 -04:00
printf '%s\n' "$bar"