cleaned up some code and reduced calls to upower

main
spinach 2 years ago
parent 06434a4935
commit e75250ac9f

@ -22,7 +22,7 @@ MOUSE_ICON=$(echo -e '\Uf098b') # bt mouse
HEADSET_ICON=$(echo -e '\Uf0970') HEADSET_ICON=$(echo -e '\Uf0970')
battery_icon() { battery_icon() {
# grabs corresponding battery level icon # grabs corresponding battery level icon; state in 2, level in 1
if [ "$2" == "charging" ] ; then if [ "$2" == "charging" ] ; then
# mouse is charging, battery icon represents # mouse is charging, battery icon represents
if [ $1 -gt 95 ] ; then if [ $1 -gt 95 ] ; then
@ -87,46 +87,20 @@ identifier() {
# returns device name or icon based on options; dev in $1 # returns device name or icon based on options; dev in $1
if [ -n "$DEV_ICON" ] ; then if [ -n "$DEV_ICON" ] ; then
if [ -n "$(upower -i "$1" | grep 'mouse')" ] ; then [[ "$1" =~ [Mm]ouse ]] && echo "$MOUSE_ICON"
printf '%s ' "$MOUSE_ICON" [[ "$1" =~ [Hh]eadset ]] && echo "$HEADSET_ICON"
fi
if [ -n "$(upower -i "$1" | grep 'headset')" ] ; then
printf '%s ' "$HEADSET_ICON"
fi
else else
printf '%s' "$(upower -i "$1" | grep 'model:' | awk '{print $2}')" echo "$1" | grep 'model:' | awk '{print $2}'
fi 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() { color() {
# sets battery color based on passed params # sets battery color; level in $1, state in $2
if [ -n "$COLOR" ] ; then if [ "$2" == "charging" ] ; then
if [ "$1" == "charging" ] ; then echo "$CHARGING_COLOR"
echo "$CHARGING_COLOR" elif [ $1 -le $LOW_BATTERY_THRESHOLD ] ; then
elif [ $2 -le $LOW_BATTERY_THRESHOLD ] ; then echo "$LOW_BATTERY_COLOR"
echo "$LOW_BATTERY_COLOR"
fi
fi fi
} }
@ -173,14 +147,36 @@ while getopts "hcidb" opt ; do
esac esac
done done
# go through devs for dev in `upower -e | grep -v 'DisplayDevice'`; do
devs=$(upower -e | grep -v "DisplayDevice") # don't use the monitor # allows proper spacing
devs+=1
for dev in $devs; do
# getting device info # getting device info
dev_id=$(identifier "$dev") info=$(upower -i "$dev")
dev_battery=$(battery "$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' "$dev_id" "$dev_battery" printf '%s %s%s' "$dev_id" "$clr" "$dev_battery"
done done
echo

Loading…
Cancel
Save