From 88a8956d5ad310089d9ee32afdb7319636004ef4 Mon Sep 17 00:00:00 2001 From: Rajiv Bharadwaj Date: Wed, 4 Mar 2020 22:16:36 -0500 Subject: [PATCH 1/3] Added a text indicator for battery charge suggestion --- battery.tmux | 2 ++ scripts/battery_chargetime.sh | 59 +++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 scripts/battery_chargetime.sh diff --git a/battery.tmux b/battery.tmux index 0c666b8..1070f96 100755 --- a/battery.tmux +++ b/battery.tmux @@ -17,6 +17,7 @@ battery_interpolation=( "\#{battery_icon_status}" "\#{battery_percentage}" "\#{battery_remain}" + "\#{battery_should_charge}" ) battery_commands=( @@ -32,6 +33,7 @@ battery_commands=( "#($CURRENT_DIR/scripts/battery_icon_status.sh)" "#($CURRENT_DIR/scripts/battery_percentage.sh)" "#($CURRENT_DIR/scripts/battery_remain.sh)" + "#(~/Documents/programming/tmux-battery/scripts/battery_chargetime.sh)" ) set_tmux_option() { diff --git a/scripts/battery_chargetime.sh b/scripts/battery_chargetime.sh new file mode 100644 index 0000000..c66188b --- /dev/null +++ b/scripts/battery_chargetime.sh @@ -0,0 +1,59 @@ +#!/usr/bin/env bash + +CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +source "$CURRENT_DIR/helpers.sh" + +get_percent() { + # percentage displayed in the 2nd field of the 2nd row + if command_exists "pmset"; then + pmset -g batt | grep -o "[0-9]\{1,3\}%" + elif command_exists "acpi"; then + acpi -b | grep -m 1 -Eo "[0-9]+%" + elif command_exists "upower"; then + # use DisplayDevice if available otherwise battery + local battery=$(upower -e | grep -E 'battery|DisplayDevice'| tail -n1) + if [ -z "$battery" ]; then + return + fi + local percentage=$(upower -i $battery | awk '/percentage:/ {print $2}') + if [ "$percentage" ]; then + echo ${percentage%.*%} + return + fi + local energy + local energy_full + energy=$(upower -i $battery | awk -v nrg="$energy" '/energy:/ {print nrg+$2}') + energy_full=$(upower -i $battery | awk -v nrgfull="$energy_full" '/energy-full:/ {print nrgfull+$2}') + if [ -n "$energy" ] && [ -n "$energy_full" ]; then + echo $energy $energy_full | awk '{printf("%d%%", ($1/$2)*100)}' + fi + elif command_exists "termux-battery-status"; then + termux-battery-status | jq -r '.percentage' | awk '{printf("%d%%", $1)}' + elif command_exists "apm"; then + apm -l + fi +} + +recommend_charging() { + local percentage = $(get_percent | sed -e 's/%//g') + if [[ percentage -le 20 ]]; then + if [[ $(battery_status) == "discharging" ]]; then + echo "CHARGE IMMEDIATELY FOOL" + else + echo "NOT REQUIRED" + fi + elif [[ percentage -ge 80 ]]; then + if [[ $(battery_status) == "charging" ]]; then + echo "REMOVE CHARGING" + else + echo "NOT REQUIRED" + fi + fi +} + +main() { + recommend_charging +} +main + From 4ed5012662799dbf3aef90d1965e468f6b0dba80 Mon Sep 17 00:00:00 2001 From: Rajiv Bharadwaj Date: Wed, 4 Mar 2020 22:30:51 -0500 Subject: [PATCH 2/3] Finished text version of charging feature --- battery.tmux | 2 +- scripts/battery_chargetime.sh | 20 ++++++++------------ 2 files changed, 9 insertions(+), 13 deletions(-) mode change 100644 => 100755 scripts/battery_chargetime.sh diff --git a/battery.tmux b/battery.tmux index 1070f96..5457e92 100755 --- a/battery.tmux +++ b/battery.tmux @@ -33,7 +33,7 @@ battery_commands=( "#($CURRENT_DIR/scripts/battery_icon_status.sh)" "#($CURRENT_DIR/scripts/battery_percentage.sh)" "#($CURRENT_DIR/scripts/battery_remain.sh)" - "#(~/Documents/programming/tmux-battery/scripts/battery_chargetime.sh)" + "#($CURRENT_DIR/scripts/battery_chargetime.sh)" ) set_tmux_option() { diff --git a/scripts/battery_chargetime.sh b/scripts/battery_chargetime.sh old mode 100644 new mode 100755 index c66188b..5091c8a --- a/scripts/battery_chargetime.sh +++ b/scripts/battery_chargetime.sh @@ -36,18 +36,15 @@ get_percent() { } recommend_charging() { - local percentage = $(get_percent | sed -e 's/%//g') - if [[ percentage -le 20 ]]; then - if [[ $(battery_status) == "discharging" ]]; then - echo "CHARGE IMMEDIATELY FOOL" - else - echo "NOT REQUIRED" + local percentage=$(get_percent) + percentage=$(echo ${percentage} | sed -e 's/%//g') + if [[ $percentage -lt 20 ]]; then + if [[ "$(battery_status)" == "discharging" ]]; then + echo "CHARGE IMMEDIATELY" fi - elif [[ percentage -ge 80 ]]; then - if [[ $(battery_status) == "charging" ]]; then - echo "REMOVE CHARGING" - else - echo "NOT REQUIRED" + elif [[ percentage -gt 80 ]]; then + if [[ "$(battery_status)" == "charging" ]]; then + echo "STOP CHARGING" fi fi } @@ -56,4 +53,3 @@ main() { recommend_charging } main - From 67559571bd3ec404b6e01ef069cf4ec2d8ef0763 Mon Sep 17 00:00:00 2001 From: Rajiv Bharadwaj Date: Thu, 5 Mar 2020 13:01:02 -0500 Subject: [PATCH 3/3] Fixed a bug that didn't work on 100% battery --- scripts/battery_chargetime.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/battery_chargetime.sh b/scripts/battery_chargetime.sh index 5091c8a..4ee12ed 100755 --- a/scripts/battery_chargetime.sh +++ b/scripts/battery_chargetime.sh @@ -42,8 +42,8 @@ recommend_charging() { if [[ "$(battery_status)" == "discharging" ]]; then echo "CHARGE IMMEDIATELY" fi - elif [[ percentage -gt 80 ]]; then - if [[ "$(battery_status)" == "charging" ]]; then + elif [[ $percentage -gt 80 ]]; then + if [[ "$(battery_status)" != "discharging" ]]; then echo "STOP CHARGING" fi fi