From 1320628829368282dee777141e5390ef2a1288a0 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 24 Jun 2026 13:45:36 +0200 Subject: [PATCH] Fold battery helpers into status command --- bin/omarchy-battery-capacity | 10 ------ bin/omarchy-battery-remaining | 8 ----- bin/omarchy-battery-remaining-time | 22 ------------- bin/omarchy-battery-status | 19 ++++++++++-- test/shell.d/battery-status-test.sh | 48 +++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 42 deletions(-) delete mode 100755 bin/omarchy-battery-capacity delete mode 100755 bin/omarchy-battery-remaining delete mode 100755 bin/omarchy-battery-remaining-time create mode 100644 test/shell.d/battery-status-test.sh diff --git a/bin/omarchy-battery-capacity b/bin/omarchy-battery-capacity deleted file mode 100755 index 10e54794..00000000 --- a/bin/omarchy-battery-capacity +++ /dev/null @@ -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 -}' diff --git a/bin/omarchy-battery-remaining b/bin/omarchy-battery-remaining deleted file mode 100755 index d9b451f2..00000000 --- a/bin/omarchy-battery-remaining +++ /dev/null @@ -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 -}' diff --git a/bin/omarchy-battery-remaining-time b/bin/omarchy-battery-remaining-time deleted file mode 100755 index 7aa77b11..00000000 --- a/bin/omarchy-battery-remaining-time +++ /dev/null @@ -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 -}' diff --git a/bin/omarchy-battery-status b/bin/omarchy-battery-status index ac81e9d6..0521a57a 100755 --- a/bin/omarchy-battery-status +++ b/bin/omarchy-battery-status @@ -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") diff --git a/test/shell.d/battery-status-test.sh b/test/shell.d/battery-status-test.sh new file mode 100644 index 00000000..41b942cd --- /dev/null +++ b/test/shell.d/battery-status-test.sh @@ -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"