2022-10-28 04:27:32 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
#
|
2022-11-15 03:22:43 +00:00
|
|
|
# Read battery status in Linux
|
2022-10-28 04:27:32 +00:00
|
|
|
set -euo pipefail
|
|
|
|
IFS=$'\n\t'
|
|
|
|
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
|
|
|
|
2022-11-15 03:22:43 +00:00
|
|
|
batteries=$(upower --enumerate | grep 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')
|
2022-11-15 03:35:53 +00:00
|
|
|
local tooltip="$time_remaining until empty"
|
|
|
|
if [ -z "$time_remaining" ]; then
|
|
|
|
time_remaining=$(grep 'time to full:' <<<"$battery_status" | sed 's/\W*time to full:\W*//g')
|
|
|
|
tooltip="$time_remaining until full"
|
|
|
|
fi
|
2022-11-15 03:22:43 +00:00
|
|
|
local battery_icon=""
|
|
|
|
local class=""
|
|
|
|
|
|
|
|
if [ $battery_state = "charging" ]; then
|
|
|
|
if [ $battery_percentage -eq 100 ]; then
|
|
|
|
battery_icon=""
|
2022-10-28 04:27:32 +00:00
|
|
|
else
|
2022-11-15 03:22:43 +00:00
|
|
|
battery_icon=""
|
2022-10-28 04:27:32 +00:00
|
|
|
fi
|
2022-11-15 03:22:43 +00:00
|
|
|
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
|
|
|
|
jq --unbuffered --compact-output <<EOF
|
2022-10-28 04:27:32 +00:00
|
|
|
{
|
2022-11-15 03:22:43 +00:00
|
|
|
"text":"${battery_percentage}% ${battery_icon}",
|
2022-11-15 03:35:53 +00:00
|
|
|
"tooltip":"$tooltip",
|
2022-11-15 03:22:43 +00:00
|
|
|
"percentage":${battery_percentage},
|
2022-11-12 17:52:26 +00:00
|
|
|
"class":"${class}"
|
2022-10-28 04:27:32 +00:00
|
|
|
}
|
|
|
|
EOF
|
2022-11-15 03:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
2022-10-28 04:27:32 +00:00
|
|
|
done
|