From c3e3dd737adb6b29ab1bcf28080ee5383c9f206a Mon Sep 17 00:00:00 2001 From: KawaiiAmber Date: Mon, 14 Dec 2020 19:50:09 -0700 Subject: [PATCH 1/3] 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 From 2548c0bc2046dbd8910f2ab74291a3537598fc66 Mon Sep 17 00:00:00 2001 From: KawaiiAmber Date: Tue, 15 Dec 2020 14:01:53 -0700 Subject: [PATCH 2/3] No longer calls cat twice --- .local/bin/statusbar/battery | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.local/bin/statusbar/battery b/.local/bin/statusbar/battery index c778ec4..48df164 100755 --- a/.local/bin/statusbar/battery +++ b/.local/bin/statusbar/battery @@ -25,8 +25,6 @@ esac # 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 @@ -44,6 +42,8 @@ format() ;; esac capacity=$(cat "$1/capacity") + # Will make a warn variable if discharging and low + [ "$status" = "🔋 " ] && [ "$capacity" -le 25 ] && local warn="❗ " # Prints the info printf "%s%s%d%%\n" "$status" "$warn" "$capacity" } From ef82a470492d7542ac4ce960a1b724e0f5861ad1 Mon Sep 17 00:00:00 2001 From: Luke Smith Date: Tue, 15 Dec 2020 16:29:17 -0500 Subject: [PATCH 3/3] conform style --- .local/bin/statusbar/battery | 43 +++++++++++------------------------- 1 file changed, 13 insertions(+), 30 deletions(-) diff --git a/.local/bin/statusbar/battery b/.local/bin/statusbar/battery index 48df164..ac4d9a4 100755 --- a/.local/bin/statusbar/battery +++ b/.local/bin/statusbar/battery @@ -16,40 +16,23 @@ case $BLOCK_BUTTON in 6) "$TERMINAL" -e "$EDITOR" "$0" ;; 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 - # 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() -{ - # 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") - # Will make a warn variable if discharging and low - [ "$status" = "🔋 " ] && [ "$capacity" -le 25 ] && local warn="❗ " - # 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 - format $battery + # Sets up the status and capacity + status=$(cat "$battery/status") + case "$status" in + "Full") status="⚡" ;; + "Discharging") status="🔋" ;; + "Not charging") status="🛑" ;; + "Unknown") status="♻️" ;; + esac + capacity=$(cat "$battery/capacity") + # Will make a warn variable if discharging and low + [ "$status" = "🔋" ] && [ "$capacity" -le 25 ] && local warn="❗" + # Prints the info + printf "%s%s%d%%\n" "$status" "$warn" "$capacity" done && return 0