diff options
| author | KawaiiAmber <japaneselearning101@gmail.com> | 2020-12-14 19:50:09 -0700 |
|---|---|---|
| committer | KawaiiAmber <japaneselearning101@gmail.com> | 2020-12-14 19:50:09 -0700 |
| commit | c3e3dd737adb6b29ab1bcf28080ee5383c9f206a (patch) | |
| tree | 93b2d97dc1dd1b89ea38e460f5cfe418a4c22cc5 | |
| parent | c3eefd7a683ad764f38fc39f1bf4aad97f78d767 (diff) | |
| download | eibhear-c3e3dd737adb6b29ab1bcf28080ee5383c9f206a.tar.gz eibhear-c3e3dd737adb6b29ab1bcf28080ee5383c9f206a.tar.zst eibhear-c3e3dd737adb6b29ab1bcf28080ee5383c9f206a.zip | |
Case block and function is more effecient according to the old for x in seq 10000 test
| -rwxr-xr-x | .local/bin/statusbar/battery | 43 |
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 |
