neodotfiles/user/.config/suckless/dwmblocks/scripts/block_battery

103 lines
2.3 KiB
Text
Raw Normal View History

2022-12-20 00:15:21 +00:00
#!/bin/bash
# Loop through all attached batteries and format the info
currntpwr=$(powerprofilesctl get)
if [ "${currntpwr}" = "performance" ]; then
2023-05-26 06:32:03 +00:00
pwr=" | 󰓅 Performance "
2022-12-20 00:15:21 +00:00
elif [ "${currntpwr}" = "balanced" ]; then
2023-05-26 06:32:03 +00:00
pwr=" | 󰾅 Balanced "
2022-12-20 00:15:21 +00:00
elif [ "${currntpwr}" = "power-saver" ]; then
2023-05-26 06:32:03 +00:00
pwr=" | 󰾆 PowerSaver "
2022-12-20 00:15:21 +00:00
fi
2023-05-26 06:32:03 +00:00
for battery in /sys/class/power_supply/BAT?*; do
# If non-first battery, print a space separator.
[ -n "${capacity+x}" ] && printf " "
2023-05-26 06:32:03 +00:00
# Sets up the status and capacity
case "$(cat "$battery/status" 2>&1)" in
"Full")
status=" 󰁹"
;;
"Discharging")
# Calculates the percentage of remaining charge
percentage="$(cat "$battery/capacity" 2>&1)"
# Updates the status icon based on the battery percentage
2023-05-28 06:21:31 +00:00
if [ "$percentage" -le 20 ]
then
status=" 󰁻"
2023-05-28 06:21:31 +00:00
elif [ "$percentage" -le 30 ]
then
status=" 󰁼"
2023-05-28 06:21:31 +00:00
elif [ "$percentage" -le 40 ]
then
status=" 󰁽"
2023-05-28 06:21:31 +00:00
elif [ "$percentage" -le 50 ]
then
status=" 󰁾"
2023-05-28 06:21:31 +00:00
elif [ "$percentage" -le 60 ]
then
status=" 󰁿"
2023-05-28 06:21:31 +00:00
elif [ "$percentage" -le 70 ]
then
status=" 󰂀"
2023-05-28 06:21:31 +00:00
elif [ "$percentage" -le 80 ]
then
status=" 󰂁"
2023-05-28 06:21:31 +00:00
elif [ "$percentage" -le 90 ]
then
status=" 󰂂"
2023-05-28 06:21:31 +00:00
else
status=" 󰁹"
fi
;;
"Charging")
# Calculates the percentage of remaining charge
percentage="$(cat "$battery/capacity" 2>&1)"
# Updates the status icon based on the battery percentage while charging
2023-05-28 06:21:31 +00:00
if [ "$percentage" -le 10 ]
then
status=" 󰢜"
2023-05-28 06:21:31 +00:00
elif [ "$percentage" -le 20 ]
then
status=" 󰂆"
2023-05-28 06:21:31 +00:00
elif [ "$percentage" -le 30 ]
then
status=" 󰂇"
2023-05-28 06:21:31 +00:00
elif [ "$percentage" -le 40 ]
then
status=" 󰂈"
2023-05-28 06:21:31 +00:00
elif [ "$percentage" -le 50 ]
then
status=" 󰢝"
2023-05-28 06:21:31 +00:00
elif [ "$percentage" -le 60 ]
then
status=" 󰂉"
2023-05-28 06:21:31 +00:00
elif [ "$percentage" -le 70 ]
then
status=" 󰢞"
2023-05-28 06:21:31 +00:00
elif [ "$percentage" -le 80 ]
then
status=" 󰂊"
2023-05-28 06:21:31 +00:00
elif [ "$percentage" -le 90 ]
then
status=" 󰂋"
else
status=" 󰂅"
fi
;;
"Not charging")
status=" 󰂃"
;;
"Unknown")
status=" 󰂃"
;;
2023-05-26 06:32:03 +00:00
*) exit 1 ;;
esac
2023-05-26 06:32:03 +00:00
# Will make a warn variable if discharging and low
[ "$status" = " 󰁺" ] && [ "$percentage" -le 10 ] && warn="󱈸"
2023-05-26 06:32:03 +00:00
# Prints the info
printf "%s%s%d%%%s" "$status" "$warn " "$percentage" "$pwr"; unset warn
2023-05-26 06:32:03 +00:00
done && printf "\\n"