See the instructions on install smc
here, then append the snippet below to your ~/.config/fish/config.fish
, if you use fish instead of the z shell.
Credit for rewriting to the fish syntax goes to ChatGPT π
# Apple Silicon laptops with firmware > 13.0 have a native charge threshold that does not require any userspace daemon. | |
# This native limit works even when the laptop is sleeping or powered off, making it preferable to the userspace daemon. | |
# However, it only supports fixed thresholds (80% as upper limit and 70% as lower limit). | |
# The CHWA key is used to enable/disable the native limit. 01 = 80% limit, 00 = no limit | |
## | |
set -g smc_command "/usr/local/bin/smc" | |
set -g smc_charge_limit_key "CHWA" | |
set -g smc_charge_limit_status_on "01" | |
set -g smc_charge_limit_status_off "00" | |
function battery-charge-limit-enable | |
if test (uname) = "Darwin" | |
if not test -e $smc_command | |
echo 'SMC command not found!' | |
return 1 | |
end | |
sudo $smc_command -k $smc_charge_limit_key -w $smc_charge_limit_status_on | |
else | |
echo "Not a Darwin system." | |
end | |
end | |
function battery-charge-limit-disable | |
if test (uname) = "Darwin" | |
if not test -e $smc_command | |
echo 'SMC command not found!' | |
return 1 | |
end | |
sudo $smc_command -k $smc_charge_limit_key -w $smc_charge_limit_status_off | |
else | |
echo "Not a Darwin system." | |
end | |
end | |
function battery-charge-limit-status | |
if test (uname) = "Darwin" | |
if not test -e $smc_command | |
echo 'SMC command not found!' | |
return 1 | |
end | |
# Run the smc command and capture its output | |
set status_raw (sudo $smc_command -k $smc_charge_limit_key -r) | |
# Extract the status value from the output | |
if string match -rq 'CHWA\s+\[flag\]\s+\(bytes\s+([0-9a-fA-F]+)\)' -- $status_raw | |
set -l status_value (string match -r 'CHWA\s+\[flag\]\s+\(bytes\s+([0-9a-fA-F]+)\)' -- $status_raw) | |
set status_value $status_value[2] # Extract the matched group | |
switch $status_value | |
case $smc_charge_limit_status_on | |
echo "Charge limit is enabled (80%)." | |
case $smc_charge_limit_status_off | |
echo "Charge limit is disabled (no limit)." | |
case '*' | |
echo "Unknown $smc_charge_limit_key status: $status_value" | |
end | |
else | |
echo "Unable to determine charge limit status." | |
end | |
else | |
echo "Not a Darwin system." | |
end | |
end |