Files
arthur-os/bin/omarchy-powerprofiles-set
T
62f6980852 Fix power profile auto-switching on USB-C only machines (#5444)
* Fix power profile auto-switching on USB-C only machines

* Small nits

* Stick with single unit name

---------

Co-authored-by: David Heinemeier Hansson <david@hey.com>
2026-05-02 11:47:02 +02:00

40 lines
1.2 KiB
Bash
Executable File

#!/bin/bash
# omarchy:summary=Set the power profile to the requested level, falling back to balanced
# omarchy:args=[autodetect|ac|battery]
action="${1-}"
# Auto-detect when called with no argument: treat any Mains or USB
# power-supply device reporting online=1 as "on AC". This handles
# USB-C only laptops where the legacy AC device may not fire udev
# events, and also avoids false negatives from per-port USB-C devices
# that are present-but-empty (online=0) while another port supplies power.
if [[ -z $action || $action == "autodetect" ]]; then
action=battery
for ps in /sys/class/power_supply/*; do
[[ -r $ps/online && -r $ps/type ]] || continue
type=$(cat "$ps/type")
[[ $type == "Mains" || $type == "USB" ]] || continue
if [[ $(cat "$ps/online") == "1" ]]; then
action=ac
break
fi
done
fi
mapfile -t profiles < <(powerprofilesctl list | awk '/^\s*[* ]\s*[a-zA-Z0-9\-]+:$/ { gsub(/^[*[:space:]]+|:$/,""); print }')
case "$action" in
ac)
# Prefer performance, fall back to balanced
if [[ " ${profiles[*]} " == *" performance "* ]]; then
powerprofilesctl set performance
else
powerprofilesctl set balanced
fi
;;
battery)
powerprofilesctl set balanced
;;
esac