Change the linux battery script to be triggered by changes in battery state via upower.
This commit is contained in:
parent
161b7c1354
commit
f7599e9564
@ -1,72 +1,63 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Read battery status in FreeBSD
|
||||
# Read battery status in Linux
|
||||
set -euo pipefail
|
||||
IFS=$'\n\t'
|
||||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
|
||||
SLEEP_INTERVAL=${SLEEP_INTERVAL:-30}
|
||||
batteries=$(upower --enumerate | grep BAT)
|
||||
|
||||
batteries=$(find /sys/class/power_supply -name 'BAT*')
|
||||
function generate_battery_update {
|
||||
local battery_path=$1
|
||||
local battery_status=$(upower --show-info "$battery_path")
|
||||
local battery_state=$(grep 'state:' <<<"$battery_status" | awk '{print $2}')
|
||||
local battery_percentage=$(grep 'percentage:' <<<"$battery_status" | awk '{print $2}')
|
||||
battery_percentage=${battery_percentage%\%}
|
||||
local time_remaining=$(grep 'time to empty:' <<<"$battery_status" | sed 's/\W*time to empty:\W*//g')
|
||||
local battery_icon=""
|
||||
local class=""
|
||||
|
||||
while true; do
|
||||
text=""
|
||||
tooltip=""
|
||||
percentage=""
|
||||
class=""
|
||||
|
||||
while read bat; do
|
||||
battery_now=$(cat "$bat/charge_now")
|
||||
battery_capacity=$(cat "$bat/charge_full")
|
||||
battery_percentage=$(( battery_now * 100 / battery_capacity ))
|
||||
battery_status=$(cat "$bat/status")
|
||||
seconds_remaining="-1"
|
||||
|
||||
if [ $battery_status = "Charging" ]; then
|
||||
if [ $battery_percentage -eq 100 ]; then
|
||||
battery_icon=""
|
||||
else
|
||||
battery_icon=""
|
||||
fi
|
||||
elif [ $battery_percentage -le 20 ]; then
|
||||
battery_icon=""
|
||||
elif [ $battery_percentage -le 40 ]; then
|
||||
battery_icon=""
|
||||
elif [ $battery_percentage -le 60 ]; then
|
||||
battery_icon=""
|
||||
elif [ $battery_percentage -le 80 ]; then
|
||||
battery_icon=""
|
||||
elif [ $battery_percentage -lt 100 ]; then
|
||||
battery_icon=""
|
||||
if [ $battery_state = "charging" ]; then
|
||||
if [ $battery_percentage -eq 100 ]; then
|
||||
battery_icon=""
|
||||
else
|
||||
battery_icon=""
|
||||
battery_icon=""
|
||||
fi
|
||||
elif [ $battery_percentage -le 20 ]; then
|
||||
battery_icon=""
|
||||
elif [ $battery_percentage -le 40 ]; then
|
||||
battery_icon=""
|
||||
elif [ $battery_percentage -le 60 ]; then
|
||||
battery_icon=""
|
||||
elif [ $battery_percentage -le 80 ]; then
|
||||
battery_icon=""
|
||||
elif [ $battery_percentage -lt 100 ]; then
|
||||
battery_icon=""
|
||||
else
|
||||
battery_icon=""
|
||||
fi
|
||||
|
||||
if [ $battery_percentage -le 15 ]; then
|
||||
class="critical"
|
||||
elif [ $battery_percentage -le 30 ]; then
|
||||
class="warning"
|
||||
fi
|
||||
|
||||
if [ $seconds_remaining -eq -1 ]; then
|
||||
time_remaining="unknown time remaining."
|
||||
else
|
||||
time_remaining=$(printf '%dh:%dm:%ds\n' $((seconds_remaining/3600)) $((seconds_remaining%3600/60)) $((seconds_remaining%60)) )
|
||||
fi
|
||||
|
||||
text="$text${battery_percentage}% ${battery_icon}"
|
||||
tooltip="$tooltip${time_remaining}"
|
||||
percentage="$percentage${battery_percentage}"
|
||||
done<<<"$batteries"
|
||||
|
||||
|
||||
jq --unbuffered --compact-output <<EOF
|
||||
if [ $battery_percentage -le 15 ]; then
|
||||
class="critical"
|
||||
elif [ $battery_percentage -le 30 ]; then
|
||||
class="warning"
|
||||
fi
|
||||
jq --unbuffered --compact-output <<EOF
|
||||
{
|
||||
"text":"$text",
|
||||
"tooltip":"$tooltip",
|
||||
"percentage":${percentage},
|
||||
"text":"${battery_percentage}% ${battery_icon}",
|
||||
"tooltip":"$time_remaining",
|
||||
"percentage":${battery_percentage},
|
||||
"class":"${class}"
|
||||
}
|
||||
EOF
|
||||
sleep $SLEEP_INTERVAL
|
||||
}
|
||||
|
||||
while read bat; do
|
||||
generate_battery_update "$bat"
|
||||
done<<<"$batteries"
|
||||
|
||||
upower --monitor | grep --line-buffered 'device changed:' | while read l; do
|
||||
while read bat; do
|
||||
generate_battery_update "$bat"
|
||||
done<<<"$batteries"
|
||||
done
|
||||
|
@ -2,6 +2,7 @@
|
||||
package:
|
||||
name:
|
||||
- bc # For waybar sound script
|
||||
- upower # Needed for waybar battery script
|
||||
state: present
|
||||
|
||||
- name: Install scripts
|
||||
|
Loading…
x
Reference in New Issue
Block a user