From dc83d0b7204cf66315471e980729490813b7d915 Mon Sep 17 00:00:00 2001 From: Ryan Voots Date: Thu, 7 Oct 2021 15:16:30 -0400 Subject: fix(battery): support `acpitool` and multiple batteries under Linux (#9609) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Marc Cornellà --- plugins/battery/battery.plugin.zsh | 50 +++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 20 deletions(-) (limited to 'plugins/battery') diff --git a/plugins/battery/battery.plugin.zsh b/plugins/battery/battery.plugin.zsh index a525fd7da..24bff8ae6 100644 --- a/plugins/battery/battery.plugin.zsh +++ b/plugins/battery/battery.plugin.zsh @@ -12,15 +12,12 @@ ########################################### if [[ "$OSTYPE" = darwin* ]]; then - function battery_is_charging() { ioreg -rc AppleSmartBattery | command grep -q '^.*"ExternalConnected"\ =\ Yes' } - function battery_pct() { pmset -g batt | grep -Eo "\d+%" | cut -d% -f1 } - function battery_pct_remaining() { if battery_is_charging; then echo "External Power" @@ -28,7 +25,6 @@ if [[ "$OSTYPE" = darwin* ]]; then battery_pct fi } - function battery_time_remaining() { local smart_battery_status="$(ioreg -rc "AppleSmartBattery")" if [[ $(echo $smart_battery_status | command grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]]; then @@ -42,7 +38,6 @@ if [[ "$OSTYPE" = darwin* ]]; then echo "∞" fi } - function battery_pct_prompt () { local battery_pct color if ioreg -rc AppleSmartBattery | command grep -q '^.*"ExternalConnected"\ =\ No'; then @@ -61,17 +56,14 @@ if [[ "$OSTYPE" = darwin* ]]; then } elif [[ "$OSTYPE" = freebsd* ]]; then - function battery_is_charging() { [[ $(sysctl -n hw.acpi.battery.state) -eq 2 ]] } - function battery_pct() { if (( $+commands[sysctl] )); then sysctl -n hw.acpi.battery.life fi } - function battery_pct_remaining() { if ! battery_is_charging; then battery_pct @@ -79,7 +71,6 @@ elif [[ "$OSTYPE" = freebsd* ]]; then echo "External Power" fi } - function battery_time_remaining() { local remaining_time remaining_time=$(sysctl -n hw.acpi.battery.time) @@ -89,7 +80,6 @@ elif [[ "$OSTYPE" = freebsd* ]]; then printf %02d:%02d $hour $minute fi } - function battery_pct_prompt() { local battery_pct color battery_pct=$(battery_pct_remaining) @@ -106,19 +96,42 @@ elif [[ "$OSTYPE" = freebsd* ]]; then echo "%{$fg[$color]%}${battery_pct}%%%{$reset_color%}" fi } - elif [[ "$OSTYPE" = linux* ]]; then - function battery_is_charging() { - ! acpi 2>/dev/null | command grep -v "rate information unavailable" | command grep -q '^Battery.*Discharging' + if (( $+commands[acpitool] )); then + ! acpitool 2>/dev/null | command grep -qE '^\s+Battery.*Discharging' + elif (( $+commands[acpi] )); then + ! acpi 2>/dev/null | command grep -v "rate information unavailable" | command grep -q '^Battery.*Discharging' + fi } - function battery_pct() { - if (( $+commands[acpi] )); then - acpi 2>/dev/null | command grep -v "rate information unavailable" | command grep -E '^Battery.*(Full|(Disc|C)harging)' | cut -f2 -d ',' | tr -cd '[:digit:]' + if (( $+commands[acpitool] )); then + # Sample output: + # Battery #1 : Unknown, 99.55% + # Battery #2 : Discharging, 49.58%, 01:12:05 + # All batteries : 62.60%, 02:03:03 + acpitool 2>/dev/null | command awk -F, ' + /^\s+All batteries/ { + gsub(/[^0-9.]/, "", $1) + pct=$1 + exit + } + !pct && /^\s+Battery/ { + gsub(/[^0-9.]/, "", $2) + pct=$2 + } + END { print pct } + ' + elif (( $+commands[acpi] )); then + # Sample output: + # Battery 0: Discharging, 0%, rate information unavailable + # Battery 1: Full, 100% + acpi 2>/dev/null | command awk -F, ' + /rate information unavailable/ { next } + /^Battery.*: /{ gsub(/[^0-9]/, "", $2); print $2; exit } + ' fi } - function battery_pct_remaining() { if ! battery_is_charging; then battery_pct @@ -126,13 +139,11 @@ elif [[ "$OSTYPE" = linux* ]]; then echo "External Power" fi } - function battery_time_remaining() { if ! battery_is_charging; then acpi 2>/dev/null | command grep -v "rate information unavailable" | cut -f3 -d ',' fi } - function battery_pct_prompt() { local battery_pct color battery_pct=$(battery_pct_remaining) @@ -149,7 +160,6 @@ elif [[ "$OSTYPE" = linux* ]]; then echo "%{$fg[$color]%}${battery_pct}%%%{$reset_color%}" fi } - else # Empty functions so we don't cause errors in prompts function battery_is_charging { false } -- cgit v1.2.3-70-g09d2 From f26a1ecdf01185218004dd6d15ce02541e6a2b26 Mon Sep 17 00:00:00 2001 From: Avneet Singh <4151485+kalsi-avneet@users.noreply.github.com> Date: Sun, 21 Mar 2021 00:06:33 +0530 Subject: feat(battery): add support for Android via Termux (#9752) --- plugins/battery/README.md | 19 +++++++++++------ plugins/battery/battery.plugin.zsh | 43 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 6 deletions(-) (limited to 'plugins/battery') diff --git a/plugins/battery/README.md b/plugins/battery/README.md index c2554a36d..18e5bd882 100644 --- a/plugins/battery/README.md +++ b/plugins/battery/README.md @@ -8,15 +8,22 @@ To use, add `battery` to the list of plugins in your `.zshrc` file: Then, add the `battery_pct_prompt` function to your custom theme. For example: -``` +```zsh RPROMPT='$(battery_pct_prompt) ...' ``` ## Requirements -On Linux, you must have the `acpi` tool installed on your operating system. +- On Linux, you must have the `acpi` or `acpitool` commands installed on your operating system. + On Debian/Ubuntu, you can do that with `sudo apt install acpi` or `sudo apt install acpitool`. -Here's an example of how to install with apt: -``` -sudo apt-get install acpi -``` +- On Android (via [Termux](https://play.google.com/store/apps/details?id=com.termux)), you must have: + + 1. The `Termux:API` addon app installed: + [Google Play](https://play.google.com/store/apps/details?id=com.termux.api) | [F-Droid](https://f-droid.org/packages/com.termux.api/) + + 2. The `termux-api` package installed within termux: + + ```sh + pkg install termux-api + ``` diff --git a/plugins/battery/battery.plugin.zsh b/plugins/battery/battery.plugin.zsh index 24bff8ae6..8e6afe65e 100644 --- a/plugins/battery/battery.plugin.zsh +++ b/plugins/battery/battery.plugin.zsh @@ -10,6 +10,9 @@ # Author: J (927589452) # # Modified to add support for FreeBSD # ########################################### +# Author: Avneet Singh (kalsi-avneet) # +# Modified to add support for Android # +########################################### if [[ "$OSTYPE" = darwin* ]]; then function battery_is_charging() { @@ -160,6 +163,46 @@ elif [[ "$OSTYPE" = linux* ]]; then echo "%{$fg[$color]%}${battery_pct}%%%{$reset_color%}" fi } +elif [[ "$OSTYPE" = linux-android ]] && (( ${+commands[termux-battery-status]} )); then + function battery_is_charging() { + termux-battery-status 2>/dev/null | command awk '/status/ { exit ($0 ~ /DISCHARGING/) }' + } + function battery_pct() { + # Sample output: + # { + # "health": "GOOD", + # "percentage": 93, + # "plugged": "UNPLUGGED", + # "status": "DISCHARGING", + # "temperature": 29.0, + # "current": 361816 + # } + termux-battery-status 2>/dev/null | command awk '/percentage/ { gsub(/[,]/,""); print $2}' + } + function battery_pct_remaining() { + if ! battery_is_charging; then + battery_pct + else + echo "External Power" + fi + } + function battery_time_remaining() { } # Not available on android + function battery_pct_prompt() { + local battery_pct color + battery_pct=$(battery_pct_remaining) + if battery_is_charging; then + echo "∞" + else + if [[ $battery_pct -gt 50 ]]; then + color='green' + elif [[ $battery_pct -gt 20 ]]; then + color='yellow' + else + color='red' + fi + echo "%{$fg[$color]%}${battery_pct}%%%{$reset_color%}" + fi + } else # Empty functions so we don't cause errors in prompts function battery_is_charging { false } -- cgit v1.2.3-70-g09d2 From 9aeb967581061ca12c1679d3fdce8a5b516a9796 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Fri, 8 Oct 2021 12:34:04 +0200 Subject: fix(battery): fix system check so Termux uses the correct method --- plugins/battery/battery.plugin.zsh | 80 +++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 40 deletions(-) (limited to 'plugins/battery') diff --git a/plugins/battery/battery.plugin.zsh b/plugins/battery/battery.plugin.zsh index 8e6afe65e..0e96b00aa 100644 --- a/plugins/battery/battery.plugin.zsh +++ b/plugins/battery/battery.plugin.zsh @@ -99,6 +99,46 @@ elif [[ "$OSTYPE" = freebsd* ]]; then echo "%{$fg[$color]%}${battery_pct}%%%{$reset_color%}" fi } +elif [[ "$OSTYPE" = linux-android ]] && (( ${+commands[termux-battery-status]} )); then + function battery_is_charging() { + termux-battery-status 2>/dev/null | command awk '/status/ { exit ($0 ~ /DISCHARGING/) }' + } + function battery_pct() { + # Sample output: + # { + # "health": "GOOD", + # "percentage": 93, + # "plugged": "UNPLUGGED", + # "status": "DISCHARGING", + # "temperature": 29.0, + # "current": 361816 + # } + termux-battery-status 2>/dev/null | command awk '/percentage/ { gsub(/[,]/,""); print $2}' + } + function battery_pct_remaining() { + if ! battery_is_charging; then + battery_pct + else + echo "External Power" + fi + } + function battery_time_remaining() { } # Not available on android + function battery_pct_prompt() { + local battery_pct color + battery_pct=$(battery_pct_remaining) + if battery_is_charging; then + echo "∞" + else + if [[ $battery_pct -gt 50 ]]; then + color='green' + elif [[ $battery_pct -gt 20 ]]; then + color='yellow' + else + color='red' + fi + echo "%{$fg[$color]%}${battery_pct}%%%{$reset_color%}" + fi + } elif [[ "$OSTYPE" = linux* ]]; then function battery_is_charging() { if (( $+commands[acpitool] )); then @@ -163,46 +203,6 @@ elif [[ "$OSTYPE" = linux* ]]; then echo "%{$fg[$color]%}${battery_pct}%%%{$reset_color%}" fi } -elif [[ "$OSTYPE" = linux-android ]] && (( ${+commands[termux-battery-status]} )); then - function battery_is_charging() { - termux-battery-status 2>/dev/null | command awk '/status/ { exit ($0 ~ /DISCHARGING/) }' - } - function battery_pct() { - # Sample output: - # { - # "health": "GOOD", - # "percentage": 93, - # "plugged": "UNPLUGGED", - # "status": "DISCHARGING", - # "temperature": 29.0, - # "current": 361816 - # } - termux-battery-status 2>/dev/null | command awk '/percentage/ { gsub(/[,]/,""); print $2}' - } - function battery_pct_remaining() { - if ! battery_is_charging; then - battery_pct - else - echo "External Power" - fi - } - function battery_time_remaining() { } # Not available on android - function battery_pct_prompt() { - local battery_pct color - battery_pct=$(battery_pct_remaining) - if battery_is_charging; then - echo "∞" - else - if [[ $battery_pct -gt 50 ]]; then - color='green' - elif [[ $battery_pct -gt 20 ]]; then - color='yellow' - else - color='red' - fi - echo "%{$fg[$color]%}${battery_pct}%%%{$reset_color%}" - fi - } else # Empty functions so we don't cause errors in prompts function battery_is_charging { false } -- cgit v1.2.3-70-g09d2 From 5fb204fa7469a89f31ffb8ee8a9b3c36bea926a6 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Fri, 8 Oct 2021 15:42:43 +0200 Subject: fix(battery): force battery percentage as integer --- plugins/battery/battery.plugin.zsh | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'plugins/battery') diff --git a/plugins/battery/battery.plugin.zsh b/plugins/battery/battery.plugin.zsh index 0e96b00aa..db5eeb93a 100644 --- a/plugins/battery/battery.plugin.zsh +++ b/plugins/battery/battery.plugin.zsh @@ -153,7 +153,7 @@ elif [[ "$OSTYPE" = linux* ]]; then # Battery #1 : Unknown, 99.55% # Battery #2 : Discharging, 49.58%, 01:12:05 # All batteries : 62.60%, 02:03:03 - acpitool 2>/dev/null | command awk -F, ' + local -i pct=$(acpitool 2>/dev/null | command awk -F, ' /^\s+All batteries/ { gsub(/[^0-9.]/, "", $1) pct=$1 @@ -164,7 +164,8 @@ elif [[ "$OSTYPE" = linux* ]]; then pct=$2 } END { print pct } - ' + ') + echo $pct elif (( $+commands[acpi] )); then # Sample output: # Battery 0: Discharging, 0%, rate information unavailable @@ -227,7 +228,7 @@ function battery_level_gauge() { local charging_color=${BATTERY_CHARGING_COLOR:-$color_yellow} local charging_symbol=${BATTERY_CHARGING_SYMBOL:-'⚡'} - local battery_remaining_percentage=$(battery_pct) + local -i battery_remaining_percentage=$(battery_pct) local filled empty gauge_color if [[ $battery_remaining_percentage =~ [0-9]+ ]]; then -- cgit v1.2.3-70-g09d2