From c3e3dd737adb6b29ab1bcf28080ee5383c9f206a Mon Sep 17 00:00:00 2001 From: KawaiiAmber Date: Mon, 14 Dec 2020 19:50:09 -0700 Subject: [PATCH] Case block and function is more effecient according to the old for x in seq 10000 test --- .local/bin/statusbar/battery | 45 ++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 12 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? +# Check if battery directories are detected +[ ! -e /sys/class/power_supply/BAT?* ] && echo "No battery found" && exit 1 + +# 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" +} + +# Loop through all attached batteries and format the info +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) - - # If it is discharging and 25% or less, we will add a ❗ as a warning. - [ "$capacity" -le 25 ] && [ "$status" = "🔋" ] && warn="❗" - - printf "%s%s%s%% " "$status" "$warn" "$capacity" - unset warn -done | sed 's/ *$//' + format $battery +done && return 0