summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-x.local/bin/statusbar/battery43
1 files changed, 32 insertions, 11 deletions
diff --git a/.local/bin/statusbar/battery b/.local/bin/statusbar/battery
index f42d096..c778ec4 100755
--- a/.local/bin/statusbar/battery
+++ b/.local/bin/statusbar/battery
@@ -19,16 +19,37 @@ esac
# acpi alternative
# acpi | sed "s/Battery [0-9]: //;s/[Dd]ischarging, /🔋/;s/[Nn]ot charging, /🛑/;s/[Cc]harging, /🔌/;s/[Uu]nknown, /♻️/;s/[Ff]ull, /⚡/;s/ \(remaining\|until charged\)//"; exit
-# Loop through all attached batteries.
-for battery in /sys/class/power_supply/BAT?
-do
- # Get its remaining capacity and charge status.
- capacity=$(cat "$battery"/capacity 2>/dev/null) || break
- status=$(sed "s/[Dd]ischarging/🔋/;s/[Nn]ot charging/🛑/;s/[Cc]harging/🔌/;s/[Uu]nknown/♻️/;s/[Ff]ull/⚡/" "$battery"/status)
+# Check if battery directories are detected
+[ ! -e /sys/class/power_supply/BAT?* ] && echo "No battery found" && exit 1
- # If it is discharging and 25% or less, we will add a ❗ as a warning.
- [ "$capacity" -le 25 ] && [ "$status" = "🔋" ] && warn="❗"
+# Defines the formatting for the info from the battery folders
+format()
+{
+ # Will make a warn variable if discharging and low
+ [ $(cat "$1/status") = "[Dd]ischarging" ] && [ $(cat "$1/capacity") -le 25 ] && local warn="❗ "
+ # Sets up the status and capacity
+ status=$(cat "$1/status")
+ case "$status" in
+ "Full")
+ status="⚡ "
+ ;;
+ "Discharging")
+ status="🔋 "
+ ;;
+ "Not charging")
+ status="🛑 "
+ ;;
+ "Unknown")
+ status="♻️/ "
+ ;;
+ esac
+ capacity=$(cat "$1/capacity")
+ # Prints the info
+ printf "%s%s%d%%\n" "$status" "$warn" "$capacity"
+}
- printf "%s%s%s%% " "$status" "$warn" "$capacity"
- unset warn
-done | sed 's/ *$//'
+# Loop through all attached batteries and format the info
+for battery in /sys/class/power_supply/BAT?*
+do
+ format $battery
+done && return 0