From f2c0820ef2dffb64e20c0c42204b7d8da273ecf4 Mon Sep 17 00:00:00 2001 From: Santiago Lo Coco Date: Tue, 9 Apr 2024 22:23:10 +0200 Subject: [PATCH] Refactor and add cache --- Makefile | 2 +- cbattery | 200 ++++++++++++++++++++++++++++++------------------------- 2 files changed, 111 insertions(+), 91 deletions(-) diff --git a/Makefile b/Makefile index 55ba42c..aed0fd4 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ $(INSTALL_PATH): cbattery uninstall: @echo "Removing cbattery from $(DESTDIR)" - @$(DESTDIR)/cbattery revert || (echo "Reverting cbattery configurations failed"; exit 1) + @su $(USER) -c '$(DESTDIR)/cbattery revert' || (echo "Reverting cbattery configurations failed"; exit 1) @rm -f $(INSTALL_PATH) help: diff --git a/cbattery b/cbattery index d491fd2..e5dffd6 100755 --- a/cbattery +++ b/cbattery @@ -2,119 +2,139 @@ set -e -function usage() { - cat << EOF +CACHE_FILE="${XDG_CACHE_HOME:-$HOME/.cache}/cbattery.status" + +usage() { + cat << EOF usage: ${0##*/} [command] - charging [on|off] - Toggle charging - adapter [on|off] - Toggle adapter connection - status - Get status information - revert - Revert to default settings - visudo - Run the script without password - help - Display this help message + charging [on|off] - Toggle charging + adapter [on|off] - Toggle adapter connection + status - Get status information + revert - Revert to default settings + visudo - Run the script without password + help - Display this help message EOF } -function enable_discharging() { - echo "Enabling battery discharging" - sudo smc -k CH0I -w 01 +write_cache() { + echo "$1" > "$CACHE_FILE" } -function disable_discharging() { - echo "Disabling battery discharging" - sudo smc -k CH0I -w 00 +read_cache() { + cat "$CACHE_FILE" 2> /dev/null } -function enable_charging() { - echo "Enabling battery charging" - sudo smc -k CH0B -w 00 - sudo smc -k CH0C -w 00 - disable_discharging +enable_discharging() { + echo "Enabling battery discharging" + sudo smc -k CH0I -w 01 } -function disable_charging() { - echo "Disabling battery charging" - sudo smc -k CH0B -w 02 - sudo smc -k CH0C -w 02 +disable_discharging() { + echo "Disabling battery discharging" + sudo smc -k CH0I -w 00 } -function get_smc_charging_hex() { - smc -k CH0B -r | awk '{print $4}' | sed s:\):: +enable_charging() { + echo "Enabling battery charging" + sudo smc -k CH0B -w 00 + sudo smc -k CH0C -w 00 + disable_discharging + write_cache "enabled" } -function get_smc_charging_status() { - hex_status=$(get_smc_charging_hex) - if [[ "$hex_status" == "00" ]]; then - echo "enabled" - else - echo "disabled" - fi +disable_charging() { + echo "Disabling battery charging" + sudo smc -k CH0B -w 02 + sudo smc -k CH0C -w 02 + write_cache "disabled" } -function get_battery_percentage() { - battery_percentage=$(pmset -g batt | tail -n1 | awk '{print $3}' | sed s:%\;::) - echo "$battery_percentage" +get_smc_charging_hex() { + smc -k CH0B -r | awk '{print $4}' | sed s:\):: } -function install_visudo_file() { - sudoers_file="/private/etc/sudoers.d/cbattery" - if ! smc_loc="$(command -v smc)"; then - echo "'smc' is required but not found." - exit 1 - fi - sed "s:smc:$smc_loc:g" ".visudo" | sudo tee "$sudoers_file" > /dev/null - if ! sudo visudo -c -f "$sudoers_file"; then - echo "Failed to check sudoers file syntax." - sudo rm "$sudoers_file" - exit 1 - fi - sudo chmod 440 "$sudoers_file" +get_smc_charging_status() { + hex_status=$(get_smc_charging_hex) + [[ "$hex_status" == "00" ]] && echo "enabled" || echo "disabled" } -action=$1 -setting=$2 +get_battery_percentage() { + pmset -g batt | awk 'END {print $3}' | sed 's/%;//' +} -if [ -z "$action" ] || [[ "$action" == "help" ]]; then - usage - exit 0 -fi +install_visudo_file() { + sudoers_file="/private/etc/sudoers.d/cbattery" + if ! smc_loc="$(command -v smc)"; then + echo "'smc' is required but not found." + exit 1 + fi + sed "s:smc:$smc_loc:g" ".visudo" | sudo tee "$sudoers_file" > /dev/null + if ! sudo visudo -c -f "$sudoers_file"; then + echo "Failed to check sudoers file syntax." + sudo rm "$sudoers_file" + exit 1 + fi + sudo chmod 440 "$sudoers_file" +} -if [[ "$action" == "revert" ]]; then - enable_charging - disable_discharging - exit 0 -fi +get_cached_status() { + set +e + read_cache_result=$(read_cache) + read_cache_error=$? + set -e + if [ $read_cache_error -ne 0 ]; then + status=$(get_smc_charging_status) + write_cache "$status" + echo "$status" + else + echo "$read_cache_result" + fi +} -if [[ "$action" == "charging" ]]; then - echo "Setting $action to $setting" - if [[ "$setting" == "on" ]]; then - enable_charging - elif [[ "$setting" == "off" ]]; then - disable_charging - fi - exit 0 -fi +runScript() { + action=$1 + setting=$2 -if [[ "$action" == "adapter" ]]; then - echo "Setting $action to $setting" - if [[ "$setting" == "on" ]]; then - enable_discharging - elif [[ "$setting" == "off" ]]; then - disable_discharging - fi - exit 0 -fi + case "$action" in + "help" | "") + usage + ;; + "revert") + enable_charging + disable_discharging + ;; + "charging") + echo "Setting $action to $setting" + case "$setting" in + "on") enable_charging ;; + "off") disable_charging ;; + *) echo "Invalid option: $setting" ;; + esac + ;; + "adapter") + echo "Setting $action to $setting" + case "$setting" in + "on") enable_discharging ;; + "off") disable_discharging ;; + *) echo "Invalid option: $setting" ;; + esac + ;; + "status") + case "$setting" in + "charging") exit "$(get_smc_charging_hex)" ;; + "cache") echo "$(get_cached_status)" ;; + *) echo "Battery at $(get_battery_percentage)%, smc charging $(get_smc_charging_status)" ;; + esac + ;; + "visudo") + install_visudo_file + ;; + *) + echo "Invalid command: $action" + usage + ;; + esac +} -if [[ "$action" == "status" ]]; then - if [[ "$setting" == "charging" ]]; then - exit "$(get_smc_charging_hex)" - else - echo "Battery at $(get_battery_percentage)%, smc charging $(get_smc_charging_status)" - fi - exit 0 -fi - -if [[ "$action" == "visudo" ]]; then - install_visudo_file - exit 0 -fi +runScript "$@"