dwmblocks/scripts/dwmblocks-battery

75 lines
2.3 KiB
Plaintext
Raw Normal View History

2023-02-21 12:15:57 -08:00
#!/usr/bin/env zsh
if [[ "$(uname)" = 'Linux' ]]; then
let charge_full="$(cat '/sys/class/power_supply/BAT0/charge_full')."
function get_battery_percent {
let charge_now="$(cat '/sys/class/power_supply/BAT0/charge_now')."
printf '%.0f' "$((charge_now / charge_full * 100))"
}
function is_adapted_connected {
let connected="$(cat /sys/class/power_supply/ADP1/online)"
2023-05-04 15:04:51 -07:00
((${connected} == 1))
}
function get_discharging_battery_icon {
let charge_percent="${1}"
if ((${charge_percent} <= 10)); then
echo -n '󰂃'
elif ((${charge_percent} <= 20)); then
echo -n '󰁻'
elif ((${charge_percent} <= 30)); then
echo -n '󰁼'
elif ((${charge_percent} <= 40)); then
echo -n '󰁽'
elif ((${charge_percent} <= 50)); then
echo -n '󰁾'
elif ((${charge_percent} <= 60)); then
echo -n '󰁿'
elif ((${charge_percent} <= 70)); then
echo -n '󰂀'
elif ((${charge_percent} <= 80)); then
echo -n '󰂁'
elif ((${charge_percent} < 100)); then
echo -n '󰂂'
2023-02-21 12:15:57 -08:00
else
2023-05-04 15:04:51 -07:00
echo -n '󰁹'
2023-02-21 12:15:57 -08:00
fi
}
2023-05-04 15:04:51 -07:00
function get_charging_battery_icon {
2023-02-21 12:15:57 -08:00
let charge_percent="${1}"
2023-05-04 15:04:51 -07:00
if ((${charge_percent} <= 10)); then
2023-07-28 23:49:29 -07:00
echo -n '󰢜 '
2023-02-21 12:15:57 -08:00
elif ((${charge_percent} <= 20)); then
2023-07-28 23:49:29 -07:00
echo -n '󰂆 '
2023-02-21 12:15:57 -08:00
elif ((${charge_percent} <= 30)); then
2023-07-28 23:49:29 -07:00
echo -n '󰂇 '
2023-02-21 12:15:57 -08:00
elif ((${charge_percent} <= 40)); then
2023-07-28 23:49:29 -07:00
echo -n '󰂈 '
2023-02-21 12:15:57 -08:00
elif ((${charge_percent} <= 50)); then
2023-07-28 23:49:29 -07:00
echo -n '󰢝 '
2023-02-21 12:15:57 -08:00
elif ((${charge_percent} <= 60)); then
2023-07-28 23:49:29 -07:00
echo -n '󰢝 '
2023-02-21 12:15:57 -08:00
elif ((${charge_percent} <= 70)); then
2023-07-28 23:49:29 -07:00
echo -n '󰢞 '
2023-02-21 12:15:57 -08:00
elif ((${charge_percent} <= 80)); then
2023-07-28 23:49:29 -07:00
echo -n '󰂊 '
2023-02-21 12:15:57 -08:00
elif ((${charge_percent} < 100)); then
2023-07-28 23:49:29 -07:00
echo -n '󰂋 '
2023-02-21 12:15:57 -08:00
else
2023-07-28 23:49:29 -07:00
echo -n '󰂅 '
2023-02-21 12:15:57 -08:00
fi
}
let cp="$(get_battery_percent)"
2023-05-04 15:04:51 -07:00
is_adapted_connected \
&& get_charging_battery_icon "${cp}" \
|| get_discharging_battery_icon "${cp}"
2023-02-21 12:15:57 -08:00
printf '%3d%%\n' "${cp}"
# Linux ends here
else
echo "${0}: error: unknown os: \"$(uname)\"" >&2
exit 1
fi