Case block and function is more effecient according to the old for x in seq 10000 test

This commit is contained in:
KawaiiAmber 2020-12-14 19:50:09 -07:00
parent c3eefd7a68
commit c3e3dd737a

View file

@ -19,16 +19,37 @@ esac
# acpi alternative # 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 # 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. # Check if battery directories are detected
for battery in /sys/class/power_supply/BAT? [ ! -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 do
# Get its remaining capacity and charge status. format $battery
capacity=$(cat "$battery"/capacity 2>/dev/null) || break done && return 0
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/ *$//'