On click, showcase the Top 10 Consumers

This requires a sudo change that will be a separate pull request to
LARBS repository. Also requires powertop.

Additionally, it will show the estimated remaining battery life when
discharging.

Screenshots will be attached to the GitHub PR.
This commit is contained in:
Nic Anderson 2019-10-25 00:38:15 -04:00
parent f255cad8e5
commit c5326a688b

View File

@ -1,32 +1,99 @@
#!/usr/bin/env sh
# Give a battery name (e.g. BAT0) as an argument.
case $BLOCK_BUTTON in
3) pgrep -x dunst >/dev/null && notify-send "🔋 Battery module" "🔋: discharging
# Arbitrary but unique appid
# Generated by mashing on my keyboard
appid=1859028
# Max width of the dunst notifications. May be system dependent?
maxwidth=29
# Help text
help_text=$(cat <<-EOF
🔋: discharging
🛑: not charging
♻: stagnant charge
🔌: charging
⚡: charged
❗: battery very low!
- Text color reflects charge left" ;;
- Text color reflects charge left
EOF
);
# For testing, take the second arg as button press
if ! [ -z $2 ]; then
BLOCK_BUTTON=$2
fi
case $BLOCK_BUTTON in
1)
# Get current power stats
sudo powertop -q --csv=/tmp/powertop.csv -t=5 >/dev/null 2>&1
# Cut out the 'Top 10 Power Consumers'
start_line=$(grep -n "Top 10 Power Consumers" /tmp/powertop.csv \
| cut -d: -f1)
start_line=$(expr $start_line + 3)
end_line=$(expr $start_line + 9)
# Parse it out
top_consumers=$(sed -n "$start_line,${end_line}p" /tmp/powertop.csv \
| grep -v powertop \
| cut -d';' -f5,4 \
| sed -Ee \
"s/Display backlight*/Display/;"`
`"s/Radio device: *//;"`
`"s/\[PID [0-9]*\] *//;"`
`"s,\[([a-z]+)/[0-9:]+\],\1,;"`
`"s/: *([A-Za-z0-9]* *[A-Za-z0-9]*) */:\1/;"`
`"s/[ \t]*$//;"`
`"s,.*/,,;"`
`"s/://g;"`
`"s/[ \t]*;[ \t]*/;/" \
| tr ';' ':' \
| column -c $maxwidth -s':' --table -T 1 -R 2)
# Finally, send!
dunstify -u low -t 20000 -r $appid -i none "🔋 Top Consumers" "$top_consumers"
;;
3)
dunstify -u low -r $appid -i none " Battery module" "$help_text"
;;
esac
capacity=$(cat /sys/class/power_supply/"$1"/capacity) || exit
status=$(cat /sys/class/power_supply/"$1"/status)
capacity=$(cat /sys/class/power_supply/"${1:-BAT0}"/capacity) || exit
status=$(cat /sys/class/power_supply/"${1:-BAT0}"/status)
# `upower` take an average over time which is nice to have for a status bar
# `acpi` is another decent method, but takes an instantaneous average
if [ -x /usr/bin/upower ]; then
time_remaining=" $(upower --show-info /org/freedesktop/UPower/devices/battery_${1-BAT0} | grep 'time to empty' | cut -d: -f2 | xargs)"
elif [ -x /usr/bin/acpi ]; then
time_remaining=$(acpi -b | cut -d, -f3 | cut -d: -f1,2)
fi
if [ "$capacity" -ge 75 ]; then
color="#00ff00"
color="#ffffff"
elif [ "$capacity" -ge 50 ]; then
color="#ffffff"
color="#ffffff"
elif [ "$capacity" -ge 25 ]; then
color="#ffff00"
color="#ffff00"
else
color="#ff0000"
warn="❗"
color="#ff0000"
warn="❗"
fi
if [[ $status != "Discharging" ]]; then
time_remaining=""
fi
[ -z $warn ] && warn=" "
[ "$status" = "Charging" ] && color="#ffffff"
printf "<span color='%s'>%s%s%s</span>\n" "$color" "$(echo "$status" | sed -e "s/,//;s/Discharging/🔋/;s/Not Charging/🛑/;s/Charging/🔌/;s/Unknown/♻️/;s/Full/⚡/;s/ 0*/ /g;s/ :/ /g")" "$warn" "$(echo "$capacity" | sed -e 's/$/%/')"
printf "<span color='%s'>%s%s%s%%%s</span>\n" \
"$color" \
"$(echo "$status" | sed -e "s/,//;s/Discharging/🔋/;s/Not Charging/🛑/;s/Charging/🔌/;s/Unknown/♻️/;s/Full/⚡/;s/ 0*/ /g;s/ :/ /g")" \
"$warn" \
"$capacity" \
"$time_remaining"