You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
4.7 KiB
Bash

#!/bin/bash
# parses upower -e and grabs device/battery level
usage() {
cat <<EOF
usage $0: [-i][-d][-b][-c][-h]
Options:
-b, --battery-icon uses battery level NF icon instead of percentage
-c, --color enables polybar color codes
-d, --dev-icon uses device NF icon instead of name
-i, --icon-test prints all icons used to test rendering
-h, --help shows this message
EOF
}
LOW_BATTERY_THRESHOLD=15
LOW_BATTERY_COLOR='%{F#F00}' # polybar red
CHARGING_COLOR='%{F#0F0}' # polybar green
MOUSE_ICON=$(echo -e '\Uf098b') # bt mouse
HEADSET_ICON=$(echo -e '\Uf0970')
battery_icon() {
# grabs corresponding battery level icon; state in 2, level in 1
if [ "$2" == "charging" ] ; then
# mouse is charging, battery icon represents
if [ $1 -gt 95 ] ; then
echo -e '\Uf0085'
elif [ $1 -gt 85 ] ; then
echo -e '\Uf008b'
elif [ $1 -gt 75 ] ; then
echo -e '\Uf008a'
elif [ $1 -gt 65 ] ; then
echo -e '\Uf089e'
elif [ $1 -gt 55 ] ; then
echo -e '\Uf0089'
elif [ $1 -gt 45 ] ; then
echo -e '\Uf089d'
elif [ $1 -gt 35 ] ; then
echo -e '\Uf0088'
elif [ $1 -gt 25 ] ; then
echo -e '\Uf0087'
elif [ $1 -gt 15 ] ; then
echo -e '\Uf0086'
elif [ $1 -gt 5 ] ; then
echo -e '\Uf089c'
else
echo -e '\Uf089f'
fi
else
# regular battery icon
if [ $1 -gt 95 ] ; then
echo -e '\Uf0079'
elif [ $1 -gt 85 ] ; then
echo -e '\Uf0081'
elif [ $1 -gt 75 ] ; then
echo -e '\Uf0081'
elif [ $1 -gt 65 ] ; then
echo -e '\Uf0080'
elif [ $1 -gt 55 ] ; then
echo -e '\Uf007f'
elif [ $1 -gt 45 ] ; then
echo -e '\Uf007e'
elif [ $1 -gt 35 ] ; then
echo -e '\Uf007d'
elif [ $1 -gt 25 ] ; then
echo -e '\Uf007c'
elif [ $1 -gt 15 ] ; then
echo -e '\Uf007b'
elif [ $1 -gt 5 ] ; then
echo -e '\Uf007a'
else
echo -e '\Uf008e'
fi
fi
}
icon_test() {
echo "Icon test for $1. (0-100)"
for ((i=-4; i<=100; i+=10)); do
battery_icon $i "$1"
done
}
identifier() {
# returns device name or icon based on options; dev in $1
if [ -n "$DEV_ICON" ] ; then
[[ "$1" =~ [Mm]ouse ]] && echo "$MOUSE_ICON"
[[ "$1" =~ [Hh]eadset ]] && echo "$HEADSET_ICON"
else
echo "$1" | grep 'model:' | awk '{print $2}'
fi
}
color() {
# sets battery color; level in $1, state in $2
if [ "$2" == "charging" ] ; then
echo "$CHARGING_COLOR"
elif [ $1 -le $LOW_BATTERY_THRESHOLD ] ; then
echo "$LOW_BATTERY_COLOR"
fi
}
# parsing long args
for arg in "$@"; do
shift
case "$arg" in
'--help') set -- "$@" '-h' ;;
'--color') set -- "$@" '-c' ;;
'--icon-test') set -- "$@" '-i' ;;
'--dev-icon') set -- "$@" '-d' ;;
'--battery-icon') set -- "$@" '-b' ;;
*) set -- "$@" "$arg" ;;
esac
done
# parsing args
while getopts "hcidb" opt ; do
case "$opt" in
'h')
usage
exit 0
;;
'c')
COLOR=true
;;
'i')
echo "Headset: $HEADSET_ICON"
echo "Mouse: $MOUSE_ICON"
icon_test "default"
icon_test "charging"
exit 0
;;
'd')
DEV_ICON=true
;;
'b')
BATTERY_ICON=true
;;
'?')
usage
exit 1
;;
esac
done
for dev in `upower -e | grep -v 'DisplayDevice'`; do
# allows proper spacing
devs+=1
# getting device info
info=$(upower -i "$dev")
state=$(echo "$info" | awk '{if($1 == "state:"){print $2}}')
dev_id=$(identifier "$info")
dev_battery=`echo "$info" | awk '{if($1 == "percentage:"){print $2}}'`
if [ -n "$COLOR" ] ; then
clr=$(color `echo $dev_battery | tr -d '%'` "$state")
fi
if [ -n "$BATTERY_ICON" ] ; then
dev_battery=$(battery_icon `echo $dev_battery | tr -d '%'` "$state")
fi
if [ "$state" == "unknown" ] ; then
# override to always show if device asleep
if [ -n "$DEV_ICON" ] ; then
printf '%s %s' "$dev_id" `echo -e '\Uf04b2 '` && continue
else
printf '%s %s' "$dev_id" '?' && continue
fi
fi
# seperator
[ $devs -gt 1 ] & printf ' '
printf '%s %s%s' "$dev_id" "$clr" "$dev_battery"
done