init
commit
06434a4935
@ -0,0 +1,186 @@
|
||||
#!/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
|
||||
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
|
||||
if [ -n "$(upower -i "$1" | grep 'mouse')" ] ; then
|
||||
printf '%s ' "$MOUSE_ICON"
|
||||
fi
|
||||
|
||||
if [ -n "$(upower -i "$1" | grep 'headset')" ] ; then
|
||||
printf '%s ' "$HEADSET_ICON"
|
||||
fi
|
||||
else
|
||||
printf '%s' "$(upower -i "$1" | grep 'model:' | awk '{print $2}')"
|
||||
fi
|
||||
}
|
||||
|
||||
battery() {
|
||||
# gets device battery level; dev in $1
|
||||
|
||||
level=$(upower -i "$1" | grep 'percentage:' | awk '{print $2}' | tr -d '%')
|
||||
state=$(upower -i "$1" | grep 'state:' | awk '{print $2}')
|
||||
|
||||
if [ "$state" == "unknown" ] ; then
|
||||
# override to always show if device asleep
|
||||
echo -e '\Uf04b2 ' && exit 0
|
||||
fi
|
||||
|
||||
color "$state" $level
|
||||
|
||||
if [ -n "$BATTERY_ICON" ] ; then
|
||||
battery_icon $level "$state"
|
||||
else
|
||||
printf '%d%%' $level
|
||||
fi
|
||||
}
|
||||
|
||||
color() {
|
||||
# sets battery color based on passed params
|
||||
if [ -n "$COLOR" ] ; then
|
||||
if [ "$1" == "charging" ] ; then
|
||||
echo "$CHARGING_COLOR"
|
||||
elif [ $2 -le $LOW_BATTERY_THRESHOLD ] ; then
|
||||
echo "$LOW_BATTERY_COLOR"
|
||||
fi
|
||||
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
|
||||
|
||||
# go through devs
|
||||
devs=$(upower -e | grep -v "DisplayDevice") # don't use the monitor
|
||||
|
||||
for dev in $devs; do
|
||||
# getting device info
|
||||
dev_id=$(identifier "$dev")
|
||||
dev_battery=$(battery "$dev")
|
||||
|
||||
printf ' %s %s' "$dev_id" "$dev_battery"
|
||||
done
|
||||
echo
|
Loading…
Reference in New Issue