Fold battery helpers into status command

This commit is contained in:
David Heinemeier Hansson
2026-06-24 13:45:36 +02:00
parent beea78084e
commit 1320628829
5 changed files with 65 additions and 42 deletions
-10
View File
@@ -1,10 +0,0 @@
#!/bin/bash
# omarchy:summary=Returns the battery full capacity in Wh (rounded to whole number).
battery_info=$(upower -i $(upower -e | grep BAT))
echo "$battery_info" | awk '/energy-full:/ {
printf "%d", $2
exit
}'
-8
View File
@@ -1,8 +0,0 @@
#!/bin/bash
# omarchy:summary=Returns the battery percentage remaining as an integer.
upower -i $(upower -e | grep BAT) | awk '/percentage/ {
print int($2)
exit
}'
-22
View File
@@ -1,22 +0,0 @@
#!/bin/bash
# omarchy:summary=Returns the battery time remaining (to empty or full) in a compact format.
battery_info=$(upower -i $(upower -e | grep BAT))
echo "$battery_info" | awk '/time to (empty|full)/ {
value = $4
unit = $5
if (unit ~ /^minute/) {
printf "%dm", int(value)
} else {
hours = int(value)
minutes = int((value - hours) * 60)
if (minutes > 0) {
printf "%dh %dm", hours, minutes
} else {
printf "%dh", hours
}
}
exit
}'
+17 -2
View File
@@ -23,6 +23,23 @@ battery=$(upower -e 2>/dev/null | grep BAT | head -n 1)
battery_info=$(upower -i "$battery")
percentage=$(awk '/percentage/ { print int($2); exit }' <<<"$battery_info")
capacity=$(awk '/energy-full:/ { printf "%d", $2; exit }' <<<"$battery_info")
time_remaining=$(awk '/time to (empty|full)/ {
value = $4
unit = $5
if (unit ~ /^minute/) {
printf "%dm", int(value)
} else {
hours = int(value)
minutes = int((value - hours) * 60)
if (minutes > 0) {
printf "%dh %dm", hours, minutes
} else {
printf "%dh", hours
}
}
exit
}' <<<"$battery_info")
power_rate_raw=$(awk '/energy-rate/ { print $2; exit }' <<<"$battery_info")
power_rate=$(awk '/energy-rate/ {
rounded = sprintf("%.1f", $2)
@@ -31,8 +48,6 @@ power_rate=$(awk '/energy-rate/ {
exit
}' <<<"$battery_info")
state=$(awk '/state/ { print $2; exit }' <<<"$battery_info")
time_remaining=$(omarchy-battery-remaining-time 2>/dev/null)
capacity=$(omarchy-battery-capacity 2>/dev/null)
threshold_start=$(awk '/charge-start-threshold:/ { gsub(/%/, "", $2); print int($2); exit }' <<<"$battery_info")
threshold_end=$(awk '/charge-end-threshold:/ { gsub(/%/, "", $2); print int($2); exit }' <<<"$battery_info")
+48
View File
@@ -0,0 +1,48 @@
#!/bin/bash
set -euo pipefail
source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh"
tmp_dir=$(mktemp -d)
trap 'rm -rf "$tmp_dir"' EXIT
mkdir -p "$tmp_dir/bin"
cat >"$tmp_dir/bin/upower" <<'STUB'
#!/bin/bash
if [[ $1 == "-e" ]]; then
echo "/org/freedesktop/UPower/devices/battery_BAT0"
exit 0
fi
if [[ $1 == "-i" ]]; then
cat <<'INFO'
native-path: BAT0
state: discharging
energy: 28.3 Wh
energy-full: 56.7 Wh
energy-rate: 7.3 W
time to empty: 2.5 hours
percentage: 51%
INFO
exit 0
fi
exit 1
STUB
chmod +x "$tmp_dir/bin/upower"
shell_output=$(PATH="$tmp_dir/bin:$PATH" "$ROOT/bin/omarchy-battery-status" --shell)
grep -Fx $'percentage\t51%' <<<"$shell_output" >/dev/null || fail "battery status reports percentage"
grep -Fx $'state\tdischarging' <<<"$shell_output" >/dev/null || fail "battery status reports state"
grep -Fx $'rate\t7.3W' <<<"$shell_output" >/dev/null || fail "battery status reports power rate"
grep -Fx $'size\t56Wh' <<<"$shell_output" >/dev/null || fail "battery status reports full capacity"
grep -Fx $'time\t2h 30m' <<<"$shell_output" >/dev/null || fail "battery status reports remaining time"
if matches=$(rg -n 'omarchy-battery-(capacity|remaining|remaining-time)' "$ROOT/bin" "$ROOT/test" "$ROOT/shell" "$ROOT/docs"); then
fail "battery status owns capacity and remaining calculations" "$matches"
fi
pass "battery status owns capacity and remaining calculations"