From 49c423c7e0a6d5193b77e60107e7c329da28d987 Mon Sep 17 00:00:00 2001 From: 927589452 <927589452@users.noreply.github.com> Date: Wed, 9 Oct 2019 19:24:44 +0200 Subject: battery: add support for sysctl in FreeBSD (#8155) --- plugins/battery/battery.plugin.zsh | 49 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'plugins/battery') diff --git a/plugins/battery/battery.plugin.zsh b/plugins/battery/battery.plugin.zsh index 8f398cfb3..6b6684716 100644 --- a/plugins/battery/battery.plugin.zsh +++ b/plugins/battery/battery.plugin.zsh @@ -7,6 +7,9 @@ # Email: neuralsandwich@gmail.com # # Modified to add support for Apple Mac # ########################################### +# Author: J (927589452) # +# Modified to add support for FreeBSD # +########################################### if [[ "$OSTYPE" = darwin* ]] ; then @@ -64,6 +67,52 @@ if [[ "$OSTYPE" = darwin* ]] ; then [[ $(ioreg -rc "AppleSmartBattery"| grep '^.*"IsCharging"\ =\ ' | sed -e 's/^.*"IsCharging"\ =\ //') == "Yes" ]] } +elif [[ "$OSTYPE" = freebsd* ]] ; then + + function battery_is_charging() { + [[ $(sysctl -n hw.acpi.battery.state) -eq 2 ]] + } + + function battery_pct() { + if (( $+commands[sysctl] )) ; then + echo "$(sysctl -n hw.acpi.battery.life)" + fi + } + + function battery_pct_remaining() { + if [ ! $(battery_is_charging) ] ; then + battery_pct + else + echo "External Power" + fi + } + + function battery_time_remaining() { + remaining_time=$(sysctl -n hw.acpi.battery.time) + if [[ $remaining_time -ge 0 ]] ; then + # calculation from https://www.unix.com/shell-programming-and-scripting/23695-convert-minutes-hours-minutes-seconds.html + ((hour=$remaining_time/60)) + ((minute=$remaining_time-$hour*60)) + echo $hour:$minute + fi + } + + function battery_pct_prompt() { + b=$(battery_pct_remaining) + if [ ! $(battery_is_charging) ] ; then + if [ $b -gt 50 ] ; then + color='green' + elif [ $b -gt 20 ] ; then + color='yellow' + else + color='red' + fi + echo "%{$fg[$color]%}$(battery_pct_remaining)%%%{$reset_color%}" + else + echo "∞" + fi + } + elif [[ "$OSTYPE" = linux* ]] ; then function battery_is_charging() { -- cgit v1.2.3-70-g09d2 From 1bd7a7ad21c51268ba0f9a0d8b643a82b5788a6a Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Tue, 11 Feb 2020 13:32:13 +0100 Subject: Fix calculation for battery percentage (#4774) Co-authored-by: Michael Wolman --- plugins/battery/battery.plugin.zsh | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) (limited to 'plugins/battery') diff --git a/plugins/battery/battery.plugin.zsh b/plugins/battery/battery.plugin.zsh index 6b6684716..5322dd18a 100644 --- a/plugins/battery/battery.plugin.zsh +++ b/plugins/battery/battery.plugin.zsh @@ -169,8 +169,8 @@ fi function battery_level_gauge() { local gauge_slots=${BATTERY_GAUGE_SLOTS:-10}; - local green_threshold=${BATTERY_GREEN_THRESHOLD:-6}; - local yellow_threshold=${BATTERY_YELLOW_THRESHOLD:-4}; + local green_threshold=${BATTERY_GREEN_THRESHOLD:-$(( gauge_slots * 0.6 ))}; + local yellow_threshold=${BATTERY_YELLOW_THRESHOLD:-$(( gauge_slots * 0.4 ))}; local color_green=${BATTERY_COLOR_GREEN:-%F{green}}; local color_yellow=${BATTERY_COLOR_YELLOW:-%F{yellow}}; local color_red=${BATTERY_COLOR_RED:-%F{red}}; @@ -183,26 +183,35 @@ function battery_level_gauge() { local charging_symbol=${BATTERY_CHARGING_SYMBOL:-'⚡'}; local battery_remaining_percentage=$(battery_pct); + local filled empty gauge_color if [[ $battery_remaining_percentage =~ [0-9]+ ]]; then - local filled=$(((( $battery_remaining_percentage + $gauge_slots - 1) / $gauge_slots))); - local empty=$(($gauge_slots - $filled)); + filled=$(( ($battery_remaining_percentage * $gauge_slots) / 100 )); + empty=$(( $gauge_slots - $filled )); - if [[ $filled -gt $green_threshold ]]; then local gauge_color=$color_green; - elif [[ $filled -gt $yellow_threshold ]]; then local gauge_color=$color_yellow; - else local gauge_color=$color_red; + if [[ $filled -gt $green_threshold ]]; then + gauge_color=$color_green; + elif [[ $filled -gt $yellow_threshold ]]; then + gauge_color=$color_yellow; + else + gauge_color=$color_red; fi else - local filled=$gauge_slots; - local empty=0; + filled=$gauge_slots; + empty=0; filled_symbol=${BATTERY_UNKNOWN_SYMBOL:-'.'}; fi - local charging=' ' && battery_is_charging && charging=$charging_symbol; + local charging=' ' + battery_is_charging && charging=$charging_symbol; + # Charging status and prefix printf ${charging_color//\%/\%\%}$charging${color_reset//\%/\%\%}${battery_prefix//\%/\%\%}${gauge_color//\%/\%\%} - printf ${filled_symbol//\%/\%\%}'%.0s' {1..$filled} + # Filled slots + [[ $filled -gt 0 ]] && printf ${filled_symbol//\%/\%\%}'%.0s' {1..$filled} + # Empty slots [[ $filled -lt $gauge_slots ]] && printf ${empty_symbol//\%/\%\%}'%.0s' {1..$empty} + # Suffix printf ${color_reset//\%/\%\%}${battery_suffix//\%/\%\%}${color_reset//\%/\%\%} } -- cgit v1.2.3-70-g09d2 From 39e61614f206f6a552f53c8d1a8b956d8c9ab32f Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Tue, 11 Feb 2020 13:49:04 +0100 Subject: Clean up Linux battery commands and syntax --- plugins/battery/battery.plugin.zsh | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'plugins/battery') diff --git a/plugins/battery/battery.plugin.zsh b/plugins/battery/battery.plugin.zsh index 5322dd18a..c1037ad26 100644 --- a/plugins/battery/battery.plugin.zsh +++ b/plugins/battery/battery.plugin.zsh @@ -116,17 +116,17 @@ elif [[ "$OSTYPE" = freebsd* ]] ; then elif [[ "$OSTYPE" = linux* ]] ; then function battery_is_charging() { - ! [[ $(acpi 2>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]] + ! acpi 2>/dev/null | command grep -q '^Battery.*Discharging' } function battery_pct() { if (( $+commands[acpi] )) ; then - echo "$(acpi 2>/dev/null | cut -f2 -d ',' | tr -cd '[:digit:]')" + acpi 2>/dev/null | cut -f2 -d ',' | tr -cd '[:digit:]' fi } function battery_pct_remaining() { - if [ ! $(battery_is_charging) ] ; then + if ! battery_is_charging; then battery_pct else echo "External Power" @@ -134,15 +134,17 @@ elif [[ "$OSTYPE" = linux* ]] ; then } function battery_time_remaining() { - if [[ $(acpi 2>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]] ; then - echo $(acpi 2>/dev/null | cut -f3 -d ',') + if ! battery_is_charging; then + acpi 2>/dev/null | cut -f3 -d ',' fi } function battery_pct_prompt() { - b=$(battery_pct_remaining) - if [[ $(acpi 2>/dev/null | grep -c '^Battery.*Discharging') -gt 0 ]] ; then - if [ $b -gt 50 ] ; then + local b=$(battery_pct_remaining) + if battery_is_charging; then + echo "∞" + else + if [[ $b -gt 50 ]]; then color='green' elif [ $b -gt 20 ] ; then color='yellow' @@ -150,8 +152,6 @@ elif [[ "$OSTYPE" = linux* ]] ; then color='red' fi echo "%{$fg[$color]%}$(battery_pct_remaining)%%%{$reset_color%}" - else - echo "∞" fi } -- cgit v1.2.3-70-g09d2 From 5f6f7b6e8de8e6b64f07deb6b185535a8cdbe03e Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Tue, 11 Feb 2020 14:17:46 +0100 Subject: Various syntax fixes and function naming equivalence - Fix code style - Fix local definitions - Don't declare unnecessary variables - Use `command` before grep --- plugins/battery/battery.plugin.zsh | 136 +++++++++++++++++-------------------- 1 file changed, 64 insertions(+), 72 deletions(-) (limited to 'plugins/battery') diff --git a/plugins/battery/battery.plugin.zsh b/plugins/battery/battery.plugin.zsh index c1037ad26..7c5bf6f82 100644 --- a/plugins/battery/battery.plugin.zsh +++ b/plugins/battery/battery.plugin.zsh @@ -11,22 +11,21 @@ # Modified to add support for FreeBSD # ########################################### -if [[ "$OSTYPE" = darwin* ]] ; then +if [[ "$OSTYPE" = darwin* ]]; then - function battery_pct() { - local smart_battery_status="$(ioreg -rc "AppleSmartBattery")" - typeset -F maxcapacity=$(echo $smart_battery_status | grep '^.*"MaxCapacity"\ =\ ' | sed -e 's/^.*"MaxCapacity"\ =\ //') - typeset -F currentcapacity=$(echo $smart_battery_status | grep '^.*"CurrentCapacity"\ =\ ' | sed -e 's/^.*CurrentCapacity"\ =\ //') - integer i=$(((currentcapacity/maxcapacity) * 100)) - echo $i + function battery_is_charging() { + ioreg -rc AppleSmartBattery | command grep -q '^.*"ExternalConnected"\ =\ Yes' } - function plugged_in() { - [ $(ioreg -rc AppleSmartBattery | grep -c '^.*"ExternalConnected"\ =\ Yes') -eq 1 ] + function battery_pct() { + local smart_battery_status="$(ioreg -rc AppleSmartBattery)" + local -F maxcapacity=$(command grep '^.*"MaxCapacity"\ =\ ' <<< $smart_battery_status | sed -e 's/^.*"MaxCapacity"\ =\ //') + local -F currentcapacity=$(command grep '^.*"CurrentCapacity"\ =\ ' <<< $smart_battery_status | sed -e 's/^.*CurrentCapacity"\ =\ //') + echo $(( (currentcapacity/maxcapacity) * 100 )) } function battery_pct_remaining() { - if plugged_in ; then + if battery_is_charging; then echo "External Power" else battery_pct @@ -35,9 +34,9 @@ if [[ "$OSTYPE" = darwin* ]] ; then function battery_time_remaining() { local smart_battery_status="$(ioreg -rc "AppleSmartBattery")" - if [[ $(echo $smart_battery_status | grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]] ; then - timeremaining=$(echo $smart_battery_status | grep '^.*"AvgTimeToEmpty"\ =\ ' | sed -e 's/^.*"AvgTimeToEmpty"\ =\ //') - if [ $timeremaining -gt 720 ] ; then + if [[ $(echo $smart_battery_status | command grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]]; then + timeremaining=$(echo $smart_battery_status | command grep '^.*"AvgTimeToEmpty"\ =\ ' | sed -e 's/^.*"AvgTimeToEmpty"\ =\ //') + if [ $timeremaining -gt 720 ]; then echo "::" else echo "~$((timeremaining / 60)):$((timeremaining % 60))" @@ -48,11 +47,11 @@ if [[ "$OSTYPE" = darwin* ]] ; then } function battery_pct_prompt () { - if [[ $(ioreg -rc AppleSmartBattery | grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]] ; then + if ioreg -rc AppleSmartBattery | command grep -q '^.*"ExternalConnected"\ =\ No'; then b=$(battery_pct_remaining) - if [ $b -gt 50 ] ; then + if [[ $b -gt 50 ]]; then color='green' - elif [ $b -gt 20 ] ; then + elif [[ $b -gt 20 ]]; then color='yellow' else color='red' @@ -63,24 +62,20 @@ if [[ "$OSTYPE" = darwin* ]] ; then fi } - function battery_is_charging() { - [[ $(ioreg -rc "AppleSmartBattery"| grep '^.*"IsCharging"\ =\ ' | sed -e 's/^.*"IsCharging"\ =\ //') == "Yes" ]] - } - -elif [[ "$OSTYPE" = freebsd* ]] ; then +elif [[ "$OSTYPE" = freebsd* ]]; then function battery_is_charging() { [[ $(sysctl -n hw.acpi.battery.state) -eq 2 ]] } function battery_pct() { - if (( $+commands[sysctl] )) ; then - echo "$(sysctl -n hw.acpi.battery.life)" + if (( $+commands[sysctl] )); then + sysctl -n hw.acpi.battery.life fi } function battery_pct_remaining() { - if [ ! $(battery_is_charging) ] ; then + if ! battery_is_charging; then battery_pct else echo "External Power" @@ -88,39 +83,40 @@ elif [[ "$OSTYPE" = freebsd* ]] ; then } function battery_time_remaining() { + local remaining_time remaining_time=$(sysctl -n hw.acpi.battery.time) - if [[ $remaining_time -ge 0 ]] ; then - # calculation from https://www.unix.com/shell-programming-and-scripting/23695-convert-minutes-hours-minutes-seconds.html - ((hour=$remaining_time/60)) - ((minute=$remaining_time-$hour*60)) - echo $hour:$minute + if [[ $remaining_time -ge 0 ]]; then + ((hour = $remaining_time / 60 )) + ((minute = $remaining_time % 60 )) + printf %02d:%02d $hour $minute fi } function battery_pct_prompt() { + local b color b=$(battery_pct_remaining) - if [ ! $(battery_is_charging) ] ; then - if [ $b -gt 50 ] ; then + if battery_is_charging; then + echo "∞" + else + if [[ $b -gt 50 ]]; then color='green' - elif [ $b -gt 20 ] ; then + elif [[ $b -gt 20 ]]; then color='yellow' else color='red' fi echo "%{$fg[$color]%}$(battery_pct_remaining)%%%{$reset_color%}" - else - echo "∞" fi } -elif [[ "$OSTYPE" = linux* ]] ; then +elif [[ "$OSTYPE" = linux* ]]; then function battery_is_charging() { ! acpi 2>/dev/null | command grep -q '^Battery.*Discharging' } function battery_pct() { - if (( $+commands[acpi] )) ; then + if (( $+commands[acpi] )); then acpi 2>/dev/null | cut -f2 -d ',' | tr -cd '[:digit:]' fi } @@ -140,13 +136,14 @@ elif [[ "$OSTYPE" = linux* ]] ; then } function battery_pct_prompt() { - local b=$(battery_pct_remaining) + local b color + b=$(battery_pct_remaining) if battery_is_charging; then echo "∞" else if [[ $b -gt 50 ]]; then color='green' - elif [ $b -gt 20 ] ; then + elif [[ $b -gt 20 ]]; then color='yellow' else color='red' @@ -157,53 +154,50 @@ elif [[ "$OSTYPE" = linux* ]] ; then else # Empty functions so we don't cause errors in prompts - function battery_pct_remaining() { - } - - function battery_time_remaining() { - } - - function battery_pct_prompt() { - } + function battery_is_charging { false } + function battery_pct \ + battery_pct_remaining \ + battery_time_remaining \ + battery_pct_prompt { } fi function battery_level_gauge() { - local gauge_slots=${BATTERY_GAUGE_SLOTS:-10}; - local green_threshold=${BATTERY_GREEN_THRESHOLD:-$(( gauge_slots * 0.6 ))}; - local yellow_threshold=${BATTERY_YELLOW_THRESHOLD:-$(( gauge_slots * 0.4 ))}; - local color_green=${BATTERY_COLOR_GREEN:-%F{green}}; - local color_yellow=${BATTERY_COLOR_YELLOW:-%F{yellow}}; - local color_red=${BATTERY_COLOR_RED:-%F{red}}; - local color_reset=${BATTERY_COLOR_RESET:-%{%f%k%b%}}; - local battery_prefix=${BATTERY_GAUGE_PREFIX:-'['}; - local battery_suffix=${BATTERY_GAUGE_SUFFIX:-']'}; - local filled_symbol=${BATTERY_GAUGE_FILLED_SYMBOL:-'▶'}; - local empty_symbol=${BATTERY_GAUGE_EMPTY_SYMBOL:-'▷'}; - local charging_color=${BATTERY_CHARGING_COLOR:-$color_yellow}; - local charging_symbol=${BATTERY_CHARGING_SYMBOL:-'⚡'}; - - local battery_remaining_percentage=$(battery_pct); + local gauge_slots=${BATTERY_GAUGE_SLOTS:-10} + local green_threshold=${BATTERY_GREEN_THRESHOLD:-$(( gauge_slots * 0.6 ))} + local yellow_threshold=${BATTERY_YELLOW_THRESHOLD:-$(( gauge_slots * 0.4 ))} + local color_green=${BATTERY_COLOR_GREEN:-%F{green}} + local color_yellow=${BATTERY_COLOR_YELLOW:-%F{yellow}} + local color_red=${BATTERY_COLOR_RED:-%F{red}} + local color_reset=${BATTERY_COLOR_RESET:-%{%f%k%b%}} + local battery_prefix=${BATTERY_GAUGE_PREFIX:-'['} + local battery_suffix=${BATTERY_GAUGE_SUFFIX:-']'} + local filled_symbol=${BATTERY_GAUGE_FILLED_SYMBOL:-'▶'} + local empty_symbol=${BATTERY_GAUGE_EMPTY_SYMBOL:-'▷'} + local charging_color=${BATTERY_CHARGING_COLOR:-$color_yellow} + local charging_symbol=${BATTERY_CHARGING_SYMBOL:-'⚡'} + + local battery_remaining_percentage=$(battery_pct) local filled empty gauge_color if [[ $battery_remaining_percentage =~ [0-9]+ ]]; then - filled=$(( ($battery_remaining_percentage * $gauge_slots) / 100 )); - empty=$(( $gauge_slots - $filled )); + filled=$(( ($battery_remaining_percentage * $gauge_slots) / 100 )) + empty=$(( $gauge_slots - $filled )) if [[ $filled -gt $green_threshold ]]; then - gauge_color=$color_green; + gauge_color=$color_green elif [[ $filled -gt $yellow_threshold ]]; then - gauge_color=$color_yellow; + gauge_color=$color_yellow else - gauge_color=$color_red; + gauge_color=$color_red fi else - filled=$gauge_slots; - empty=0; - filled_symbol=${BATTERY_UNKNOWN_SYMBOL:-'.'}; + filled=$gauge_slots + empty=0 + filled_symbol=${BATTERY_UNKNOWN_SYMBOL:-'.'} fi local charging=' ' - battery_is_charging && charging=$charging_symbol; + battery_is_charging && charging=$charging_symbol # Charging status and prefix printf ${charging_color//\%/\%\%}$charging${color_reset//\%/\%\%}${battery_prefix//\%/\%\%}${gauge_color//\%/\%\%} @@ -214,5 +208,3 @@ function battery_level_gauge() { # Suffix printf ${color_reset//\%/\%\%}${battery_suffix//\%/\%\%}${color_reset//\%/\%\%} } - - -- cgit v1.2.3-70-g09d2 From b8b87629157b30fc00d0b4af62dd0cf422896346 Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Thu, 24 Dec 2015 04:11:22 +0200 Subject: Report only active battery (#4726) On a system with multiple batteries (like thinkpads) report percentage and time remaining only for the active battery (the one being discharged). Ideally we should report all batteries, but acpi only shows time remaining for the active battery. Also callers of these functions expect a single return value. This is still better than reporting 596% remaining (like it did on my laptop). For the reference, the output of acpi command with multiple batteries looks like this: Battery 0: Unknown, 5% Battery 1: Discharging, 86%, 03:14:04 remaining --- plugins/battery/battery.plugin.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins/battery') diff --git a/plugins/battery/battery.plugin.zsh b/plugins/battery/battery.plugin.zsh index 7c5bf6f82..d1adcd0b6 100644 --- a/plugins/battery/battery.plugin.zsh +++ b/plugins/battery/battery.plugin.zsh @@ -117,7 +117,7 @@ elif [[ "$OSTYPE" = linux* ]]; then function battery_pct() { if (( $+commands[acpi] )); then - acpi 2>/dev/null | cut -f2 -d ',' | tr -cd '[:digit:]' + acpi 2>/dev/null | command grep -E '^Battery.*(Disc|C)harging' | cut -f2 -d ',' | tr -cd '[:digit:]' fi } -- cgit v1.2.3-70-g09d2 From 15a03744a9e87b8dd371a4fbac456a73d27d32d5 Mon Sep 17 00:00:00 2001 From: GregoireW Date: Thu, 17 Oct 2019 11:30:24 +0200 Subject: Remove invalid batteries (#8275) --- plugins/battery/battery.plugin.zsh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'plugins/battery') diff --git a/plugins/battery/battery.plugin.zsh b/plugins/battery/battery.plugin.zsh index d1adcd0b6..6fe801c9f 100644 --- a/plugins/battery/battery.plugin.zsh +++ b/plugins/battery/battery.plugin.zsh @@ -112,12 +112,12 @@ elif [[ "$OSTYPE" = freebsd* ]]; then elif [[ "$OSTYPE" = linux* ]]; then function battery_is_charging() { - ! acpi 2>/dev/null | command grep -q '^Battery.*Discharging' + ! acpi 2>/dev/null | command grep -v "rate information unavailable" | command grep -q '^Battery.*Discharging' } function battery_pct() { if (( $+commands[acpi] )); then - acpi 2>/dev/null | command grep -E '^Battery.*(Disc|C)harging' | cut -f2 -d ',' | tr -cd '[:digit:]' + acpi 2>/dev/null | command grep -v "rate information unavailable" | command grep -E '^Battery.*(Disc|C)harging' | cut -f2 -d ',' | tr -cd '[:digit:]' fi } @@ -131,7 +131,7 @@ elif [[ "$OSTYPE" = linux* ]]; then function battery_time_remaining() { if ! battery_is_charging; then - acpi 2>/dev/null | cut -f3 -d ',' + acpi 2>/dev/null | command grep -v "rate information unavailable" | cut -f3 -d ',' fi } -- cgit v1.2.3-70-g09d2 From 7290a08bf6098993b783877c8feab4c33cf49c38 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Tue, 25 Feb 2020 11:54:52 +0100 Subject: battery: fix floating point output in macOS Fixes #8676 --- plugins/battery/battery.plugin.zsh | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) (limited to 'plugins/battery') diff --git a/plugins/battery/battery.plugin.zsh b/plugins/battery/battery.plugin.zsh index 6fe801c9f..857ab6e8c 100644 --- a/plugins/battery/battery.plugin.zsh +++ b/plugins/battery/battery.plugin.zsh @@ -21,7 +21,8 @@ if [[ "$OSTYPE" = darwin* ]]; then local smart_battery_status="$(ioreg -rc AppleSmartBattery)" local -F maxcapacity=$(command grep '^.*"MaxCapacity"\ =\ ' <<< $smart_battery_status | sed -e 's/^.*"MaxCapacity"\ =\ //') local -F currentcapacity=$(command grep '^.*"CurrentCapacity"\ =\ ' <<< $smart_battery_status | sed -e 's/^.*CurrentCapacity"\ =\ //') - echo $(( (currentcapacity/maxcapacity) * 100 )) + local -i pct=$(( (currentcapacity/maxcapacity) * 100 )) + echo $pct } function battery_pct_remaining() { @@ -47,16 +48,17 @@ if [[ "$OSTYPE" = darwin* ]]; then } function battery_pct_prompt () { + local battery_pct color if ioreg -rc AppleSmartBattery | command grep -q '^.*"ExternalConnected"\ =\ No'; then - b=$(battery_pct_remaining) - if [[ $b -gt 50 ]]; then + battery_pct=$(battery_pct_remaining) + if [[ $battery_pct -gt 50 ]]; then color='green' - elif [[ $b -gt 20 ]]; then + elif [[ $battery_pct -gt 20 ]]; then color='yellow' else color='red' fi - echo "%{$fg[$color]%}[$(battery_pct_remaining)%%]%{$reset_color%}" + echo "%{$fg[$color]%}[${battery_pct}%%]%{$reset_color%}" else echo "∞" fi @@ -93,19 +95,19 @@ elif [[ "$OSTYPE" = freebsd* ]]; then } function battery_pct_prompt() { - local b color - b=$(battery_pct_remaining) + local battery_pct color + battery_pct=$(battery_pct_remaining) if battery_is_charging; then echo "∞" else - if [[ $b -gt 50 ]]; then + if [[ $battery_pct -gt 50 ]]; then color='green' - elif [[ $b -gt 20 ]]; then + elif [[ $battery_pct -gt 20 ]]; then color='yellow' else color='red' fi - echo "%{$fg[$color]%}$(battery_pct_remaining)%%%{$reset_color%}" + echo "%{$fg[$color]%}${battery_pct}%%%{$reset_color%}" fi } @@ -136,19 +138,19 @@ elif [[ "$OSTYPE" = linux* ]]; then } function battery_pct_prompt() { - local b color - b=$(battery_pct_remaining) + local battery_pct color + battery_pct=$(battery_pct_remaining) if battery_is_charging; then echo "∞" else - if [[ $b -gt 50 ]]; then + if [[ $battery_pct -gt 50 ]]; then color='green' - elif [[ $b -gt 20 ]]; then + elif [[ $battery_pct -gt 20 ]]; then color='yellow' else color='red' fi - echo "%{$fg[$color]%}$(battery_pct_remaining)%%%{$reset_color%}" + echo "%{$fg[$color]%}${battery_pct}%%%{$reset_color%}" fi } -- cgit v1.2.3-70-g09d2 From 3e9e385d98da148a7ad8e8d99da35ce6b7aae9ca Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Tue, 25 Feb 2020 15:41:17 +0100 Subject: battery: remove redundant grep calls in battery_pct function --- plugins/battery/battery.plugin.zsh | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'plugins/battery') diff --git a/plugins/battery/battery.plugin.zsh b/plugins/battery/battery.plugin.zsh index 857ab6e8c..4c4d0d4fc 100644 --- a/plugins/battery/battery.plugin.zsh +++ b/plugins/battery/battery.plugin.zsh @@ -18,11 +18,10 @@ if [[ "$OSTYPE" = darwin* ]]; then } function battery_pct() { - local smart_battery_status="$(ioreg -rc AppleSmartBattery)" - local -F maxcapacity=$(command grep '^.*"MaxCapacity"\ =\ ' <<< $smart_battery_status | sed -e 's/^.*"MaxCapacity"\ =\ //') - local -F currentcapacity=$(command grep '^.*"CurrentCapacity"\ =\ ' <<< $smart_battery_status | sed -e 's/^.*CurrentCapacity"\ =\ //') - local -i pct=$(( (currentcapacity/maxcapacity) * 100 )) - echo $pct + local battery_status="$(ioreg -rc AppleSmartBattery)" + local -i capacity=$(sed -n -e '/MaxCapacity/s/^.*"MaxCapacity"\ =\ //p' <<< $battery_status) + local -i current=$(sed -n -e '/CurrentCapacity/s/^.*"CurrentCapacity"\ =\ //p' <<< $battery_status) + echo $(( current * 100 / capacity )) } function battery_pct_remaining() { -- cgit v1.2.3-70-g09d2