From 8bbb1b383d60a02e198134abbae098b037fd5317 Mon Sep 17 00:00:00 2001 From: tangb4c Date: Sun, 21 Jan 2024 20:19:35 +0800 Subject: [PATCH 1/6] new CLI feature: maintain battery in a certain range --- battery.sh | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/battery.sh b/battery.sh index e4bdea6..12d3a56 100755 --- a/battery.sh +++ b/battery.sh @@ -52,10 +52,16 @@ Usage: eg: battery logs 100 battery maintain LEVEL[1-100,stop] - reboot-persistent battery level maintenance: turn off charging above, and on below a certain value - it has the option of a --force-discharge flag that discharges even when plugged in (this does NOT work well with clamshell mode) + battery maintain RANGE[min-max] + reboot-persistent battery level maintenance: + Threshold Mode: + turn off charging above, and on below a certain value. + Range Mode: + turn off charging above the maximum value, and on below the minimum value. + it has the option of a --force-discharge flag that discharges even when plugged in (this does NOT work well with clamshell mode) eg: battery maintain 80 eg: battery maintain stop + eg: battery maintain 20-70 battery charging SETTING[on/off] manually set the battery to (not) charge @@ -436,7 +442,15 @@ if [[ "$action" == "maintain_synchronous" ]]; then # Start charging battery_percentage=$(get_battery_percentage) - log "Charging to and maintaining at $setting% from $battery_percentage%" + IFS=- read lower_threshold upper_threshold <<<"$setting" + if [ -z "$upper_threshold" ];then + upper_threshold=$lower_threshold + fi + if [ "$lower_threshold" -eq "$upper_threshold" ];then + log "Charging to and maintaining at $lower_threshold% from $battery_percentage%" + else + log "Charging to and maintaining in range of $lower_threshold% to $upper_threshold% from $battery_percentage%" + fi # Loop until battery percent is exceeded while true; do @@ -444,15 +458,15 @@ if [[ "$action" == "maintain_synchronous" ]]; then # Keep track of status is_charging=$(get_smc_charging_status) - if [[ "$battery_percentage" -ge "$setting" && "$is_charging" == "enabled" ]]; then + if [[ "$battery_percentage" -ge "$upper_threshold" && "$is_charging" == "enabled" ]]; then - log "Charge above $setting" + log "Charge above $upper_threshold" disable_charging change_magsafe_led_color "green" - elif [[ "$battery_percentage" -lt "$setting" && "$is_charging" == "disabled" ]]; then + elif [[ "$battery_percentage" -lt "$lower_threshold" && "$is_charging" == "disabled" ]]; then - log "Charge below $setting" + log "Charge below $lower_threshold" enable_charging change_magsafe_led_color "orange" @@ -487,8 +501,15 @@ if [[ "$action" == "maintain" ]]; then exit 0 fi + if [[ "$setting" =~ ^[0-9]{1,3}-[0-9]{1,3}$ ]];then + IFS=- read lower_threshold upper_threshold <<<"$setting" + if [ "$lower_threshold" -lt 0 ] || [ "$lower_threshold" -gt 100 ] || + [ "$upper_threshold" -lt 0 ] || [ "$upper_threshold" -gt 100 ] || + [ "$lower_threshold" -gt "$upper_threshold" ];then + log "Error: $setting is not a valid range setting for battery maintain. Please use a range just like 30-80, or an action keyword like 'stop' or 'recover'." + fi # Check if setting is value between 0 and 100 - if ! [[ "$setting" =~ ^[0-9]+$ ]] || [[ "$setting" -lt 0 ]] || [[ "$setting" -gt 100 ]]; then + elif ! [[ "$setting" =~ ^[0-9]+$ ]] || [[ "$setting" -lt 0 ]] || [[ "$setting" -gt 100 ]]; then log "Called with $setting $action" # If non 0-100 setting is not a special keyword, exit with an error. From 1db423498557443838b2244c60cb8c735b1ef3e9 Mon Sep 17 00:00:00 2001 From: tangb4c Date: Sun, 21 Jan 2024 20:34:32 +0800 Subject: [PATCH 2/6] change help message --- battery.sh | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/battery.sh b/battery.sh index 12d3a56..72e72f0 100755 --- a/battery.sh +++ b/battery.sh @@ -51,17 +51,16 @@ Usage: output logs of the battery CLI and GUI eg: battery logs 100 - battery maintain LEVEL[1-100,stop] - battery maintain RANGE[min-max] - reboot-persistent battery level maintenance: - Threshold Mode: + battery maintain LEVEL[value/minvalue-maxvalue/stop] + reboot-persistent battery level maintenance: + Threshold mode: turn off charging above, and on below a certain value. - Range Mode: + Range mode: turn off charging above the maximum value, and on below the minimum value. it has the option of a --force-discharge flag that discharges even when plugged in (this does NOT work well with clamshell mode) - eg: battery maintain 80 + eg: battery maintain 80 // Threshold mode + eg: battery maintain 20-80 // Range mode eg: battery maintain stop - eg: battery maintain 20-70 battery charging SETTING[on/off] manually set the battery to (not) charge From 942899978355e0ddbd36db4e5fc28142e369d575 Mon Sep 17 00:00:00 2001 From: tangb4c Date: Mon, 22 Jan 2024 09:28:04 +0800 Subject: [PATCH 3/6] optimize help text --- battery.sh | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/battery.sh b/battery.sh index 72e72f0..298e367 100755 --- a/battery.sh +++ b/battery.sh @@ -52,15 +52,15 @@ Usage: eg: battery logs 100 battery maintain LEVEL[value/minvalue-maxvalue/stop] - reboot-persistent battery level maintenance: - Threshold mode: + reboot-persistent battery level maintenance. There are two modes: + threshold mode: turn off charging above, and on below a certain value. - Range mode: + range mode: turn off charging above the maximum value, and on below the minimum value. it has the option of a --force-discharge flag that discharges even when plugged in (this does NOT work well with clamshell mode) - eg: battery maintain 80 // Threshold mode - eg: battery maintain 20-80 // Range mode - eg: battery maintain stop + eg: battery maintain 80 // threshold mode + eg: battery maintain 20-80 // range mode + eg: battery maintain stop // disable maintain mode battery charging SETTING[on/off] manually set the battery to (not) charge @@ -441,14 +441,14 @@ if [[ "$action" == "maintain_synchronous" ]]; then # Start charging battery_percentage=$(get_battery_percentage) - IFS=- read lower_threshold upper_threshold <<<"$setting" - if [ -z "$upper_threshold" ];then - upper_threshold=$lower_threshold - fi - if [ "$lower_threshold" -eq "$upper_threshold" ];then - log "Charging to and maintaining at $lower_threshold% from $battery_percentage%" - else - log "Charging to and maintaining in range of $lower_threshold% to $upper_threshold% from $battery_percentage%" + IFS=- read lower_threshold upper_threshold <<<"$setting" + if [ -z "$upper_threshold" ];then + upper_threshold=$lower_threshold + fi + if [ "$lower_threshold" -eq "$upper_threshold" ];then + log "Charging to and maintaining at $lower_threshold% from $battery_percentage%" + else + log "Charging to and maintaining in range of $lower_threshold% to $upper_threshold% from $battery_percentage%" fi # Loop until battery percent is exceeded @@ -500,13 +500,13 @@ if [[ "$action" == "maintain" ]]; then exit 0 fi - if [[ "$setting" =~ ^[0-9]{1,3}-[0-9]{1,3}$ ]];then - IFS=- read lower_threshold upper_threshold <<<"$setting" - if [ "$lower_threshold" -lt 0 ] || [ "$lower_threshold" -gt 100 ] || - [ "$upper_threshold" -lt 0 ] || [ "$upper_threshold" -gt 100 ] || - [ "$lower_threshold" -gt "$upper_threshold" ];then - log "Error: $setting is not a valid range setting for battery maintain. Please use a range just like 30-80, or an action keyword like 'stop' or 'recover'." - fi + if [[ "$setting" =~ ^[0-9]{1,3}-[0-9]{1,3}$ ]];then + IFS=- read lower_threshold upper_threshold <<<"$setting" + if [ "$lower_threshold" -lt 0 ] || [ "$lower_threshold" -gt 100 ] || + [ "$upper_threshold" -lt 0 ] || [ "$upper_threshold" -gt 100 ] || + [ "$lower_threshold" -gt "$upper_threshold" ];then + log "Error: $setting is not a valid range setting for battery maintain. The min/max value should between 0 and 100. A range example like 30-80" + fi # Check if setting is value between 0 and 100 elif ! [[ "$setting" =~ ^[0-9]+$ ]] || [[ "$setting" -lt 0 ]] || [[ "$setting" -gt 100 ]]; then From 7295f0fc72250decd3c1ad6571561e1df8b9a892 Mon Sep 17 00:00:00 2001 From: tangb4c Date: Mon, 22 Jan 2024 23:39:12 +0800 Subject: [PATCH 4/6] update readme and version --- README.md | 18 +++++++++++++----- battery.sh | 2 +- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8496fa1..4c831ab 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,8 @@ Example usage: ```shell # This will enable charging when your battery dips under 80, and disable it when it exceeds 80 battery maintain 80 +# This will enable charging when your battery dips under 30, and disable it when it exceeds 70 +battery maintain 30-70 ``` After running a command like `battery charging off` you can verify the change visually by looking at the battery icon: @@ -69,17 +71,23 @@ After running `battery charging on` you will see it change to this: For help, run `battery` without parameters: ``` -Battery CLI utility v1.0.1 +Battery CLI utility v1.2.0 Usage: battery status output battery SMC status, % and time remaining - battery maintain LEVEL[1-100,stop] - reboot-persistent battery level maintenance: turn off charging above, and on below a certain value - eg: battery maintain 80 - eg: battery maintain stop + battery maintain LEVEL[value/minvalue-maxvalue/stop] + reboot-persistent battery level maintenance. There are two modes: + threshold mode: + turn off charging above, and on below a certain value. + range mode: + turn off charging above the maximum value, and on below the minimum value. + it has the option of a --force-discharge flag that discharges even when plugged in (this does NOT work well with clamshell mode) + eg: battery maintain 80 // threshold mode + eg: battery maintain 20-80 // range mode + eg: battery maintain stop // disable maintain mode battery charging SETTING[on/off] manually set the battery to (not) charge diff --git a/battery.sh b/battery.sh index 298e367..6553ab7 100755 --- a/battery.sh +++ b/battery.sh @@ -4,7 +4,7 @@ ## Update management ## variables are used by this binary as well at the update script ## ############### -BATTERY_CLI_VERSION="v1.1.6" +BATTERY_CLI_VERSION="v1.2.0" # Path fixes for unexpected environments PATH=/bin:/usr/bin:/usr/local/bin:/usr/sbin:/opt/homebrew/bin:/opt/homebrew/sbin:/opt/homebrew From 7350d27c419273901b4bd91ac045b0f61eed40dd Mon Sep 17 00:00:00 2001 From: tangb4c Date: Wed, 14 Feb 2024 21:44:08 +0800 Subject: [PATCH 5/6] change range mode regex, and fix bug --- battery.sh | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/battery.sh b/battery.sh index 6553ab7..5b73007 100755 --- a/battery.sh +++ b/battery.sh @@ -500,20 +500,23 @@ if [[ "$action" == "maintain" ]]; then exit 0 fi - if [[ "$setting" =~ ^[0-9]{1,3}-[0-9]{1,3}$ ]];then + # Check if setting is range mode + if [[ "$setting" =~ ^[0-9]+-[0-9]+$ ]];then + log "Called with range $setting $action" IFS=- read lower_threshold upper_threshold <<<"$setting" - if [ "$lower_threshold" -lt 0 ] || [ "$lower_threshold" -gt 100 ] || - [ "$upper_threshold" -lt 0 ] || [ "$upper_threshold" -gt 100 ] || + if [ "$lower_threshold" -lt 1 ] || [ "$lower_threshold" -gt 100 ] || + [ "$upper_threshold" -lt 1 ] || [ "$upper_threshold" -gt 100 ] || [ "$lower_threshold" -gt "$upper_threshold" ];then - log "Error: $setting is not a valid range setting for battery maintain. The min/max value should between 0 and 100. A range example like 30-80" + log "Error: $setting is not a valid range setting for battery maintain. The min/max value should between 1 and 100. A range example like 30-80" + exit 1 fi - # Check if setting is value between 0 and 100 - elif ! [[ "$setting" =~ ^[0-9]+$ ]] || [[ "$setting" -lt 0 ]] || [[ "$setting" -gt 100 ]]; then + # Check if setting is value between 1 and 100 + elif ! [[ "$setting" =~ ^[0-9]+$ ]] || [[ "$setting" -lt 1 ]] || [[ "$setting" -gt 100 ]]; then log "Called with $setting $action" # If non 0-100 setting is not a special keyword, exit with an error. if ! { [[ "$setting" == "stop" ]] || [[ "$setting" == "recover" ]]; }; then - log "Error: $setting is not a valid setting for battery maintain. Please use a number between 0 and 100, or an action keyword like 'stop' or 'recover'." + log "Error: $setting is not a valid setting for battery maintain. Please use a number between 1 and 100, or an action keyword like 'stop' or 'recover'." exit 1 fi From 877641cce511a2d15a4575df37729b524f3f3065 Mon Sep 17 00:00:00 2001 From: Ira Abramov Date: Tue, 9 Apr 2024 22:31:50 +0300 Subject: [PATCH 6/6] Add some comments, improve code a bit --- battery.sh | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/battery.sh b/battery.sh index 6fb5f23..58be9c3 100755 --- a/battery.sh +++ b/battery.sh @@ -466,11 +466,11 @@ if [[ "$action" == "maintain_synchronous" ]]; then # Start charging battery_percentage=$(get_battery_percentage) - IFS=- read lower_threshold upper_threshold <<<"$setting" - if [ -z "$upper_threshold" ];then - upper_threshold=$lower_threshold - fi - if [ "$lower_threshold" -eq "$upper_threshold" ];then + # Extract values from $setting + lower_threshold=${setting/-*/} + upper_threshold=${setting/*-/} + if [[ "$lower_threshold" -eq "$upper_threshold" ]]; then + # the string had no "-", both threasholds are the same log "Charging to and maintaining at $lower_threshold% from $battery_percentage%" else log "Charging to and maintaining in range of $lower_threshold% to $upper_threshold% from $battery_percentage%" @@ -525,26 +525,26 @@ if [[ "$action" == "maintain" ]]; then exit 0 fi - # Check if setting is range mode - if [[ "$setting" =~ ^[0-9]+-[0-9]+$ ]];then + # Check if setting is a legal range + if [[ "$setting" =~ ^[0-9]+-?[0-9]+$ ]]; then log "Called with range $setting $action" - IFS=- read lower_threshold upper_threshold <<<"$setting" - if [ "$lower_threshold" -lt 1 ] || [ "$lower_threshold" -gt 100 ] || - [ "$upper_threshold" -lt 1 ] || [ "$upper_threshold" -gt 100 ] || - [ "$lower_threshold" -gt "$upper_threshold" ];then + # Extract upper and lower if necessary and then check if values are legal + lower_threshold=${setting/-*/} + upper_threshold=${setting/*-/} + if [[ "$lower_threshold" -lt 1 ]] || [[ "$lower_threshold" -gt 100 ]] || + [[ "$upper_threshold" -lt 1 ]] || [[ "$upper_threshold" -gt 100 ]] || + [[ "$lower_threshold" -gt "$upper_threshold" ]]; then log "Error: $setting is not a valid range setting for battery maintain. The min/max value should between 1 and 100. A range example like 30-80" exit 1 fi # Check if setting is value between 1 and 100 - elif ! [[ "$setting" =~ ^[0-9]+$ ]] || [[ "$setting" -lt 1 ]] || [[ "$setting" -gt 100 ]]; then - + else log "Called with $setting $action" - # If non 0-100 setting is not a special keyword, exit with an error. + # If setting is not a special keyword, exit with an error. if ! { [[ "$setting" == "stop" ]] || [[ "$setting" == "recover" ]]; }; then log "Error: $setting is not a valid setting for battery maintain. Please use a number between 1 and 100, or an action keyword like 'stop' or 'recover'." exit 1 fi - fi # Start maintenance script