From 7bed3af90c16954b0038e0886cdf8697cf9c72eb Mon Sep 17 00:00:00 2001 From: Skladnayazebra Date: Fri, 7 Nov 2025 19:08:07 +0100 Subject: Use swich/case for battery icon, minor refactor, comments --- tmux/battery.sh | 61 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 32 insertions(+), 29 deletions(-) (limited to 'tmux') diff --git a/tmux/battery.sh b/tmux/battery.sh index 96ca839..d981ae5 100755 --- a/tmux/battery.sh +++ b/tmux/battery.sh @@ -1,47 +1,50 @@ round() { awk -v n=$1 -v d=$2 'BEGIN{print int((n+d/2)/d) * d}'; } -# e.g. 69.420% -percentage_raw=$(upower -i /org/freedesktop/UPower/devices/DisplayDevice | awk '/percentage:/ {print $2}') # e.g. 7h est_time=$(upower -i /org/freedesktop/UPower/devices/DisplayDevice | awk '/time to empty:/ {print $4substr($5, 1, 1)}') - -# charging / discharging / pending-charge +# charging / discharging / pending-charge / ??? chg_state=$(upower -i /org/freedesktop/UPower/devices/DisplayDevice | awk '/state:/ {print $2}') +# e.g. 69.420% +percentage_raw=$(upower -i /org/freedesktop/UPower/devices/DisplayDevice | awk '/percentage:/ {print $2}') +# rounded to integer +percentage_int=$(printf "%.0f%" "${percentage_raw%%%}") -# rounded to tens: 10 20 30 etc. -percentage_10s=$(round "${percentage_raw%%%}" 10); - -# when battery is low, highlight it -if [[ $percentage_10s -le "10" ]]; then +# when battery is low, highlight the widget +if [[ $percentage_int -le "10" ]]; then printf "#[[bg=red,fg=brightwhite]]"; -elif [[ $percentage_10s -le "20" ]]; then +elif [[ $percentage_int -le "15" ]]; then printf "#[[bg=brightyellow]]"; fi -printf " "; +printf " "; # left padding + +# battery icon if [[ $chg_state == "charging" ]]; then printf "󰂄" else - if [[ $percentage_10s -le "10" ]]; then printf "󰁺"; fi - if [[ $percentage_10s -eq "20" ]]; then printf "󰁻"; fi - if [[ $percentage_10s -eq "30" ]]; then printf "󰁼"; fi - if [[ $percentage_10s -eq "40" ]]; then printf "󰁽"; fi - if [[ $percentage_10s -eq "50" ]]; then printf "󰁾"; fi - if [[ $percentage_10s -eq "60" ]]; then printf "󰁿"; fi - if [[ $percentage_10s -eq "70" ]]; then printf "󰂀"; fi - if [[ $percentage_10s -eq "80" ]]; then printf "󰂁"; fi - if [[ $percentage_10s -eq "90" ]]; then printf "󰂂"; fi - if [[ $percentage_10s -eq "100" ]]; then printf "󰁹"; fi + case "$(round "$percentage_int" 10)" in + "10") printf "󰁺";; + "20") printf "󰁻";; + "30") printf "󰁼";; + "40") printf "󰁽";; + "50") printf "󰁾";; + "60") printf "󰁿";; + "70") printf "󰂀";; + "80") printf "󰂁";; + "90") printf "󰂂";; + "100") printf "󰁹";; + esac fi -printf " "; -# percentage level rounded to a whole number (69%) -printf "%.0f%%" "${percentage_raw%%%}"; +printf " "; #bat-percent gap + +printf "%s%%" "$percentage_int"; + +# this is only shown when on battery if [[ $est_time ]]; then - printf " "; - # estimated time, if on battery + printf " "; #percent-estimage gap printf " $est_time" fi -printf " "; -# reset style -printf "#[default]"; + +printf " "; # right padding +printf "#[default]"; # reset style -- cgit v1.3.1