Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions bin/omarchy-battery-status
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,22 @@ battery_path="$power_supply_path/$native_path"

# UPower's energy-rate can lag the kernel telemetry by tens of seconds. Use
# the instantaneous sysfs reading when available so the open panel stays live.
if [[ -r $battery_path/power_now ]]; then
power_rate_raw=$(awk -v microwatts="$(<"$battery_path/power_now")" 'BEGIN { print microwatts / 1000000 }')
elif [[ -r $battery_path/current_now && -r $battery_path/voltage_now ]]; then
# Some ACPI batteries (HP Spectre x360) list power_now and current_now but fail
# every read with ENODEV, so go by what a read returns, not the permission bit,
# and keep UPower's rate when nothing comes back.
power_now=""
current_now=""
voltage_now=""
[[ ! -r $battery_path/power_now ]] || power_now=$(cat "$battery_path/power_now" 2>/dev/null)
[[ ! -r $battery_path/current_now ]] || current_now=$(cat "$battery_path/current_now" 2>/dev/null)
[[ ! -r $battery_path/voltage_now ]] || voltage_now=$(cat "$battery_path/voltage_now" 2>/dev/null)

if [[ -n $power_now ]]; then
power_rate_raw=$(awk -v microwatts="$power_now" 'BEGIN { print microwatts / 1000000 }')
elif [[ -n $current_now && -n $voltage_now ]]; then
power_rate_raw=$(awk \
-v microamps="$(<"$battery_path/current_now")" \
-v microvolts="$(<"$battery_path/voltage_now")" \
-v microamps="$current_now" \
-v microvolts="$voltage_now" \
'BEGIN { print microamps * microvolts / 1000000000000 }')
fi

Expand Down
12 changes: 12 additions & 0 deletions test/shell.d/battery-status-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,18 @@ danish_time=$(OMARCHY_LANGUAGE_FILE="$tmp_dir/language" OMARCHY_POWER_SUPPLY_PAT
grep -Fx $'time\t1 t 30 min' <<<"$danish_time" >/dev/null || fail "battery status durations follow the Omarchy language" "$danish_time"
pass "battery status reads in the Omarchy language"

# A battery that lists power_now and current_now but fails every read keeps
# UPower's rate. A directory stands in for the ENODEV attribute: it passes -r
# yet cannot be read as a file.
rm "$hold_dir/power/BAT0/power_now"
mkdir "$hold_dir/power/BAT0/power_now" "$hold_dir/power/BAT0/current_now"
printf '17556000\n' >"$hold_dir/power/BAT0/voltage_now"
sed -i 's/energy-rate: 0.1 W/energy-rate: 21.5336 W/' "$hold_dir/bin/upower"
unreadable_output=$(OMARCHY_POWER_SUPPLY_PATH="$hold_dir/power" PATH="$hold_dir/bin:$PATH" "$ROOT/bin/omarchy-battery-status" --shell 2>&1)
grep -Fx $'rate\t21.5W' <<<"$unreadable_output" >/dev/null || fail "an unreadable power_now falls back to UPower's rate" "$unreadable_output"
pass "battery status keeps UPower's rate when sysfs power cannot be read"
rmdir "$hold_dir/power/BAT0/power_now" "$hold_dir/power/BAT0/current_now"

# A full battery says so, with no time to count and no 0W draw.
cat >"$hold_dir/bin/upower" <<'STUB'
#!/bin/bash
Expand Down