From a9c5806b784ab4c9e41d2dfa53ed512a7da842f5 Mon Sep 17 00:00:00 2001 From: Cynnamoroll Date: Mon, 30 Nov 2026 08:00:31 +0000 Subject: [PATCH 01/15] Hide internal-only groups from CLI help --- bin/omarchy | 2 -- test/cli | 23 ++++++++++++++--------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/bin/omarchy b/bin/omarchy index a01fa794f9a7..17823269dbe5 100755 --- a/bin/omarchy +++ b/bin/omarchy @@ -40,7 +40,6 @@ GROUP_DESCRIPTIONS[brightness]="Display and keyboard brightness" GROUP_DESCRIPTIONS[camera]="Webcam relay and image tuning" GROUP_DESCRIPTIONS[capture]="Screenshots and screen recording" GROUP_DESCRIPTIONS[channel]="Omarchy release channel management" -GROUP_DESCRIPTIONS[clipboard]="Clipboard helpers" GROUP_DESCRIPTIONS[cmd]="Command and shortcut helpers" GROUP_DESCRIPTIONS[config]="System configuration helpers" GROUP_DESCRIPTIONS[crash]="Crash notification controls" @@ -63,7 +62,6 @@ GROUP_DESCRIPTIONS[hw]="Hardware detection" GROUP_DESCRIPTIONS[hyprland]="Hyprland window, monitor, and toggle controls" GROUP_DESCRIPTIONS[icons]="App icon set management" GROUP_DESCRIPTIONS[install]="Optional software installers" -GROUP_DESCRIPTIONS[installed]="Installed optional service checks" GROUP_DESCRIPTIONS[launch]="Application launchers" GROUP_DESCRIPTIONS[kernel]="Default boot kernel selection" GROUP_DESCRIPTIONS[language]="Display language and system locale" diff --git a/test/cli b/test/cli index 9685cba9a1e0..66a5b966700e 100755 --- a/test/cli +++ b/test/cli @@ -234,16 +234,15 @@ if [[ $output == *"omarchy update perform"* ]]; then fi pass "hidden update perform compatibility wrapper is not shown" -# These three are install-time plumbing the ISO and install leaves call, so -# they stay out of the listing a user browses -- while still routing, because -# those callers use the CLI. A GROUP_DESCRIPTIONS entry would put the group -# back in the top-level listing even with every command in it hidden, which is -# why apply has none. +# Hidden-only groups stay out of the listing a user browses, while their +# commands still route for callers that use them directly. output=$(timeout 5 "$CLI") -if [[ $output == *"apply"* ]]; then - fail "a group whose commands are all hidden stays out of the top-level listing" -fi -pass "a group whose commands are all hidden stays out of the top-level listing" +for group in apply clipboard installed; do + if grep -Eq "^ $group[[:space:]]" <<<"$output"; then + fail "hidden-only group $group stays out of the top-level listing" + fi +done +pass "hidden-only groups stay out of the top-level listing" for internal in omarchy-apply-system omarchy-apply-hardware omarchy-apply-lock; do if jq -e --arg binary "$internal" '.commands[] | select(.binary == $binary)' "$commands_json_file" >/dev/null; then @@ -258,6 +257,12 @@ pass "install-time setup plumbing is hidden from the setup wizards" output=$(timeout 5 "$CLI" apply hardware --help) assert_output_contains "hidden setup plumbing still routes" "$output" "omarchy-apply-hardware" +output=$(timeout 5 "$CLI" clipboard paste text --help) +assert_output_contains "hidden clipboard command still routes" "$output" "omarchy-clipboard-paste-text" + +output=$(timeout 5 "$CLI" installed service dropbox --help) +assert_output_contains "hidden installed check still routes" "$output" "omarchy-installed-service-dropbox" + output=$("$CLI" screenshot --help) assert_output_contains "root alias resolves to command help" "$output" "omarchy-capture-screenshot" From eb2f7b1daba4bc82fb9ccb2244463fc98798cc4c Mon Sep 17 00:00:00 2001 From: Cynnamoroll Date: Mon, 30 Nov 2026 19:02:24 +0000 Subject: [PATCH 02/15] Keep CLI help coverage focused on the regression --- test/cli | 6 ------ 1 file changed, 6 deletions(-) diff --git a/test/cli b/test/cli index 66a5b966700e..eb7b17dd7ac1 100755 --- a/test/cli +++ b/test/cli @@ -257,12 +257,6 @@ pass "install-time setup plumbing is hidden from the setup wizards" output=$(timeout 5 "$CLI" apply hardware --help) assert_output_contains "hidden setup plumbing still routes" "$output" "omarchy-apply-hardware" -output=$(timeout 5 "$CLI" clipboard paste text --help) -assert_output_contains "hidden clipboard command still routes" "$output" "omarchy-clipboard-paste-text" - -output=$(timeout 5 "$CLI" installed service dropbox --help) -assert_output_contains "hidden installed check still routes" "$output" "omarchy-installed-service-dropbox" - output=$("$CLI" screenshot --help) assert_output_contains "root alias resolves to command help" "$output" "omarchy-capture-screenshot" From 65bed77e64a56db04a5c19b5a315b155fb07be21 Mon Sep 17 00:00:00 2001 From: theprimeagen Date: Tue, 1 Dec 2026 08:46:01 -0700 Subject: [PATCH 03/15] Fix bare dispatch for commands with optional alternatives command_requires_args stripped only [...] groups from the whole usage string, so a top-level | left bare pipes and the other alternatives behind and wrongly required an argument. Bare 'omarchy boot switch' therefore showed help instead of running the binary, whose bare action is valid. Split the usage on top-level | (ignoring pipes inside [], () and <>) and only require an argument when every alternative needs one after stripping optional groups. This also fixes the same pattern in other commands whose first alternative is all-optional. --- bin/omarchy | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/bin/omarchy b/bin/omarchy index 17823269dbe5..7cfb2c8c0f02 100755 --- a/bin/omarchy +++ b/bin/omarchy @@ -648,14 +648,45 @@ group_has_binary_commands() { command_requires_args() { local key="$1" local args="${COMMAND_ARGS[$key]}" - local required="$args" + local segment="" + local segments=() + local depth=0 + local i=0 + local char="" + + # A top-level | separates alternatives, and any one of them can satisfy the + # command: a bare invocation is only missing an argument when every + # alternative needs one. Pipes inside [], () or <> belong to one alternative. + for ((i = 0; i < ${#args}; i++)); do + char="${args:i:1}" + case "$char" in + '[' | '(' | '<') depth=$((depth + 1)) ;; + ']' | ')' | '>') + if ((depth > 0)); then depth=$((depth - 1)); fi + ;; + '|') + if ((depth == 0)); then + segments+=("$segment") + segment="" + continue + fi + ;; + esac + segment+="$char" + done + segments+=("$segment") - while [[ $required =~ ^(.*)\[[^][]*\](.*)$ ]]; do - required="${BASH_REMATCH[1]}${BASH_REMATCH[2]}" + local required="" + for segment in "${segments[@]}"; do + required="$segment" + while [[ $required =~ ^(.*)\[[^][]*\](.*)$ ]]; do + required="${BASH_REMATCH[1]}${BASH_REMATCH[2]}" + done + required="${required// /}" + [[ -z $required ]] && return 1 done - required="${required// /}" - [[ -n $required ]] + return 0 } # A command whose documented arguments are all flags can't take a word, so a From 56d903524ed30522e4baab57c4a2aca40e775b5c Mon Sep 17 00:00:00 2001 From: ThePrimeagen Date: Tue, 1 Dec 2026 09:09:48 -0700 Subject: [PATCH 04/15] Handle repeated optional groups and test alternative bare dispatch Strip trailing ... after [...] optional groups in command_requires_args so commands like omarchy-boot-rebuild ([--initramfs PKGBASE=FILE]... [PKGBASE...] | --pacman-install | --pacman-remove) also allow bare invocation, and add CLI router tests for optional and required alternatives. --- bin/omarchy | 6 +++--- test/cli | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/bin/omarchy b/bin/omarchy index 7cfb2c8c0f02..3d445bca0977 100755 --- a/bin/omarchy +++ b/bin/omarchy @@ -679,10 +679,10 @@ command_requires_args() { local required="" for segment in "${segments[@]}"; do required="$segment" - while [[ $required =~ ^(.*)\[[^][]*\](.*)$ ]]; do - required="${BASH_REMATCH[1]}${BASH_REMATCH[2]}" + while [[ $required =~ ^(.*)\[[^][]*\](\.\.\.)?(.*)$ ]]; do + required="${BASH_REMATCH[1]}${BASH_REMATCH[3]}" done - required="${required// /}" + required="${required//[[:space:]]/}" [[ -z $required ]] && return 1 done diff --git a/test/cli b/test/cli index eb7b17dd7ac1..e697ae0cfdd8 100755 --- a/test/cli +++ b/test/cli @@ -873,6 +873,53 @@ output=$("$TMPDIR/omarchy" subcmd 2>&1) || true [[ $output != *"subcmd-ran"* ]] || fail "subcommands make a bare invocation show help" pass "subcommands make a bare invocation show help" +# A top-level | separates alternatives; bare dispatch runs when any alternative +# consists solely of optional [...] (or [...]...) groups, while pipes inside +# () or <> stay part of their enclosing alternative. +{ + printf '#!/bin/bash\n\n' + printf '# omarchy:summary=Optional first alternative test\n' + printf '# omarchy:args=[--check] [--yes] | status | revert\n' + printf 'echo optalt-ran\n' +} >"$TMPDIR/omarchy-optalt" +{ + printf '#!/bin/bash\n\n' + printf '# omarchy:summary=Repeated optional alternative test\n' + printf '# omarchy:args=[--item VAL]... [NAME...] | --other\n' + printf 'echo optrepeat-ran\n' +} >"$TMPDIR/omarchy-optrepeat" +{ + printf '#!/bin/bash\n\n' + printf '# omarchy:summary=Required alternatives test\n' + printf '# omarchy:args=status | set \n' + printf 'echo reqalt-ran\n' +} >"$TMPDIR/omarchy-reqalt" +{ + printf '#!/bin/bash\n\n' + printf '# omarchy:summary=Required grouped alternatives test\n' + printf '# omarchy:args=[--file ] (--all | [fallback])\n' + printf 'echo reqgroup-ran\n' +} >"$TMPDIR/omarchy-reqgroup" +chmod +x "$TMPDIR/omarchy-optalt" "$TMPDIR/omarchy-optrepeat" "$TMPDIR/omarchy-reqalt" "$TMPDIR/omarchy-reqgroup" + +output=$("$TMPDIR/omarchy" optalt) +assert_output_contains "bare dispatch runs when an alternative is all optional" "$output" "optalt-ran" + +output=$("$TMPDIR/omarchy" optrepeat) +assert_output_contains "bare dispatch runs when an alternative uses repeated optional groups" "$output" "optrepeat-ran" + +status=0 +output=$("$TMPDIR/omarchy" reqalt 2>&1) || status=$? +(( status == 1 )) || fail "bare command where every alternative requires args exits 1" +[[ $output != *"reqalt-ran"* ]] || fail "bare command where every alternative requires args does not run" +assert_output_contains "bare command where every alternative requires args renders help" "$output" "Usage:" + +status=0 +output=$("$TMPDIR/omarchy" reqgroup 2>&1) || status=$? +(( status == 1 )) || fail "bare command with required parenthesized alternatives exits 1" +[[ $output != *"reqgroup-ran"* ]] || fail "bare command with required parenthesized alternatives does not run" +assert_output_contains "bare command with required parenthesized alternatives renders help" "$output" "Usage:" + # A trailing --help must render help even when the route only partially # resolves, leaving unresolved words ahead of the flag (e.g. `update aur --help` # used to start a real update because only the first leftover token was checked). From bf173beae1886cb83cd3deea1ec88d4299f695b2 Mon Sep 17 00:00:00 2001 From: Joshua Pruitt <50942+photuris@users.noreply.github.com> Date: Mon, 30 Nov 2026 18:32:51 -0600 Subject: [PATCH 05/15] add omarchy doctor, a read-only health check of the Omarchy install --- bin/omarchy | 1 + bin/omarchy-doctor | 374 ++++++++++++++++++++++++ test/shell.d/doctor-test.sh | 561 ++++++++++++++++++++++++++++++++++++ 3 files changed, 936 insertions(+) create mode 100755 bin/omarchy-doctor create mode 100644 test/shell.d/doctor-test.sh diff --git a/bin/omarchy b/bin/omarchy index 3d445bca0977..d59b61822246 100755 --- a/bin/omarchy +++ b/bin/omarchy @@ -52,6 +52,7 @@ GROUP_DESCRIPTIONS[dictation]="Dictation recording" GROUP_DESCRIPTIONS[display]="Display and text scaling" GROUP_DESCRIPTIONS[dns]="DNS resolver configuration" GROUP_DESCRIPTIONS[dock]="Dock shown on floating workspaces" +GROUP_DESCRIPTIONS[doctor]="Health check of this Omarchy install, with a next step for each failure" GROUP_DESCRIPTIONS[drive]="Drive selection and encryption" GROUP_DESCRIPTIONS[file]="File selection helpers" GROUP_DESCRIPTIONS[font]="Font management" diff --git a/bin/omarchy-doctor b/bin/omarchy-doctor new file mode 100755 index 000000000000..6d88062c262a --- /dev/null +++ b/bin/omarchy-doctor @@ -0,0 +1,374 @@ +#!/bin/bash + +# omarchy:summary=Check that this Omarchy install is healthy: runtime directory, shell, notifications, services, migrations, restarts, paths and theme +# omarchy:args=[--json] [--list] [check...] +# omarchy:examples=omarchy doctor | omarchy doctor shell migrations | omarchy doctor --json + +set -uo pipefail + +root=${OMARCHY_DOCTOR_ROOT:-} +runtime=${XDG_RUNTIME_DIR:-/run/user/$UID} + +CHECKS=(runtime-dir shell notifications user-services migrations restarts omarchy-path theme) + +declare -A SUMMARY=( + [runtime-dir]="The session's runtime directory has free space" + [shell]="The Omarchy shell answers, one instance per display" + [notifications]="A notification server answers" + [user-services]="No user service has failed" + [migrations]="No Omarchy migration is pending" + [restarts]="No reboot or service restart is pending" + [omarchy-path]="OMARCHY_PATH matches /etc/omarchy.conf and holds Omarchy" + [theme]="The current theme and background are in place" +) + +PASS=0 FAIL=1 SKIP=77 +RUNTIME_DIR_FULL_PERCENT=90 + +usage() { + echo "Usage: omarchy-doctor [--json] [--list] [check...]" + echo "Checks: ${CHECKS[*]}" +} + +is_check() { + local check + for check in "${CHECKS[@]}"; do + [[ $1 == "$check" ]] && return 0 + done + return 1 +} + +has_session() { + local socket + shopt -s nullglob + local -a sockets=("$runtime"/wayland-[0-9]*) + shopt -u nullglob + for socket in "${sockets[@]}"; do + [[ $socket == *.lock ]] && continue + [[ -e $socket ]] && return 0 + done + return 1 +} + +check_runtime_dir() { + local percent link pid target dir id stale=0 detail + local -A live_ids=() + + if [[ ! -d $runtime ]]; then + echo "missing: $runtime" + echo "Log out and back in" + return $FAIL + fi + + percent=$(LC_ALL=C timeout --kill-after=1s 2s df --output=pcent -- "$runtime" 2>/dev/null | tail -n 1 | tr -dc '0-9') + if [[ -z $percent ]]; then + echo "usage could not be read" + return $SKIP + fi + + if [[ -d $runtime/quickshell/by-id ]]; then + shopt -s nullglob + if [[ -d $runtime/quickshell/by-pid ]]; then + for link in "$runtime/quickshell/by-pid"/*; do + [[ -L $link ]] || continue + pid=${link##*/} + [[ $pid =~ ^[0-9]+$ ]] || continue + [[ -d $root/proc/$pid ]] || continue + target=$(readlink "$link" 2>/dev/null) || continue + target=${target%/} + target=${target##*/} + [[ -n $target ]] && live_ids[$target]=1 + done + fi + for dir in "$runtime/quickshell/by-id"/*; do + [[ -d $dir ]] || continue + id=${dir##*/} + [[ -n ${live_ids[$id]:-} ]] || (( stale += 1 )) + done + shopt -u nullglob + fi + + detail="${percent}% used" + (( stale > 0 )) && detail+=", stale shell logs: $stale" + + if (( 10#$percent >= RUNTIME_DIR_FULL_PERCENT )); then + echo "$detail" + if (( stale > 0 )); then + echo "Delete the stale directories in $runtime/quickshell/by-id, or log out and back in" + else + echo "Find what fills it: du -xh $runtime | sort -h | tail" + fi + return $FAIL + else + echo "$detail" + return $PASS + fi +} + +check_shell() { + local max_instances + + if ! has_session; then + echo "no desktop session" + return $SKIP + fi + + if ! timeout --kill-after=1s 3s omarchy-shell shell ping >/dev/null 2>&1; then + echo "not answering" + echo "Run: omarchy restart shell" + return $FAIL + fi + + max_instances=$( + LC_ALL=C timeout --kill-after=1s 2s quickshell list -p "${OMARCHY_PATH:-}/shell" --any-display 2>/dev/null | + awk ' + /^ Display connection: / { + display = substr($0, 23) + count[display]++ + if (count[display] > max) max = count[display] + } + END { print max + 0 } + ' + ) + + if (( max_instances > 1 )); then + echo "$max_instances instances on one display" + echo "Run: omarchy restart shell" + return $FAIL + else + echo "answering" + return $PASS + fi +} + +check_notifications() { + local reply name + + if ! has_session; then + echo "no desktop session" + return $SKIP + fi + + if ! reply=$(timeout --kill-after=1s 2s busctl --user --auto-start=no -j call org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications GetServerInformation 2>/dev/null); then + echo "no notification server answers" + echo "Run: omarchy restart shell" + return $FAIL + fi + + name=$(jq -r '.data[0] | strings' 2>/dev/null <<<"$reply") || name="" + if [[ -z $name ]]; then + echo "no notification server answers" + echo "Run: omarchy restart shell" + return $FAIL + else + echo "served by $name" + return $PASS + fi +} + +check_user_services() { + local units + local -a failed=() + + if ! units=$(timeout --kill-after=1s 5s systemctl --user list-units --state=failed --no-legend --plain 2>/dev/null); then + echo "systemd did not answer" + return $SKIP + fi + + mapfile -t failed < <(awk 'NF {print $1}' <<<"$units" | LC_ALL=C sort -u) + if (( ${#failed[@]} == 0 )); then + echo "none failed" + return $PASS + else + echo "failed: ${failed[*]}" + echo "Inspect: systemctl --user status ${failed[0]}" + return $FAIL + fi +} + +check_migrations() { + local pending n + + pending=$(timeout --kill-after=1s 5s omarchy-migrate --pending 2>/dev/null) || true + n=$(awk 'NF { count++ } END { print count + 0 }' <<<"$pending") + + if (( n == 0 )); then + echo "none pending" + return $PASS + else + echo "$n pending" + echo "Run: omarchy migrate" + return $FAIL + fi +} + +check_restarts() { + local state=$HOME/.local/state/omarchy reboot=false file name + local -a services=() + + [[ -f $state/reboot-required ]] && reboot=true + + shopt -s nullglob + for file in "$state"/restart-*-required; do + [[ -f $file ]] || continue + name=${file##*/} + name=${name#restart-} + name=${name%-required} + [[ -n $name ]] && services+=("$name") + done + shopt -u nullglob + + if [[ $reboot == "true" ]] && (( ${#services[@]} > 0 )); then + echo "reboot required; restart required: ${services[*]}" + echo "Reboot: omarchy system reboot" + return $FAIL + elif [[ $reboot == "true" ]]; then + echo "reboot required" + echo "Reboot: omarchy system reboot" + return $FAIL + elif (( ${#services[@]} > 0 )); then + echo "restart required: ${services[*]}" + echo "Run: omarchy update restart --services-only" + return $FAIL + else + echo "none pending" + return $PASS + fi +} + +check_omarchy_path() { + local path=${OMARCHY_PATH:-} conf=$root/etc/omarchy.conf configured + + # shellcheck source=/dev/null + configured=$(unset OMARCHY_PATH; [[ -r $conf ]] && . "$conf" 2>/dev/null; printf '%s' "${OMARCHY_PATH:-/usr/share/omarchy}") + + if [[ -z $path ]]; then + echo "OMARCHY_PATH is not set" + echo "Log out and back in" + return $FAIL + elif [[ ! -f $path/bin/omarchy || ! -x $path/bin/omarchy ]]; then + echo "no Omarchy at $path" + echo "Fix OMARCHY_PATH in /etc/omarchy.conf, then reboot" + return $FAIL + elif [[ ! $path -ef $configured ]]; then + echo "session uses $path, /etc/omarchy.conf says $configured" + echo "Reboot to use $configured" + return $FAIL + elif [[ $configured != "/usr/share/omarchy" ]]; then + echo "$path (dev link)" + return $PASS + else + echo "$path" + return $PASS + fi +} + +check_theme() { + local current=$HOME/.local/state/omarchy/current name="" + + if [[ ! -d $current/theme || -z $(find -H "$current/theme" -mindepth 1 -maxdepth 1 -print -quit 2>/dev/null) ]]; then + echo "no current theme" + echo "Run: omarchy theme set (names: omarchy theme list)" + return $FAIL + elif [[ ! -e $current/background ]]; then + echo "background is missing" + echo "Run: omarchy theme bg next" + return $FAIL + else + if [[ -f $current/theme.name && -r $current/theme.name ]]; then + IFS= read -r name <"$current/theme.name" || true + fi + if [[ -n $name ]]; then + echo "$name" + else + echo "set" + fi + return $PASS + fi +} + +render_text() { + jq -r ' + def mark: {pass: "✓", fail: "✗", skip: "–"}[.]; + "Omarchy doctor", + "", + (.checks[] | + " \(.result | mark) \(.id | . + " " * (14 - length))\(if .result == "skip" then "skipped: " else "" end)\(.detail)", + (if .result == "fail" and .hint != "" then " \(.hint)" else empty end)), + "", + ([.checks[] | .result] as $r | "\($r | map(select(. == "pass")) | length) passed, \($r | map(select(. == "fail")) | length) failed, \($r | map(select(. == "skip")) | length) skipped") + ' +} + +run_checks() { + local check output status result detail hint + for check in "${CHECKS[@]}"; do + [[ " ${selected[*]} " == *" $check "* ]] || continue + output=$("check_${check//-/_}") + status=$? + case $status in + "$PASS") result=pass ;; + "$SKIP") result=skip ;; + *) result=fail ;; + esac + detail="" + hint="" + { + IFS= read -r detail || true + IFS= read -r hint || true + } <<<"$output" + [[ $result == "fail" ]] || hint="" + if [[ -n ${HOME:-} && ${HOME:-} != "/" ]]; then + detail=${detail//"$HOME/"/"~/"} + hint=${hint//"$HOME/"/"~/"} + fi + jq -nc --arg id "$check" --arg result "$result" --arg detail "$detail" --arg hint "$hint" \ + '{id: $id, result: $result, detail: $detail, hint: $hint}' + done +} + +json=false +list=false +selected=() + +while (( $# > 0 )); do + case $1 in + --json) json=true ;; + --list) list=true ;; + -h | --help) + usage + exit 0 + ;; + *) + if is_check "$1"; then + selected+=("$1") + else + usage >&2 + exit 2 + fi + ;; + esac + shift +done + +if [[ $list == "true" ]]; then + for check in "${CHECKS[@]}"; do + printf '%-14s %s\n' "$check" "${SUMMARY[$check]}" + done +else + (( ${#selected[@]} )) || selected=("${CHECKS[@]}") + + omarchy_version=$(timeout --kill-after=1s 2s omarchy-version 2>/dev/null) || true + report=$(jq -nc \ + --arg omarchy "$omarchy_version" \ + --argjson checks "$(run_checks | jq -sc .)" \ + '{version: 1, omarchy: $omarchy, checks: $checks}') + + if [[ $json == "true" ]]; then + printf '%s\n' "$report" + else + render_text <<<"$report" + fi + + jq -e 'any(.checks[]; .result == "fail") | not' >/dev/null <<<"$report" +fi diff --git a/test/shell.d/doctor-test.sh b/test/shell.d/doctor-test.sh new file mode 100644 index 000000000000..6f36184eda8a --- /dev/null +++ b/test/shell.d/doctor-test.sh @@ -0,0 +1,561 @@ +#!/bin/bash + +set -euo pipefail + +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" + +unset DF_PERCENT SHELL_PING QS_LIST BUSCTL_REPLY BUSCTL_STATUS SYSTEMCTL_STATUS FAILED_UNITS + +test_tmp=$(mktemp -d) +trap 'rm -rf "$test_tmp"' EXIT + +stub_bin="$test_tmp/bin" +mkdir -p "$stub_bin" + +cat >"$stub_bin/df" <>"$test_tmp/probed" +if [[ \${LC_ALL:-} != "C" ]]; then + printf 'Use%%\nvoll\n' +elif [[ \${DF_PERCENT:-12} != "none" ]]; then + printf 'Use%%\n%s%%\n' "\${DF_PERCENT:-12}" +fi +SH + +cat >"$stub_bin/omarchy-shell" <<'SH' +#!/bin/bash +exit "${SHELL_PING:-0}" +SH + +cat >"$stub_bin/quickshell" <<'SH' +#!/bin/bash +printf '%b' "${QS_LIST:-}" +SH + +cat >"$stub_bin/busctl" <<'SH' +#!/bin/bash +has_auto_start=false +has_user=false +for arg in "$@"; do + [[ $arg == "--auto-start=no" ]] && has_auto_start=true + [[ $arg == "--user" ]] && has_user=true +done +if [[ $has_auto_start != "true" || $has_user != "true" ]]; then + exit 1 +fi +if [[ -v BUSCTL_REPLY ]]; then + printf '%s\n' "$BUSCTL_REPLY" +else + printf '%s\n' '{"type":"ssss","data":["quickshell","quickshell","","1.2"]}' +fi +exit "${BUSCTL_STATUS:-0}" +SH + +cat >"$stub_bin/systemctl" <<'SH' +#!/bin/bash +has_user=false +for arg in "$@"; do + [[ $arg == "--user" ]] && has_user=true +done +if [[ $has_user == "true" ]]; then + printf '%b' "${FAILED_UNITS:-}" +else + printf 'system-only.service loaded failed failed\n' +fi +exit "${SYSTEMCTL_STATUS:-0}" +SH + +cat >"$stub_bin/omarchy-version" <<'SH' +#!/bin/bash +echo stub +SH + +for name in sudo pkexec; do + cat >"$stub_bin/$name" <>"$test_tmp/escalated" +exit 1 +SH +done + +chmod +x "$stub_bin"/* + +expect() { + local description=$1 expected=$2 actual=$3 + [[ $actual == "$expected" ]] || fail "$description" "expected: $expected"$'\n'"actual: $actual" + pass "$description" +} + +# Verify escalation stubs record their names before any doctor test runs. +"$stub_bin/sudo" || true +"$stub_bin/pkexec" || true +expect "sudo and pkexec stubs record their names in the marker file" $'sudo\npkexec' "$(<"$test_tmp/escalated")" +rm -f "$test_tmp/escalated" + +new_tree() { + fake=$(mktemp -d "$test_tmp/tree.XXXXXX") + mkdir -p "$fake/home/.local/state/omarchy/current/theme" "$fake/run" \ + "$fake/omarchy/bin" "$fake/omarchy/migrations" "$fake/etc" "$fake/proc" + touch "$fake/run/wayland-0.lock" "$fake/run/wayland-1" "$fake/run/wayland-1.lock" + printf '#!/bin/bash\n' >"$fake/omarchy/bin/omarchy" + chmod +x "$fake/omarchy/bin/omarchy" + printf 'export OMARCHY_PATH="%s"\n' "$fake/omarchy" >"$fake/etc/omarchy.conf" + touch "$fake/home/.local/state/omarchy/current/theme/colors.toml" + printf 'ristretto\n' >"$fake/home/.local/state/omarchy/current/theme.name" + touch "$fake/home/.local/state/omarchy/current/wallpaper.png" + ln -s "$fake/home/.local/state/omarchy/current/wallpaper.png" "$fake/home/.local/state/omarchy/current/background" +} + +run_doctor() { + local -a unset_env=(-u OMARCHY_MIGRATION_STATE) extra_env=() base_env=() + local set_omarchy_path=true + while (( $# > 0 )); do + case $1 in + --env) + extra_env+=("$2") + shift 2 + ;; + --unset) + unset_env+=(-u "$2") + [[ $2 == "OMARCHY_PATH" ]] && set_omarchy_path=false + shift 2 + ;; + --) + shift + break + ;; + *) + break + ;; + esac + done + base_env=( + "HOME=$fake/home" + "XDG_RUNTIME_DIR=$fake/run" + "OMARCHY_DOCTOR_ROOT=$fake" + "PATH=$stub_bin:$ROOT/bin:$PATH" + "LC_ALL=de_DE.UTF-8" + ) + [[ $set_omarchy_path == "true" ]] && base_env+=("OMARCHY_PATH=$fake/omarchy") + env "${unset_env[@]}" "${base_env[@]}" "${extra_env[@]}" "$ROOT/bin/omarchy-doctor" "$@" +} + +doctor() { + local -a extra_env=() + while (( $# > 0 )); do + case $1 in + --env | --unset) + extra_env+=("$1" "$2") + shift 2 + ;; + *) + break + ;; + esac + done + run_doctor "${extra_env[@]}" -- --json "$@" 2>/dev/null +} + +outcome() { + local report + report=$(doctor "$@") || true + jq -r '.checks[0] | "\(.result): \(.detail)"' <<<"$report" +} + +hint_of() { + local report + report=$(doctor "$@") || true + jq -r '.checks[0].hint' <<<"$report" +} + +setup_stale_logs() { + mkdir -p "$fake/run/quickshell/by-id/a" "$fake/run/quickshell/by-id/b" "$fake/run/quickshell/by-id/c" \ + "$fake/run/quickshell/by-pid" "$fake/proc/100" "$fake/proc/300" + touch "$fake/run/quickshell/by-id/note" + ln -s "$fake/run/quickshell/by-id/a" "$fake/run/quickshell/by-pid/100" + ln -s "$fake/run/quickshell/by-id/b" "$fake/run/quickshell/by-pid/200" + ln -s "$fake/run/quickshell/by-id/a" "$fake/run/quickshell/by-pid/300" +} + +# runtime-dir +new_tree +expect "1: healthy runtime-dir passes" "pass: 12% used" "$(outcome runtime-dir)" +expect "2: runtime-dir at 89% passes" "pass: 89% used" "$(DF_PERCENT=89 outcome runtime-dir)" +expect "3: runtime-dir at 90% fails" "fail: 90% used" "$(DF_PERCENT=90 outcome runtime-dir)" +expect "3: runtime-dir at 90% without stale logs suggests du" \ + "Find what fills it: du -xh $fake/run | sort -h | tail" "$(DF_PERCENT=90 hint_of runtime-dir)" + +new_tree +mkdir -p "$fake/home/run" +expect "3a: full runtime-dir under HOME fails" "fail: 90% used" \ + "$(DF_PERCENT=90 outcome --env "XDG_RUNTIME_DIR=$fake/home/run" runtime-dir)" +expect "3a: full runtime-dir under HOME shortens HOME prefix in hint" \ + "Find what fills it: du -xh ~/run | sort -h | tail" \ + "$(DF_PERCENT=90 hint_of --env "XDG_RUNTIME_DIR=$fake/home/run" runtime-dir)" + +new_tree +setup_stale_logs +expect "4: runtime-dir counts stale shell log directories" "pass: 12% used, stale shell logs: 2" "$(outcome runtime-dir)" +expect "5: full runtime-dir with stale logs fails with stale count" \ + "fail: 95% used, stale shell logs: 2" "$(DF_PERCENT=95 outcome runtime-dir)" +expect "5: full runtime-dir with stale logs suggests deleting stale dirs" \ + "Delete the stale directories in $fake/run/quickshell/by-id, or log out and back in" "$(DF_PERCENT=95 hint_of runtime-dir)" + +new_tree +expect "6: unreadable df output skips runtime-dir" "skip: usage could not be read" "$(DF_PERCENT=none outcome runtime-dir)" +expect "7: missing runtime-dir fails" "fail: missing: $fake/absent" "$(outcome --env "XDG_RUNTIME_DIR=$fake/absent" runtime-dir)" +expect "7: missing runtime-dir hint tells user to log out and back in" \ + "Log out and back in" "$(hint_of --env "XDG_RUNTIME_DIR=$fake/absent" runtime-dir)" + +new_tree +mkdir -p "$fake/run/quickshell/by-id/a" "$fake/run/quickshell/by-pid" "$fake/proc/100" +ln -s "/nonexistent/elsewhere/a" "$fake/run/quickshell/by-pid/100" +expect "7a: live pid symlink with nonexistent target still counts by-id dir as live" \ + "pass: 12% used" "$(outcome runtime-dir)" + +new_tree +mkdir -p "$fake/run/quickshell/by-id/a" "$fake/run/quickshell/by-pid" "$fake/proc/self" +ln -s "$fake/run/quickshell/by-id/a" "$fake/run/quickshell/by-pid/self" +expect "7b: non-numeric by-pid name is never live" \ + "pass: 12% used, stale shell logs: 1" "$(outcome runtime-dir)" + +new_tree +mkdir -p "$fake/run/quickshell/by-id/a" +expect "7c: missing by-pid marks by-id dirs as stale" \ + "pass: 12% used, stale shell logs: 1" "$(outcome runtime-dir)" + +new_tree +mkdir -p "$fake/run/quickshell/by-id/a" "$fake/run/quickshell/by-id/b" \ + "$fake/run/quickshell/by-pid" "$fake/proc/100" "$fake/proc/200" +ln -s "$fake/run/quickshell/by-id/a" "$fake/run/quickshell/by-pid/100" +ln -s "$fake/run/quickshell/by-id/b" "$fake/run/quickshell/by-pid/200" +expect "7d: two distinct live pids keep both by-id dirs live" \ + "pass: 12% used" "$(outcome runtime-dir)" + +# shell +new_tree +rm "$fake/run/wayland-1" +expect "8: shell skips when no wayland socket exists" "skip: no desktop session" "$(outcome shell)" + +new_tree +expect "9: shell fails when ping exits non-zero" "fail: not answering" "$(SHELL_PING=1 outcome shell)" +expect "9: shell failure hint suggests restarting shell" "Run: omarchy restart shell" "$(SHELL_PING=1 hint_of shell)" + +expect "10: two shell instances on one display fail" "fail: 2 instances on one display" \ + "$(QS_LIST='Instance 1\n Display connection: wayland/wayland-1\nInstance 2\n Display connection: wayland/wayland-1\n' outcome shell)" +expect "10: duplicate shell instances hint suggests restarting shell" "Run: omarchy restart shell" \ + "$(QS_LIST='Instance 1\n Display connection: wayland/wayland-1\nInstance 2\n Display connection: wayland/wayland-1\n' hint_of shell)" + +expect "11: two shell instances on different displays pass" "pass: answering" \ + "$(QS_LIST='Instance 1\n Display connection: wayland/wayland-1\nInstance 2\n Display connection: wayland/wayland-2\n' outcome shell)" + +expect "11a: non-adjacent duplicate display instances fail" "fail: 2 instances on one display" \ + "$(QS_LIST='Instance 1\n Display connection: wayland/wayland-2\nInstance 2\n Display connection: wayland/wayland-1\nInstance 3\n Display connection: wayland/wayland-2\n' outcome shell)" + +expect "12: empty quickshell list passes when ping answers" "pass: answering" "$(outcome shell)" + +new_tree +rm "$fake/run/wayland-1" +python3 -c 'import socket,sys; socket.socket(socket.AF_UNIX).bind(sys.argv[1])' "$fake/run/wayland-1" +expect "12a: shell passes with a real Unix wayland socket" "pass: answering" "$(outcome shell)" + +# notifications +new_tree +rm "$fake/run/wayland-1" +expect "13: notifications skips when no wayland socket exists" "skip: no desktop session" "$(outcome notifications)" + +new_tree +expect "14: notifications fails when busctl exits non-zero" "fail: no notification server answers" "$(BUSCTL_STATUS=1 outcome notifications)" +expect "14: notifications failure hint suggests restarting shell" "Run: omarchy restart shell" "$(BUSCTL_STATUS=1 hint_of notifications)" +expect "15: notifications fails on empty data array" "fail: no notification server answers" "$(BUSCTL_REPLY='{"type":"ssss","data":[]}' outcome notifications)" +expect "15a: notifications fails on trailing garbage after JSON" "fail: no notification server answers" \ + "$(BUSCTL_REPLY='{"type":"ssss","data":["quickshell"]} garbage' outcome notifications)" +expect "15b: notifications fails on non-string data[0]" "fail: no notification server answers" \ + "$(BUSCTL_REPLY='{"type":"ssss","data":[7]}' outcome notifications)" +expect "16: notifications passes with --user and --auto-start=no" "pass: served by quickshell" "$(outcome notifications)" + +# user-services +new_tree +expect "17: healthy user-services passes and passes --user" "pass: none failed" "$(outcome user-services)" + +available_locales=$(locale -a 2>/dev/null || true) +sort_locale=de_DE.UTF-8 +if grep -qxF 'en_US.utf8' <<<"$available_locales"; then + sort_locale=en_US.utf8 +elif grep -qxF 'de_DE.utf8' <<<"$available_locales"; then + sort_locale=de_DE.utf8 +fi + +failed_units=$'alpha.service loaded failed failed A\n\nZeta.service loaded failed failed Z\nalpha.service loaded failed failed A\n' +expect "18: user-services deduplicates and sorts failed units in C locale" \ + "fail: failed: Zeta.service alpha.service" \ + "$(FAILED_UNITS=$failed_units outcome --env "LC_ALL=$sort_locale" user-services)" +expect "18: user-services hint inspects first sorted unit" \ + "Inspect: systemctl --user status Zeta.service" \ + "$(FAILED_UNITS=$failed_units hint_of --env "LC_ALL=$sort_locale" user-services)" +expect "19: user-services skips when systemctl exits non-zero" "skip: systemd did not answer" "$(SYSTEMCTL_STATUS=1 outcome user-services)" + +# migrations +new_tree +expect "20: migrations passes when none are pending" "pass: none pending" "$(outcome migrations)" + +touch "$fake/omarchy/migrations/1.sh" "$fake/omarchy/migrations/2.sh" +mkdir -p "$fake/home/.local/state/omarchy/migrations" +touch "$fake/home/.local/state/omarchy/migrations/1.sh" +expect "21: migrations fails with 1 pending when one marker exists" "fail: 1 pending" "$(outcome migrations)" +expect "21: migrations hint suggests omarchy migrate" "Run: omarchy migrate" "$(hint_of migrations)" + +rm "$fake/home/.local/state/omarchy/migrations/1.sh" +expect "22: migrations fails with 2 pending when no markers exist" "fail: 2 pending" "$(outcome migrations)" + +# restarts +new_tree +expect "23: restarts passes when no marker files exist" "pass: none pending" "$(outcome restarts)" + +touch "$fake/home/.local/state/omarchy/reboot-required" +expect "24: restarts fails when reboot-required exists" "fail: reboot required" "$(outcome restarts)" +expect "24: reboot-required hint suggests reboot" "Reboot: omarchy system reboot" "$(hint_of restarts)" + +new_tree +touch "$fake/home/.local/state/omarchy/restart-bluetooth-required" \ + "$fake/home/.local/state/omarchy/restart-walker-required" \ + "$fake/home/.local/state/omarchy/restart--required" +mkdir -p "$fake/home/.local/state/omarchy/restart-dir-required" +expect "25: restarts lists regular service markers and ignores empty name and directories" \ + "fail: restart required: bluetooth walker" "$(outcome restarts)" +expect "25: service restarts hint suggests services-only restart" \ + "Run: omarchy update restart --services-only" "$(hint_of restarts)" + +new_tree +touch "$fake/home/.local/state/omarchy/restart--required" +expect "25a: empty service restart name alone passes" "pass: none pending" "$(outcome restarts)" + +new_tree +touch "$fake/home/.local/state/omarchy/reboot-required" \ + "$fake/home/.local/state/omarchy/restart-bluetooth-required" \ + "$fake/home/.local/state/omarchy/restart-walker-required" \ + "$fake/home/.local/state/omarchy/restart--required" +mkdir -p "$fake/home/.local/state/omarchy/restart-dir-required" +expect "26: restarts reports both reboot and services together" \ + "fail: reboot required; restart required: bluetooth walker" "$(outcome restarts)" +expect "26: combined reboot and service restarts hint suggests reboot" \ + "Reboot: omarchy system reboot" "$(hint_of restarts)" + +# omarchy-path +new_tree +expect "27: healthy dev-linked omarchy-path passes" "pass: $fake/omarchy (dev link)" "$(outcome omarchy-path)" +expect "28: unset OMARCHY_PATH fails" "fail: OMARCHY_PATH is not set" "$(outcome --unset OMARCHY_PATH omarchy-path)" +expect "28: unset OMARCHY_PATH hint tells user to log out and back in" "Log out and back in" "$(hint_of --unset OMARCHY_PATH omarchy-path)" +expect "28a: empty OMARCHY_PATH fails" "fail: OMARCHY_PATH is not set" "$(outcome --env "OMARCHY_PATH=" omarchy-path)" + +rm "$fake/omarchy/bin/omarchy" +expect "29: missing bin/omarchy fails" "fail: no Omarchy at $fake/omarchy" "$(outcome omarchy-path)" +expect "29: missing bin/omarchy hint tells user to fix omarchy.conf" \ + "Fix OMARCHY_PATH in /etc/omarchy.conf, then reboot" "$(hint_of omarchy-path)" + +printf '#!/bin/bash\n' >"$fake/omarchy/bin/omarchy" +chmod -x "$fake/omarchy/bin/omarchy" +expect "29a: non-executable bin/omarchy fails" "fail: no Omarchy at $fake/omarchy" "$(outcome omarchy-path)" + +rm "$fake/omarchy/bin/omarchy" +mkdir -p "$fake/omarchy/bin/omarchy" +expect "29b: directory bin/omarchy fails" "fail: no Omarchy at $fake/omarchy" "$(outcome omarchy-path)" + +new_tree +rm "$fake/etc/omarchy.conf" +expect "30: missing omarchy.conf defaults to /usr/share/omarchy and fails on mismatch" \ + "fail: session uses $fake/omarchy, /etc/omarchy.conf says /usr/share/omarchy" "$(outcome omarchy-path)" +expect "30: mismatch hint suggests rebooting to configured path" \ + "Reboot to use /usr/share/omarchy" "$(hint_of omarchy-path)" + +new_tree +ln -s "$fake/omarchy" "$fake/link" +printf 'export OMARCHY_PATH="%s"\n' "$fake/link" >"$fake/etc/omarchy.conf" +expect "31: omarchy.conf symlink pointing to OMARCHY_PATH matches" \ + "pass: $fake/omarchy (dev link)" "$(outcome omarchy-path)" + +new_tree +mkdir -p "$fake/home/omarchy/bin" +cp "$fake/omarchy/bin/omarchy" "$fake/home/omarchy/bin/omarchy" +printf 'export OMARCHY_PATH="%s"\n' "$fake/home/omarchy" >"$fake/etc/omarchy.conf" +expect "32: OMARCHY_PATH under HOME shortens to ~/" \ + "pass: ~/omarchy (dev link)" "$(outcome --env "OMARCHY_PATH=$fake/home/omarchy" omarchy-path)" + +new_tree +mkdir -p "$fake/home2/omarchy/bin" +cp "$fake/omarchy/bin/omarchy" "$fake/home2/omarchy/bin/omarchy" +printf 'export OMARCHY_PATH="%s"\n' "$fake/home2/omarchy" >"$fake/etc/omarchy.conf" +expect "32a: sibling path starting with HOME prefix is not shortened" \ + "pass: $fake/home2/omarchy (dev link)" "$(outcome --env "OMARCHY_PATH=$fake/home2/omarchy" omarchy-path)" + +if [[ -f /usr/share/omarchy/bin/omarchy && -x /usr/share/omarchy/bin/omarchy ]]; then + new_tree + rm "$fake/etc/omarchy.conf" + expect "32b: default /usr/share/omarchy passes without dev link suffix" \ + "pass: /usr/share/omarchy" "$(outcome --env "OMARCHY_PATH=/usr/share/omarchy" omarchy-path)" +else + skip "32b: /usr/share/omarchy/bin/omarchy not installed on this machine" +fi + +# theme +new_tree +expect "33: healthy theme passes with theme.name" "pass: ristretto" "$(outcome theme)" + +rm "$fake/home/.local/state/omarchy/current/theme.name" +expect "34: missing theme.name passes as set" "pass: set" "$(outcome theme)" + +printf 'ristretto\nsecond\n' >"$fake/home/.local/state/omarchy/current/theme.name" +expect "34a: multi-line theme.name reads first line only" "pass: ristretto" "$(outcome theme)" + +printf '\nsecond\n' >"$fake/home/.local/state/omarchy/current/theme.name" +expect "34b: empty first line in theme.name passes as set" "pass: set" "$(outcome theme)" + +new_tree +rm "$fake/home/.local/state/omarchy/current/theme/colors.toml" +touch "$fake/home/.local/state/omarchy/current/theme/.keep" +expect "34c: dotfile-only theme directory counts as non-empty" "pass: ristretto" "$(outcome theme)" + +new_tree +mkdir -p "$fake/theme-target" +touch "$fake/theme-target/colors.toml" +rm -rf "$fake/home/.local/state/omarchy/current/theme" +ln -s "$fake/theme-target" "$fake/home/.local/state/omarchy/current/theme" +expect "34d: symlinked theme directory is read through symlink" "pass: ristretto" "$(outcome theme)" + +new_tree +rm "$fake/home/.local/state/omarchy/current/theme/colors.toml" +expect "35: empty theme directory fails" "fail: no current theme" "$(outcome theme)" +expect "35: empty theme directory hint includes literal " \ + "Run: omarchy theme set (names: omarchy theme list)" "$(hint_of theme)" + +rmdir "$fake/home/.local/state/omarchy/current/theme" +expect "36: missing theme directory fails" "fail: no current theme" "$(outcome theme)" + +new_tree +rm "$fake/home/.local/state/omarchy/current/wallpaper.png" +expect "37: broken background symlink fails" "fail: background is missing" "$(outcome theme)" +expect "37: missing background hint suggests next bg" "Run: omarchy theme bg next" "$(hint_of theme)" + +# whole command +new_tree +status=0 +run_doctor >/dev/null 2>&1 || status=$? +expect "38: healthy full run exits 0" "0" "$status" + +touch "$fake/home/.local/state/omarchy/reboot-required" +status=0 +run_doctor >/dev/null 2>&1 || status=$? +expect "39: run with a failure exits 1" "1" "$status" + +new_tree +rm -f "$test_tmp/probed" +usage_line="Usage: omarchy-doctor [--json] [--list] [check...]" + +for bad_arg in "nonsense" "" " "; do + status=0 + run_doctor -- "$bad_arg" >/dev/null 2>&1 || status=$? + expect "40: invalid argument '$bad_arg' exits 2" "2" "$status" +done + +check_bad_combo() { + local label=$1 + shift + local out_file="$test_tmp/out" err_file="$test_tmp/err" status=0 err_first + run_doctor -- "$@" >"$out_file" 2>"$err_file" || status=$? + expect "40: $label exits 2" "2" "$status" + expect "40: $label writes nothing to stdout" "" "$(<"$out_file")" + err_first=$(grep -v '^[^:]*bash: warning: setlocale:' "$err_file" | head -n 1 || true) + expect "40: $label prints usage on stderr" "$usage_line" "$err_first" +} + +check_bad_combo "--list nonsense" --list nonsense +check_bad_combo "nonsense --list" nonsense --list +check_bad_combo "--json nonsense" --json nonsense + +out_file="$test_tmp/out" +status=0 +run_doctor -- --help nonsense >"$out_file" 2>/dev/null || status=$? +expect "40: --help nonsense exits 0" "0" "$status" +expect "40: --help nonsense prints usage on stdout" "$usage_line" "$(head -n 1 "$out_file")" + +status=0 +run_doctor -- nonsense --help >/dev/null 2>&1 || status=$? +expect "40: nonsense --help exits 2" "2" "$status" + +expect "40: invalid runs and --help run no checks" "absent" "$([[ -e $test_tmp/probed ]] && echo present || echo absent)" +run_doctor runtime-dir >/dev/null 2>&1 +expect "40: runtime-dir run creates the df probe marker" "present" "$([[ -e $test_tmp/probed ]] && echo present || echo absent)" + +new_tree +list_out=$(run_doctor --list 2>/dev/null) +expect "41: --list prints 8 lines" "8" "$(wc -l <<<"$list_out")" +expect "41: --list ids match CHECKS order" \ + "runtime-dir shell notifications user-services migrations restarts omarchy-path theme" \ + "$(awk '{print $1}' <<<"$list_out" | paste -sd' ' -)" +list_combo=$(run_doctor --json --list theme 2>/dev/null) +expect "41: --json --list theme prints the same 8 lines" "$list_out" "$list_combo" + +dup_out=$(doctor theme theme) +expect "42: duplicate check id runs once" "1" "$(jq -r '.checks | length' <<<"$dup_out")" +order_out=$(doctor theme runtime-dir) +expect "42: selected checks run in CHECKS order" "runtime-dir theme" "$(jq -r '.checks | map(.id) | join(" ")' <<<"$order_out")" + +full_json=$(doctor) +expect "43: --json stdout is one line" "1" "$(wc -l <<<"$full_json")" +expect "43: --json top-level keys are exact" "checks omarchy version" "$(jq -r 'keys | join(" ")' <<<"$full_json")" +expect "43: --json version is 1" "1" "$(jq -r '.version' <<<"$full_json")" +expect "43: --json omarchy is stub" "stub" "$(jq -r '.omarchy' <<<"$full_json")" +expect "43: --json check ids equal CHECKS in order" \ + "runtime-dir shell notifications user-services migrations restarts omarchy-path theme" \ + "$(jq -r '.checks | map(.id) | join(" ")' <<<"$full_json")" +expect "43: --json every check has exact four keys" \ + "detail hint id result" \ + "$(jq -r '[.checks[] | keys | join(" ")] | unique | join(",")' <<<"$full_json")" +expect "43: --json every passing check has empty hint" \ + "true" \ + "$(jq -r '[.checks[] | .hint == ""] | all' <<<"$full_json")" + +new_tree +touch "$fake/omarchy/migrations/1.sh" "$fake/omarchy/migrations/2.sh" +run_doctor migrations >"$test_tmp/text.out" 2>/dev/null || true +if diff_out=$(diff -u - "$test_tmp/text.out" <<'TXT' +Omarchy doctor + + ✗ migrations 2 pending + Run: omarchy migrate + +0 passed, 1 failed, 0 skipped +TXT +); then + pass "44: text output of failed migrations matches exact six lines" +else + fail "44: text output of failed migrations matches exact six lines" "$diff_out" +fi + +new_tree +rm "$fake/run/wayland-1" +skip_text=$(run_doctor shell 2>/dev/null) +if grep -qxF ' – shell skipped: no desktop session' <<<"$skip_text"; then + pass "45: text output of skipped shell contains expected line" +else + fail "45: text output of skipped shell contains expected line" "$skip_text" +fi + +router_status=0 +router_out=$(OMARCHY_PATH="$ROOT" "$ROOT/bin/omarchy" doctor --list 2>/dev/null) || router_status=$? +expect "46: router omarchy doctor --list exits 0" "0" "$router_status" +expect "46: router omarchy doctor --list prints 8 lines" "8" "$(wc -l <<<"$router_out")" + +new_tree +setup_stale_logs +touch "$fake/home/.local/state/omarchy/reboot-required" \ + "$fake/home/.local/state/omarchy/restart-bluetooth-required" \ + "$fake/home/.local/state/omarchy/restart-walker-required" \ + "$fake/home/.local/state/omarchy/restart--required" +mkdir -p "$fake/home/.local/state/omarchy/restart-dir-required" +before_tree=$(find "$fake" -printf '%p %y %s %T@ %l\n' | LC_ALL=C sort) +run_doctor >/dev/null 2>&1 || true +after_tree=$(find "$fake" -printf '%p %y %s %T@ %l\n' | LC_ALL=C sort) +expect "47: full doctor run leaves fake tree completely unchanged" "$before_tree" "$after_tree" + +expect "48: doctor never invoked sudo or pkexec" "absent" "$([[ -e $test_tmp/escalated ]] && echo present || echo absent)" From 1769ad9973bc74fdc1d76aae3a21cb43dfe3d6ed Mon Sep 17 00:00:00 2001 From: Joshua Pruitt <50942+photuris@users.noreply.github.com> Date: Mon, 30 Nov 2026 20:35:38 -0600 Subject: [PATCH 06/15] count shells from every checkout and skip migrations when the helper fails in omarchy doctor --- bin/omarchy-doctor | 73 +++++++++++++++++------ test/shell.d/doctor-test.sh | 114 +++++++++++++++++++++++++++++++++--- 2 files changed, 161 insertions(+), 26 deletions(-) diff --git a/bin/omarchy-doctor b/bin/omarchy-doctor index 6d88062c262a..3bea09274e39 100755 --- a/bin/omarchy-doctor +++ b/bin/omarchy-doctor @@ -105,8 +105,25 @@ check_runtime_dir() { fi } +is_omarchy_shell() { + local pid=$1 config_path=$2 tree + + [[ $config_path == *"/shell/shell.qml" ]] || return 1 + tree=${config_path%/shell/shell.qml} + + if [[ -f $tree/bin/omarchy-launch-shell && -x $tree/bin/omarchy-launch-shell ]]; then + return 0 + elif [[ $pid =~ ^[0-9]+$ && -r $root/proc/$pid/environ ]] && + tr '\0' '\n' 2>/dev/null <"$root/proc/$pid/environ" | grep -Fx "OMARCHY_PATH=$tree" >/dev/null; then + return 0 + else + return 1 + fi +} + check_shell() { - local max_instances + local line pid="" config_path="" display="" in_block=false count max_instances=0 + local -A counts=() if ! has_session; then echo "no desktop session" @@ -119,17 +136,33 @@ check_shell() { return $FAIL fi - max_instances=$( - LC_ALL=C timeout --kill-after=1s 2s quickshell list -p "${OMARCHY_PATH:-}/shell" --any-display 2>/dev/null | - awk ' - /^ Display connection: / { - display = substr($0, 23) - count[display]++ - if (count[display] > max) max = count[display] - } - END { print max + 0 } - ' - ) + while IFS= read -r line || [[ -n $line ]]; do + if [[ $line == "Instance "* ]]; then + if [[ $in_block == "true" && -n $display ]] && is_omarchy_shell "$pid" "$config_path"; then + count=$(( ${counts[$display]:-0} + 1 )) + counts[$display]=$count + (( count > max_instances )) && max_instances=$count + fi + in_block=true + pid="" + config_path="" + display="" + elif [[ $in_block == "true" ]]; then + if [[ $line == " Process ID: "* ]]; then + pid=${line#" Process ID: "} + elif [[ $line == " Config path: "* ]]; then + config_path=${line#" Config path: "} + elif [[ $line == " Display connection: "* ]]; then + display=${line#" Display connection: "} + fi + fi + done < <(LC_ALL=C timeout --kill-after=1s 2s quickshell list -a 2>/dev/null) + + if [[ $in_block == "true" && -n $display ]] && is_omarchy_shell "$pid" "$config_path"; then + count=$(( ${counts[$display]:-0} + 1 )) + counts[$display]=$count + (( count > max_instances )) && max_instances=$count + fi if (( max_instances > 1 )); then echo "$max_instances instances on one display" @@ -187,18 +220,22 @@ check_user_services() { } check_migrations() { - local pending n + local pending status n - pending=$(timeout --kill-after=1s 5s omarchy-migrate --pending 2>/dev/null) || true + pending=$(timeout --kill-after=1s 5s omarchy-migrate --pending 2>/dev/null) + status=$? n=$(awk 'NF { count++ } END { print count + 0 }' <<<"$pending") - if (( n == 0 )); then - echo "none pending" - return $PASS - else + if (( status == 0 && n > 0 )); then echo "$n pending" echo "Run: omarchy migrate" return $FAIL + elif (( status == 1 )); then + echo "none pending" + return $PASS + else + echo "omarchy-migrate did not answer" + return $SKIP fi } diff --git a/test/shell.d/doctor-test.sh b/test/shell.d/doctor-test.sh index 6f36184eda8a..b8cd6dcf6301 100644 --- a/test/shell.d/doctor-test.sh +++ b/test/shell.d/doctor-test.sh @@ -4,7 +4,7 @@ set -euo pipefail source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" -unset DF_PERCENT SHELL_PING QS_LIST BUSCTL_REPLY BUSCTL_STATUS SYSTEMCTL_STATUS FAILED_UNITS +unset DF_PERCENT SHELL_PING QS_LIST BUSCTL_REPLY BUSCTL_STATUS SYSTEMCTL_STATUS FAILED_UNITS MIGRATE_OUT MIGRATE_STATUS test_tmp=$(mktemp -d) trap 'rm -rf "$test_tmp"' EXIT @@ -29,7 +29,9 @@ SH cat >"$stub_bin/quickshell" <<'SH' #!/bin/bash -printf '%b' "${QS_LIST:-}" +if (( $# == 2 )) && [[ $1 == "list" && $2 == "-a" ]]; then + printf '%b' "${QS_LIST:-}" +fi SH cat >"$stub_bin/busctl" <<'SH' @@ -98,7 +100,8 @@ new_tree() { "$fake/omarchy/bin" "$fake/omarchy/migrations" "$fake/etc" "$fake/proc" touch "$fake/run/wayland-0.lock" "$fake/run/wayland-1" "$fake/run/wayland-1.lock" printf '#!/bin/bash\n' >"$fake/omarchy/bin/omarchy" - chmod +x "$fake/omarchy/bin/omarchy" + printf '#!/bin/bash\n' >"$fake/omarchy/bin/omarchy-launch-shell" + chmod +x "$fake/omarchy/bin/omarchy" "$fake/omarchy/bin/omarchy-launch-shell" printf 'export OMARCHY_PATH="%s"\n' "$fake/omarchy" >"$fake/etc/omarchy.conf" touch "$fake/home/.local/state/omarchy/current/theme/colors.toml" printf 'ristretto\n' >"$fake/home/.local/state/omarchy/current/theme.name" @@ -241,16 +244,91 @@ new_tree expect "9: shell fails when ping exits non-zero" "fail: not answering" "$(SHELL_PING=1 outcome shell)" expect "9: shell failure hint suggests restarting shell" "Run: omarchy restart shell" "$(SHELL_PING=1 hint_of shell)" +qs_instance() { + local pid=$1 config_path=$2 display=$3 + printf 'Instance inst-%s:\n Process ID: %s\n Shell ID: 2ccd0973d86f3fbc931cfeefa353cba9\n Config path: %s\n Display connection: %s\n Launch time: 2026-11-30 18:58:01 (running for 1 hours, 32 minutes, 41 seconds)\n' \ + "$pid" "$pid" "$config_path" "$display" +} + +qs_list=$( + qs_instance 101 "$fake/omarchy/shell/shell.qml" "wayland/wayland-1" + qs_instance 102 "$fake/omarchy/shell/shell.qml" "wayland/wayland-1" +) expect "10: two shell instances on one display fail" "fail: 2 instances on one display" \ - "$(QS_LIST='Instance 1\n Display connection: wayland/wayland-1\nInstance 2\n Display connection: wayland/wayland-1\n' outcome shell)" + "$(QS_LIST=$qs_list outcome shell)" expect "10: duplicate shell instances hint suggests restarting shell" "Run: omarchy restart shell" \ - "$(QS_LIST='Instance 1\n Display connection: wayland/wayland-1\nInstance 2\n Display connection: wayland/wayland-1\n' hint_of shell)" - + "$(QS_LIST=$qs_list hint_of shell)" + +new_tree +mkdir -p "$fake/other/bin" +printf '#!/bin/bash\n' >"$fake/other/bin/omarchy-launch-shell" +chmod +x "$fake/other/bin/omarchy-launch-shell" +qs_list=$( + qs_instance 101 "$fake/omarchy/shell/shell.qml" "wayland/wayland-1" + qs_instance 102 "$fake/other/shell/shell.qml" "wayland/wayland-1" +) +expect "10a: two shell instances from different Omarchy trees on one display fail" "fail: 2 instances on one display" \ + "$(QS_LIST=$qs_list outcome shell)" +expect "10a: duplicate shell instances across trees hint suggests restarting shell" "Run: omarchy restart shell" \ + "$(QS_LIST=$qs_list hint_of shell)" + +new_tree +mkdir -p "$fake/proc/4242" +printf 'HOME=/x\0OMARCHY_PATH=%s\0' "$fake/gone" >"$fake/proc/4242/environ" +qs_list=$( + qs_instance 101 "$fake/omarchy/shell/shell.qml" "wayland/wayland-1" + qs_instance 4242 "$fake/gone/shell/shell.qml" "wayland/wayland-1" +) +expect "10b: deleted tree shell matched by OMARCHY_PATH in proc environ counts" "fail: 2 instances on one display" \ + "$(QS_LIST=$qs_list outcome shell)" + +printf 'HOME=/x\0OMARCHY_PATH=%s\0' "$fake/gone-too" >"$fake/proc/4242/environ" +expect "10c: OMARCHY_PATH prefix in proc environ does not match deleted tree" "pass: answering" \ + "$(QS_LIST=$qs_list outcome shell)" + +new_tree +qs_list=$( + qs_instance 101 "$fake/omarchy/shell/shell.qml" "wayland/wayland-1" + qs_instance 102 "$fake/home/.config/quickshell/shell.qml" "wayland/wayland-1" +) +expect "10d: non-Omarchy quickshell config path is not counted" "pass: answering" \ + "$(QS_LIST=$qs_list outcome shell)" + +new_tree +mkdir -p "$fake/mine/shell" +qs_list=$( + qs_instance 101 "$fake/omarchy/shell/shell.qml" "wayland/wayland-1" + qs_instance 102 "$fake/mine/shell/shell.qml" "wayland/wayland-1" +) +expect "10e: directory ending in /shell/shell.qml without launcher or environ is not counted" "pass: answering" \ + "$(QS_LIST=$qs_list outcome shell)" + +new_tree +mkdir -p "$fake/my tree/bin" +printf '#!/bin/bash\n' >"$fake/my tree/bin/omarchy-launch-shell" +chmod +x "$fake/my tree/bin/omarchy-launch-shell" +qs_list=$( + qs_instance 101 "$fake/omarchy/shell/shell.qml" "wayland/wayland-1" + qs_instance 102 "$fake/my tree/shell/shell.qml" "wayland/wayland-1" +) +expect "10f: Omarchy tree path containing spaces is counted" "fail: 2 instances on one display" \ + "$(QS_LIST=$qs_list outcome shell)" + +new_tree +qs_list=$( + qs_instance 101 "$fake/omarchy/shell/shell.qml" "wayland/wayland-1" + qs_instance 102 "$fake/omarchy/shell/shell.qml" "wayland/wayland-2" +) expect "11: two shell instances on different displays pass" "pass: answering" \ - "$(QS_LIST='Instance 1\n Display connection: wayland/wayland-1\nInstance 2\n Display connection: wayland/wayland-2\n' outcome shell)" + "$(QS_LIST=$qs_list outcome shell)" +qs_list=$( + qs_instance 101 "$fake/omarchy/shell/shell.qml" "wayland/wayland-2" + qs_instance 102 "$fake/omarchy/shell/shell.qml" "wayland/wayland-1" + qs_instance 103 "$fake/omarchy/shell/shell.qml" "wayland/wayland-2" +) expect "11a: non-adjacent duplicate display instances fail" "fail: 2 instances on one display" \ - "$(QS_LIST='Instance 1\n Display connection: wayland/wayland-2\nInstance 2\n Display connection: wayland/wayland-1\nInstance 3\n Display connection: wayland/wayland-2\n' outcome shell)" + "$(QS_LIST=$qs_list outcome shell)" expect "12: empty quickshell list passes when ping answers" "pass: answering" "$(outcome shell)" @@ -308,6 +386,26 @@ expect "21: migrations hint suggests omarchy migrate" "Run: omarchy migrate" "$( rm "$fake/home/.local/state/omarchy/migrations/1.sh" expect "22: migrations fails with 2 pending when no markers exist" "fail: 2 pending" "$(outcome migrations)" +migrate_stub_bin="$test_tmp/migrate-bin" +mkdir -p "$migrate_stub_bin" +cat >"$migrate_stub_bin/omarchy-migrate" <<'SH' +#!/bin/bash +printf '%b' "${MIGRATE_OUT:-}" +exit "${MIGRATE_STATUS:-0}" +SH +chmod +x "$migrate_stub_bin/omarchy-migrate" + +new_tree +migrate_path="$migrate_stub_bin:$stub_bin:$ROOT/bin:$PATH" +expect "22a: migrations skips when omarchy-migrate times out" "skip: omarchy-migrate did not answer" \ + "$(MIGRATE_STATUS=124 outcome --env "PATH=$migrate_path" migrations)" +expect "22b: migrations skips when omarchy-migrate fails to launch" "skip: omarchy-migrate did not answer" \ + "$(MIGRATE_STATUS=127 outcome --env "PATH=$migrate_path" migrations)" +expect "22c: migrations skips on timeout even with partial output" "skip: omarchy-migrate did not answer" \ + "$(MIGRATE_OUT='1.sh\n' MIGRATE_STATUS=124 outcome --env "PATH=$migrate_path" migrations)" +expect "22d: migrations skips when omarchy-migrate exits 0 with no output" "skip: omarchy-migrate did not answer" \ + "$(MIGRATE_STATUS=0 outcome --env "PATH=$migrate_path" migrations)" + # restarts new_tree expect "23: restarts passes when no marker files exist" "pass: none pending" "$(outcome restarts)" From bee0deaa8ff00cc6852ef873da1a6a870be1c09e Mon Sep 17 00:00:00 2001 From: Joshua Pruitt <50942+photuris@users.noreply.github.com> Date: Tue, 1 Dec 2026 00:44:55 -0600 Subject: [PATCH 07/15] Add omarchy mode for reversible desktop presets --- bin/omarchy | 1 + bin/omarchy-mode | 671 +++++++++++++++++++++++++++++ default/omarchy/modes/focus.conf | 2 + default/omarchy/modes/present.conf | 4 + default/omarchy/modes/travel.conf | 3 + test/shell.d/mode-test.sh | 600 ++++++++++++++++++++++++++ 6 files changed, 1281 insertions(+) create mode 100755 bin/omarchy-mode create mode 100644 default/omarchy/modes/focus.conf create mode 100644 default/omarchy/modes/present.conf create mode 100644 default/omarchy/modes/travel.conf create mode 100755 test/shell.d/mode-test.sh diff --git a/bin/omarchy b/bin/omarchy index d59b61822246..16906eea1186 100755 --- a/bin/omarchy +++ b/bin/omarchy @@ -68,6 +68,7 @@ GROUP_DESCRIPTIONS[kernel]="Default boot kernel selection" GROUP_DESCRIPTIONS[language]="Display language and system locale" GROUP_DESCRIPTIONS[menu]="Omarchy menu commands" GROUP_DESCRIPTIONS[migrate]="Migration runner" +GROUP_DESCRIPTIONS[mode]="Desktop modes that set several settings at once and put them back" GROUP_DESCRIPTIONS[monitor]="Monitor status helpers" GROUP_DESCRIPTIONS[network]="Network, Wi-Fi, and Ethernet controls" GROUP_DESCRIPTIONS[notification]="Notification helpers" diff --git a/bin/omarchy-mode b/bin/omarchy-mode new file mode 100755 index 000000000000..bce344dc4a97 --- /dev/null +++ b/bin/omarchy-mode @@ -0,0 +1,671 @@ +#!/bin/bash + +# omarchy:summary=Apply a desktop mode that sets several settings at once, and put them back +# omarchy:subcommand= — Apply a mode such as focus, present or travel +# omarchy:subcommand=off — Leave the active mode and restore the settings it changed +# omarchy:subcommand=status [--json] — Show the active mode and the settings it holds +# omarchy:subcommand=list — List the built-in modes and your own +# omarchy:examples=omarchy mode present | omarchy mode off | omarchy mode status --json + +set -uo pipefail + +STATE_DIR="$HOME/.local/state/omarchy" +STATE_FILE="$STATE_DIR/mode.json" +LOCK_FILE="$STATE_DIR/mode.lock" +STATE_TMP="$STATE_DIR/mode.json.tmp" +BUILTIN_DIR="$OMARCHY_PATH/default/omarchy/modes" +USER_DIR="$HOME/.config/omarchy/modes" + +LOCK_FD="" + +PRESET_KEYS=() +declare -A PRESET_VALUES=() + +STATE_MODE="" +STATE_SINCE="" +STATE_KEYS=() +STATE_PRIORS=() +STATE_APPLIEDS=() +declare -A OLD_INDEX=() + +warn() { + printf 'omarchy-mode: %s\n' "$1" >&2 +} + +usage() { + warn "Usage: omarchy-mode |off|status [--json]|list" +} + +is_valid_mode_name() { + local name="$1" + local re='^[abcdefghijklmnopqrstuvwxyz0123456789][abcdefghijklmnopqrstuvwxyz0123456789-]*$' + + [[ $name =~ $re ]] && [[ $name != "off" && $name != "status" && $name != "list" ]] +} + +setting_allowed_values() { + case $1 in + dnd) printf '%s\n' on off ;; + stay-awake) printf '%s\n' on off ;; + bar) printf '%s\n' shown hidden ;; + animations) printf '%s\n' on off ;; + power-profile) printf '%s\n' power-saver balanced performance ;; + *) return 1 ;; + esac +} + +is_known_setting() { + setting_allowed_values "$1" >/dev/null +} + +is_allowed_value() { + local key="$1" value="$2" allowed + + while IFS= read -r allowed; do + if [[ $allowed == "$value" ]]; then + return 0 + fi + done < <(setting_allowed_values "$key") + + return 1 +} + +setting_read() { + local key="$1" value="" + + case $key in + dnd) + if value=$(omarchy-shell notifications isDnd 2>/dev/null) && + [[ $value == "on" || $value == "off" ]]; then + printf '%s\n' "$value" + return 0 + else + return 1 + fi + ;; + stay-awake) + if [[ -f $HOME/.local/state/omarchy/indicators/stay-awake ]]; then + printf 'on\n' + else + printf 'off\n' + fi + return 0 + ;; + bar) + if omarchy-toggle-enabled bar-off >/dev/null 2>&1; then + printf 'hidden\n' + else + printf 'shown\n' + fi + return 0 + ;; + animations) + if omarchy-hyprland-animations-off >/dev/null 2>&1; then + printf 'off\n' + else + printf 'on\n' + fi + return 0 + ;; + power-profile) + if value=$(powerprofilesctl get 2>/dev/null) && + [[ $value == "power-saver" || $value == "balanced" || $value == "performance" ]]; then + printf '%s\n' "$value" + return 0 + else + return 1 + fi + ;; + *) return 1 ;; + esac +} + +setting_write() { + local key="$1" value="$2" output="" + + case $key in + dnd) + if output=$(omarchy-shell notifications setDnd "$value" 2>/dev/null) && [[ $output == "$value" ]]; then + omarchy-shell -q omarchy.indicators refresh >/dev/null 2>&1 + return 0 + else + return 1 + fi + ;; + stay-awake) + if [[ $value == "on" ]]; then + omarchy-toggle-idle stay-awake >/dev/null 2>&1 + else + omarchy-toggle-idle allow-idle >/dev/null 2>&1 + fi + ;; + bar) + if [[ $value == "hidden" ]]; then + omarchy-toggle-bar on >/dev/null 2>&1 + else + omarchy-toggle-bar off >/dev/null 2>&1 + fi + ;; + animations) + omarchy-toggle-animations "$value" >/dev/null 2>&1 + ;; + power-profile) + omarchy-powerprofiles-set autodetect "$value" >/dev/null 2>&1 + ;; + *) return 1 ;; + esac +} + +acquire_lock() { + if ! mkdir -p "$STATE_DIR" 2>/dev/null; then + warn "could not lock $STATE_DIR" + return 1 + fi + + if ! { exec {LOCK_FD}>"$LOCK_FILE"; } 2>/dev/null; then + LOCK_FD="" + warn "could not lock $STATE_DIR" + return 1 + fi + + if ! flock "$LOCK_FD" 2>/dev/null; then + exec {LOCK_FD}>&- 2>/dev/null + LOCK_FD="" + warn "could not lock $STATE_DIR" + return 1 + fi + + return 0 +} + +release_lock() { + if [[ -n $LOCK_FD ]]; then + exec {LOCK_FD}>&- 2>/dev/null + LOCK_FD="" + fi +} + +validate_state_raw() { + local raw="$1" + + # Slurping catches empty files and trailing JSON values as well as bad types. + jq -se ' + length == 1 + and (.[0] | type == "object") + and (.[0].mode | type == "string") + and (.[0].since | type == "string") + and (.[0].settings | type == "object") + and (.[0].settings | to_entries | all( + (.value | type == "object") + and (.value.prior | type == "string") + and (.value.applied | type == "string") + )) + ' <<<"$raw" >/dev/null 2>&1 +} + +parse_state_raw() { + local raw="$1" + local key prior applied idx=0 + + STATE_MODE="" + STATE_SINCE="" + STATE_KEYS=() + STATE_PRIORS=() + STATE_APPLIEDS=() + OLD_INDEX=() + + { + IFS= read -r -d '' STATE_MODE + IFS= read -r -d '' STATE_SINCE + while IFS= read -r -d '' key && + IFS= read -r -d '' prior && + IFS= read -r -d '' applied; do + STATE_KEYS+=("$key") + STATE_PRIORS+=("$prior") + STATE_APPLIEDS+=("$applied") + if [[ -n $key ]]; then + OLD_INDEX["$key"]=$idx + fi + idx=$(( idx + 1 )) + done + } < <( + jq -j ' + .mode, "\u0000", + .since, "\u0000", + (.settings | to_entries[] | .key, "\u0000", .value.prior, "\u0000", .value.applied, "\u0000") + ' <<<"$raw" 2>/dev/null + ) +} + +# Returns 0 with state loaded, 2 when there is no state file, 1 when invalid. +read_state_file() { + local raw="" + + if ! raw=$(cat "$STATE_FILE" 2>/dev/null); then + if [[ ! -e $STATE_FILE ]]; then + return 2 + else + warn "$STATE_FILE is not a valid mode state; remove it to reset" + return 1 + fi + fi + + if ! validate_state_raw "$raw"; then + warn "$STATE_FILE is not a valid mode state; remove it to reset" + return 1 + fi + + parse_state_raw "$raw" + return 0 +} + +write_state() { + local mode="$1" since="$2" + shift 2 + + if ! { + jq -cn --arg mode "$mode" --arg since "$since" ' + ($ARGS.positional) as $a | + { + mode: $mode, + since: $since, + settings: ( + reduce range(0; ($a | length); 3) as $i ({}; . + {($a[$i]): {prior: $a[$i + 1], applied: $a[$i + 2]}}) + ) + } + ' --args "$@" >"$STATE_TMP" + } 2>/dev/null; then + warn "could not write $STATE_FILE" + return 1 + fi + + if ! mv -f "$STATE_TMP" "$STATE_FILE" 2>/dev/null; then + warn "could not write $STATE_FILE" + return 1 + fi + + return 0 +} + +remove_state() { + if rm -f "$STATE_FILE" 2>/dev/null; then + return 0 + else + warn "could not write $STATE_FILE" + return 1 + fi +} + +resolve_preset() { + local name="$1" + + if [[ -f $USER_DIR/$name.conf ]]; then + printf '%s\n' "$USER_DIR/$name.conf" + return 0 + elif [[ -f $BUILTIN_DIR/$name.conf ]]; then + printf '%s\n' "$BUILTIN_DIR/$name.conf" + return 0 + else + return 1 + fi +} + +parse_preset() { + local path="$1" + local line="" key="" value="" + local line_num=0 + local tab=$'\t' + local kv_re='^([abcdefghijklmnopqrstuvwxyz][abcdefghijklmnopqrstuvwxyz-]*)=([abcdefghijklmnopqrstuvwxyz][abcdefghijklmnopqrstuvwxyz-]*)$' + + PRESET_KEYS=() + PRESET_VALUES=() + + while IFS= read -r line || [[ -n $line ]]; do + line_num=$(( line_num + 1 )) + + if [[ $line == *$'\r'* ]]; then + warn "$path:$line_num: not a key=value line" + return 2 + elif [[ -z ${line//[ $tab]/} ]]; then + continue + elif [[ $line == "#"* ]]; then + continue + elif [[ $line =~ $kv_re ]]; then + key=${BASH_REMATCH[1]} + value=${BASH_REMATCH[2]} + if ! is_known_setting "$key"; then + warn "$path:$line_num: unknown setting $key" + return 2 + elif ! is_allowed_value "$key" "$value"; then + warn "$path:$line_num: $value is not a value for $key" + return 2 + elif [[ -n ${PRESET_VALUES[$key]:-} ]]; then + warn "$path:$line_num: $key is set twice" + return 2 + else + PRESET_KEYS+=("$key") + PRESET_VALUES["$key"]="$value" + fi + else + warn "$path:$line_num: not a key=value line" + return 2 + fi + done <"$path" + + if (( ${#PRESET_KEYS[@]} == 0 )); then + warn "$path: no settings" + return 2 + else + return 0 + fi +} + +preset_summary() { + local path="$1" first_line="" + + { IFS= read -r first_line <"$path"; } 2>/dev/null || true + if [[ $first_line == "# "* ]]; then + printf '%s' "${first_line#\# }" + fi +} + +# Returns 0 when done, 1 when the entry must be kept. +restore_entry() { + local key="$1" prior="$2" applied="$3" + local current="" + + if ! is_known_setting "$key"; then + warn "dropping unknown setting $key" + return 0 + fi + + if ! is_allowed_value "$key" "$prior" || ! is_allowed_value "$key" "$applied"; then + warn "dropping invalid entry $key" + return 0 + fi + + if ! current=$(setting_read "$key"); then + warn "$key is unavailable, not restored" + return 1 + fi + + if [[ $current != "$applied" ]]; then + return 0 + fi + + if [[ $current == "$prior" ]]; then + return 0 + fi + + if setting_write "$key" "$prior"; then + return 0 + else + warn "could not restore $key" + return 1 + fi +} + +cmd_status() { + local format="$1" + local raw="" rc=0 i + + if raw=$(cat "$STATE_FILE" 2>/dev/null); then + rc=0 + else + rc=$? + if [[ ! -e $STATE_FILE ]]; then + rc=2 + fi + fi + + if (( rc == 2 )); then + if [[ $format == "json" ]]; then + printf '{"mode":null}\n' + else + printf 'none\n' + fi + return 0 + elif (( rc != 0 )) || ! validate_state_raw "$raw"; then + warn "$STATE_FILE is not a valid mode state; remove it to reset" + return 1 + else + if [[ $format == "json" ]]; then + jq -c . <<<"$raw" 2>/dev/null + else + parse_state_raw "$raw" + printf '%s since %s\n' "$STATE_MODE" "$STATE_SINCE" + for (( i = 0; i < ${#STATE_KEYS[@]}; i++ )); do + printf '%s=%s (was %s)\n' "${STATE_KEYS[$i]}" "${STATE_APPLIEDS[$i]}" "${STATE_PRIORS[$i]}" + done + fi + return 0 + fi +} + +cmd_list() { + local -A preset_files=() + local dir file base name summary + + for dir in "$BUILTIN_DIR" "$USER_DIR"; do + [[ -d $dir ]] || continue + for file in "$dir"/*.conf; do + [[ -f $file ]] || continue + base=${file##*/} + name=${base%.conf} + if is_valid_mode_name "$name"; then + preset_files["$name"]="$file" + fi + done + done + + if (( ${#preset_files[@]} > 0 )); then + while IFS= read -r name; do + summary=$(preset_summary "${preset_files[$name]}") + printf '%s\t%s\n' "$name" "$summary" + done < <(printf '%s\n' "${!preset_files[@]}" | LC_ALL=C sort) + fi + + return 0 +} + +cmd_off() { + local rc=0 i + local -a kept_entries=() + + if ! acquire_lock; then + return 1 + fi + + read_state_file + rc=$? + if (( rc == 2 )); then + release_lock + printf 'none\n' + return 0 + elif (( rc != 0 )); then + release_lock + return 1 + fi + + for (( i = 0; i < ${#STATE_KEYS[@]}; i++ )); do + if ! restore_entry "${STATE_KEYS[$i]}" "${STATE_PRIORS[$i]}" "${STATE_APPLIEDS[$i]}"; then + kept_entries+=("${STATE_KEYS[$i]}" "${STATE_PRIORS[$i]}" "${STATE_APPLIEDS[$i]}") + fi + done + + if (( ${#kept_entries[@]} == 0 )); then + if ! remove_state; then + release_lock + return 1 + fi + release_lock + omarchy-hook mode-set off >/dev/null + printf 'none\n' + return 0 + else + if ! write_state "$STATE_MODE" "$STATE_SINCE" "${kept_entries[@]}"; then + release_lock + return 1 + fi + release_lock + cmd_status text + return 1 + fi +} + +cmd_apply() { + local name="$1" + local preset_path="" rc=0 + local key="" current="" prior="" applied="" since="" + local old_idx=0 i=0 + local carried_count=0 fresh_count=0 had_failure=0 + local -A preset_class=() + local -A carried_keys=() + local -A kept_keys=() + local -a kept_entries=() + local -a new_entries=() + local -a fresh_keys=() + local -a fresh_priors=() + local -a fresh_applieds=() + local -a all_entries=() + + if ! is_valid_mode_name "$name"; then + warn "Unknown mode: $name" + return 2 + fi + + if ! preset_path=$(resolve_preset "$name"); then + warn "Unknown mode: $name" + return 2 + fi + + if ! parse_preset "$preset_path"; then + return 2 + fi + + if ! acquire_lock; then + return 1 + fi + + STATE_MODE="" + STATE_SINCE="" + STATE_KEYS=() + STATE_PRIORS=() + STATE_APPLIEDS=() + OLD_INDEX=() + + read_state_file + rc=$? + if (( rc == 1 )); then + release_lock + return 1 + fi + + for key in "${PRESET_KEYS[@]}"; do + if ! current=$(setting_read "$key"); then + warn "$key is unavailable, skipping" + preset_class["$key"]="skip" + elif [[ -n ${OLD_INDEX[$key]:-} ]]; then + old_idx=${OLD_INDEX[$key]} + if [[ $current == "${STATE_APPLIEDS[$old_idx]}" && ${PRESET_VALUES[$key]} == "${STATE_APPLIEDS[$old_idx]}" ]]; then + preset_class["$key"]="carried" + carried_keys["$key"]=1 + carried_count=$(( carried_count + 1 )) + else + preset_class["$key"]="fresh" + fresh_count=$(( fresh_count + 1 )) + fi + else + preset_class["$key"]="fresh" + fresh_count=$(( fresh_count + 1 )) + fi + done + + if (( carried_count == 0 && fresh_count == 0 )); then + warn "none of the settings of $name are available" + release_lock + return 1 + fi + + for (( i = 0; i < ${#STATE_KEYS[@]}; i++ )); do + key=${STATE_KEYS[$i]} + if [[ -n $key && -n ${carried_keys[$key]:-} ]]; then + continue + fi + if ! restore_entry "$key" "${STATE_PRIORS[$i]}" "${STATE_APPLIEDS[$i]}"; then + kept_entries+=("$key" "${STATE_PRIORS[$i]}" "${STATE_APPLIEDS[$i]}") + if [[ -n $key ]]; then + kept_keys["$key"]=1 + fi + had_failure=1 + fi + done + + for key in "${PRESET_KEYS[@]}"; do + if [[ ${preset_class[$key]} == "carried" ]]; then + old_idx=${OLD_INDEX[$key]} + new_entries+=("$key" "${STATE_PRIORS[$old_idx]}" "${PRESET_VALUES[$key]}") + elif [[ ${preset_class[$key]} == "fresh" ]]; then + if [[ -n ${kept_keys[$key]:-} ]]; then + warn "$key is still held by the previous mode, skipping" + elif current=$(setting_read "$key"); then + prior="$current" + applied=${PRESET_VALUES[$key]} + new_entries+=("$key" "$prior" "$applied") + fresh_keys+=("$key") + fresh_priors+=("$prior") + fresh_applieds+=("$applied") + else + warn "$key is unavailable, skipping" + fi + fi + done + + since=$(date -Iseconds) + all_entries=("${kept_entries[@]}" "${new_entries[@]}") + if ! write_state "$name" "$since" "${all_entries[@]}"; then + release_lock + return 1 + fi + + for (( i = 0; i < ${#fresh_keys[@]}; i++ )); do + if [[ ${fresh_priors[$i]} != "${fresh_applieds[$i]}" ]]; then + if ! setting_write "${fresh_keys[$i]}" "${fresh_applieds[$i]}"; then + warn "could not set ${fresh_keys[$i]}" + had_failure=1 + fi + fi + done + + release_lock + omarchy-hook mode-set "$name" >/dev/null + cmd_status text || had_failure=1 + + if (( had_failure )); then + return 1 + else + return 0 + fi +} + +main() { + if (( $# == 1 )); then + case $1 in + status) cmd_status text ;; + list) cmd_list ;; + off) cmd_off ;; + --json) + usage + return 2 + ;; + *) cmd_apply "$1" ;; + esac + elif (( $# == 2 )) && [[ $1 == "status" && $2 == "--json" ]]; then + cmd_status json + else + usage + return 2 + fi +} + +main "$@" diff --git a/default/omarchy/modes/focus.conf b/default/omarchy/modes/focus.conf new file mode 100644 index 000000000000..40d1e33cddca --- /dev/null +++ b/default/omarchy/modes/focus.conf @@ -0,0 +1,2 @@ +# Silence notifications +dnd=on diff --git a/default/omarchy/modes/present.conf b/default/omarchy/modes/present.conf new file mode 100644 index 000000000000..f7a5575b8a61 --- /dev/null +++ b/default/omarchy/modes/present.conf @@ -0,0 +1,4 @@ +# Silence notifications, stay awake and hide the bar +dnd=on +stay-awake=on +bar=hidden diff --git a/default/omarchy/modes/travel.conf b/default/omarchy/modes/travel.conf new file mode 100644 index 000000000000..f618618ca9d8 --- /dev/null +++ b/default/omarchy/modes/travel.conf @@ -0,0 +1,3 @@ +# Save power with the power-saver profile and no animations +power-profile=power-saver +animations=off diff --git a/test/shell.d/mode-test.sh b/test/shell.d/mode-test.sh new file mode 100755 index 000000000000..adb5642ec45e --- /dev/null +++ b/test/shell.d/mode-test.sh @@ -0,0 +1,600 @@ +#!/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 + +export HOME="$tmp_dir/home" +export XDG_RUNTIME_DIR="$tmp_dir/run" +export STUB_WORLD="$tmp_dir/world" +unset WAYLAND_DISPLAY HYPRLAND_INSTANCE_SIGNATURE OMARCHY_POWERPROFILES_STATE_DIR + +mkdir -p "$HOME" "$XDG_RUNTIME_DIR" "$STUB_WORLD" "$tmp_dir/stubs" + +cat >"$tmp_dir/stubs/omarchy-shell" <<'EOF' +#!/bin/bash + +if [[ ${1:-} == "-q" ]]; then + shift +fi + +if [[ ${1:-} == "notifications" && ${2:-} == "isDnd" ]]; then + if [[ -e $STUB_WORLD/shell-down ]]; then + exit 1 + else + cat "$STUB_WORLD/dnd" + exit 0 + fi +elif [[ ${1:-} == "notifications" && ${2:-} == "setDnd" ]]; then + if [[ -e $STUB_WORLD/shell-down ]]; then + exit 1 + fi + printf '%s\n' "$3" >>"$STUB_WORLD/setdnd.log" + if [[ -e $STUB_WORLD/kill-on-setdnd ]]; then + kill -TERM 0 + sleep 5 + fi + if [[ -e $STUB_WORLD/setdnd-fail ]]; then + cat "$STUB_WORLD/dnd" + exit 0 + else + printf '%s\n' "$3" >"$STUB_WORLD/dnd" + printf '%s\n' "$3" + exit 0 + fi +else + printf '%s\n' "$*" >>"$STUB_WORLD/shell.log" + exit 0 +fi +EOF + +cat >"$tmp_dir/stubs/powerprofilesctl" <<'EOF' +#!/bin/bash + +if [[ ${1:-} == "get" ]]; then + if [[ -e $STUB_WORLD/pp-down ]]; then + exit 1 + else + cat "$STUB_WORLD/pp" + fi +elif [[ ${1:-} == "set" ]]; then + printf '%s\n' "$2" >"$STUB_WORLD/pp" +elif [[ ${1:-} == "list" ]]; then + printf ' power-saver:\n* balanced:\n performance:\n' +fi +EOF + +cat >"$tmp_dir/stubs/busctl" <<'EOF' +#!/bin/bash + +printf 'b false\n' +EOF + +for stub in omarchy-restart-hyprctl omarchy-notification-send hyprctl; do + cat >"$tmp_dir/stubs/$stub" <<'EOF' +#!/bin/bash +exit 0 +EOF +done + +chmod +x \ + "$tmp_dir/stubs/omarchy-shell" \ + "$tmp_dir/stubs/powerprofilesctl" \ + "$tmp_dir/stubs/busctl" \ + "$tmp_dir/stubs/omarchy-restart-hyprctl" \ + "$tmp_dir/stubs/omarchy-notification-send" \ + "$tmp_dir/stubs/hyprctl" + +export PATH="$tmp_dir/stubs:$ROOT/bin:$PATH" + +STATE_FILE="$HOME/.local/state/omarchy/mode.json" +NO_ANIM_FLAG="$HOME/.local/state/omarchy/toggles/hypr/no-animations.lua" +BAR_FLAG="$HOME/.local/state/omarchy/toggles/bar-off" +STAY_AWAKE_FILE="$HOME/.local/state/omarchy/indicators/stay-awake" +USER_MODES_DIR="$HOME/.config/omarchy/modes" +S="$STUB_WORLD" + +OUT="" +ERR="" +RC=0 + +run_mode() { + local out_file="$tmp_dir/stdout" err_file="$tmp_dir/stderr" + if "$ROOT/bin/omarchy-mode" "$@" >"$out_file" 2>"$err_file"; then + RC=0 + else + RC=$? + fi + OUT=$(<"$out_file") + ERR=$(<"$err_file") +} + +reset_world() { + rm -rf "$HOME/.local/state/omarchy" "$HOME/.config/omarchy" "$HOME/hook.log" + rm -rf "$S"/* "$S"/.[!.]* "$S"/..?* 2>/dev/null || true + printf 'off\n' >"$S/dnd" + printf 'balanced\n' >"$S/pp" +} + +write_user_preset() { + local name="$1" content="$2" + mkdir -p "$USER_MODES_DIR" + printf '%s' "$content" >"$USER_MODES_DIR/$name.conf" +} + +expect_eq() { + local test_name="$1" label="$2" actual="$3" expected="$4" + if [[ $actual != "$expected" ]]; then + fail "$test_name" "$label: expected <$expected>, got <$actual>" + fi +} + +# 1. list prints the three built-in presets with summaries +reset_world +T="list prints the three built-in presets with summaries" +run_mode list +expect_eq "$T" "exit code" "$RC" "0" +expected=$(printf 'focus\tSilence notifications\npresent\tSilence notifications, stay awake and hide the bar\ntravel\tSave power with the power-saver profile and no animations') +expect_eq "$T" "stdout" "$OUT" "$expected" +pass "$T" + +# 2. focus turns DND on and off restores it off +reset_world +T="focus turns DND on and off restores it off" +run_mode focus +expect_eq "$T" "focus exit code" "$RC" "0" +expect_eq "$T" "dnd after focus" "$(<"$S/dnd")" "on" +expect_eq "$T" "state mode" "$(jq -r .mode "$STATE_FILE")" "focus" +expect_eq "$T" "state dnd" "$(jq -c .settings.dnd "$STATE_FILE")" '{"prior":"off","applied":"on"}' +grep -Fxq "omarchy.indicators refresh" "$S/shell.log" || + fail "$T" "shell.log missing 'omarchy.indicators refresh': $(<"$S/shell.log")" +run_mode off +expect_eq "$T" "off exit code" "$RC" "0" +expect_eq "$T" "dnd after off" "$(<"$S/dnd")" "off" +[[ ! -e $STATE_FILE ]] || fail "$T" "state file still exists after off" +pass "$T" + +# 3. off leaves DND on when it was on before the mode +reset_world +T="off leaves DND on when it was on before the mode" +printf 'on\n' >"$S/dnd" +run_mode focus +expect_eq "$T" "focus exit code" "$RC" "0" +run_mode off +expect_eq "$T" "off exit code" "$RC" "0" +expect_eq "$T" "dnd after off" "$(<"$S/dnd")" "on" +[[ ! -e $STATE_FILE ]] || fail "$T" "state file still exists after off" +[[ ! -e $S/setdnd.log ]] || fail "$T" "setdnd.log exists when no write was needed: $(<"$S/setdnd.log")" +pass "$T" + +# 4. present applies and restores three settings +reset_world +T="present applies and restores three settings" +run_mode present +expect_eq "$T" "present exit code" "$RC" "0" +expect_eq "$T" "dnd after present" "$(<"$S/dnd")" "on" +[[ -f $STAY_AWAKE_FILE ]] || fail "$T" "stay-awake file missing after present" +[[ -f $BAR_FLAG ]] || fail "$T" "bar flag missing after present" +run_mode off +expect_eq "$T" "off exit code" "$RC" "0" +expect_eq "$T" "dnd after off" "$(<"$S/dnd")" "off" +[[ ! -e $STAY_AWAKE_FILE ]] || fail "$T" "stay-awake file still exists after off" +[[ ! -e $BAR_FLAG ]] || fail "$T" "bar flag still exists after off" +[[ ! -e $STATE_FILE ]] || fail "$T" "state file still exists after off" +pass "$T" + +# 5. present leaves settings that were already set +reset_world +T="present leaves settings that were already set" +printf 'on\n' >"$S/dnd" +omarchy-toggle-idle stay-awake >/dev/null +omarchy-toggle bar-off on +run_mode present +expect_eq "$T" "present exit code" "$RC" "0" +run_mode off +expect_eq "$T" "off exit code" "$RC" "0" +expect_eq "$T" "dnd after off" "$(<"$S/dnd")" "on" +[[ -f $STAY_AWAKE_FILE ]] || fail "$T" "stay-awake file missing after off" +[[ -f $BAR_FLAG ]] || fail "$T" "bar flag missing after off" +[[ ! -e $STATE_FILE ]] || fail "$T" "state file still exists after off" +pass "$T" + +# 6. off leaves a setting changed during the mode +reset_world +T="off leaves a setting changed during the mode" +write_user_preset saver $'power-profile=power-saver\n' +run_mode saver +expect_eq "$T" "saver exit code" "$RC" "0" +expect_eq "$T" "profile after saver" "$(<"$S/pp")" "power-saver" +printf 'performance\n' >"$S/pp" +run_mode off +expect_eq "$T" "off exit code" "$RC" "0" +expect_eq "$T" "profile after off" "$(<"$S/pp")" "performance" +[[ ! -e $STATE_FILE ]] || fail "$T" "state file still exists after off" +pass "$T" + +# 7. switching modes restores settings only the old mode set +reset_world +T="switching modes restores settings only the old mode set" +run_mode present +expect_eq "$T" "present exit code" "$RC" "0" +run_mode travel +expect_eq "$T" "travel exit code" "$RC" "0" +expect_eq "$T" "dnd after switch" "$(<"$S/dnd")" "off" +[[ ! -e $STAY_AWAKE_FILE ]] || fail "$T" "stay-awake file still exists after switch" +[[ ! -e $BAR_FLAG ]] || fail "$T" "bar flag still exists after switch" +expect_eq "$T" "profile after switch" "$(<"$S/pp")" "power-saver" +[[ -f $NO_ANIM_FLAG ]] || fail "$T" "no-animations flag missing after switch" +expect_eq "$T" "state mode" "$(jq -r .mode "$STATE_FILE")" "travel" +expect_eq "$T" "state keys" "$(jq -c '.settings | keys_unsorted' "$STATE_FILE")" '["power-profile","animations"]' +run_mode off +expect_eq "$T" "off exit code" "$RC" "0" +expect_eq "$T" "profile after off" "$(<"$S/pp")" "balanced" +[[ ! -e $NO_ANIM_FLAG ]] || fail "$T" "no-animations flag still exists after off" +[[ ! -e $STATE_FILE ]] || fail "$T" "state file still exists after off" +pass "$T" + +# 8. switching modes keeps the first baseline for a shared setting +reset_world +T="switching modes keeps the first baseline for a shared setting" +run_mode focus +expect_eq "$T" "first focus exit code" "$RC" "0" +run_mode focus +expect_eq "$T" "second focus exit code" "$RC" "0" +run_mode present +expect_eq "$T" "present exit code" "$RC" "0" +expect_eq "$T" "dnd prior" "$(jq -r .settings.dnd.prior "$STATE_FILE")" "off" +expect_eq "$T" "setdnd.log" "$(<"$S/setdnd.log")" "on" +run_mode off +expect_eq "$T" "off exit code" "$RC" "0" +expect_eq "$T" "dnd after off" "$(<"$S/dnd")" "off" +pass "$T" + +# 9. an unavailable setting is skipped +reset_world +T="an unavailable setting is skipped" +touch "$S/pp-down" +run_mode travel +expect_eq "$T" "travel exit code" "$RC" "0" +[[ $ERR == *"power-profile is unavailable, skipping"* ]] || + fail "$T" "stderr missing unavailable message: $ERR" +expect_eq "$T" "state keys" "$(jq -c '.settings | keys_unsorted' "$STATE_FILE")" '["animations"]' +[[ -f $NO_ANIM_FLAG ]] || fail "$T" "no-animations flag missing after travel" +run_mode off +expect_eq "$T" "off exit code" "$RC" "0" +[[ ! -e $NO_ANIM_FLAG ]] || fail "$T" "no-animations flag still exists after off" +[[ ! -e $STATE_FILE ]] || fail "$T" "state file still exists after off" +pass "$T" + +# 10. a mode with no available setting changes nothing +reset_world +T="a mode with no available setting changes nothing" +touch "$S/shell-down" +run_mode focus +expect_eq "$T" "part 1 focus exit code" "$RC" "1" +[[ ! -e $STATE_FILE ]] || fail "$T" "part 1 created state file" + +reset_world +write_user_preset saver $'power-profile=power-saver\n' +run_mode saver +expect_eq "$T" "part 2 saver exit code" "$RC" "0" +touch "$S/shell-down" +run_mode focus +expect_eq "$T" "part 2 focus exit code" "$RC" "1" +expect_eq "$T" "part 2 state mode" "$(jq -r .mode "$STATE_FILE")" "saver" +expect_eq "$T" "part 2 profile" "$(<"$S/pp")" "power-saver" +pass "$T" + +# 11. off keeps the state when a restore cannot run +reset_world +T="off keeps the state when a restore cannot run" +run_mode focus +expect_eq "$T" "focus exit code" "$RC" "0" +touch "$S/shell-down" +run_mode off +expect_eq "$T" "first off exit code" "$RC" "1" +[[ -f $STATE_FILE ]] || fail "$T" "state file missing after failed off" +expect_eq "$T" "kept state dnd" "$(jq -c .settings.dnd "$STATE_FILE")" '{"prior":"off","applied":"on"}' +rm -f "$S/shell-down" +run_mode off +expect_eq "$T" "second off exit code" "$RC" "0" +expect_eq "$T" "dnd after second off" "$(<"$S/dnd")" "off" +[[ ! -e $STATE_FILE ]] || fail "$T" "state file still exists after second off" +pass "$T" + +# 12. a failed write is reported and stays recoverable +reset_world +T="a failed write is reported and stays recoverable" +touch "$S/setdnd-fail" +run_mode focus +expect_eq "$T" "focus exit code" "$RC" "1" +expect_eq "$T" "state mode" "$(jq -r .mode "$STATE_FILE")" "focus" +expect_eq "$T" "dnd after failed write" "$(<"$S/dnd")" "off" +rm -f "$S/setdnd-fail" +run_mode off +expect_eq "$T" "off exit code" "$RC" "0" +expect_eq "$T" "dnd after off" "$(<"$S/dnd")" "off" +[[ ! -e $STATE_FILE ]] || fail "$T" "state file still exists after off" +pass "$T" + +# 13. an interrupted apply is undone by off +reset_world +T="an interrupted apply is undone by off" +write_user_preset cut $'bar=hidden\ndnd=on\n' +touch "$S/kill-on-setdnd" +if setsid -w "$ROOT/bin/omarchy-mode" cut >/dev/null 2>&1; then + RC=0 +else + RC=$? +fi +(( RC != 0 )) || fail "$T" "interrupted apply exited 0" +[[ -f $BAR_FLAG ]] || fail "$T" "bar flag missing after interrupted apply" +[[ -f $STATE_FILE ]] || fail "$T" "state file missing after interrupted apply" +expect_eq "$T" "state mode" "$(jq -r .mode "$STATE_FILE")" "cut" +rm -f "$S/kill-on-setdnd" +run_mode off +expect_eq "$T" "off exit code" "$RC" "0" +[[ ! -e $BAR_FLAG ]] || fail "$T" "bar flag still exists after off" +expect_eq "$T" "dnd after off" "$(<"$S/dnd")" "off" +[[ ! -e $STATE_FILE ]] || fail "$T" "state file still exists after off" +pass "$T" + +# 14. invalid presets are refused before anything changes +reset_world +T="invalid presets are refused before anything changes" +check_bad_preset() { + local label="$1" content="$2" extra_substr="${3:-}" + local bad_path="$USER_MODES_DIR/bad.conf" + write_user_preset bad "$content" + run_mode bad + expect_eq "$T" "$label exit code" "$RC" "2" + [[ $ERR == *"$bad_path"* ]] || fail "$T" "$label stderr missing path: $ERR" + if [[ -n $extra_substr && $ERR != *"$extra_substr"* ]]; then + fail "$T" "$label stderr missing <$extra_substr>: $ERR" + fi + expect_eq "$T" "$label dnd" "$(<"$S/dnd")" "off" + [[ ! -e $STATE_FILE ]] || fail "$T" "$label created state file" +} + +check_bad_preset "unknown setting" $'# x\nvolume=low\n' "bad.conf:2:" +check_bad_preset "bad value" $'dnd=maybe\n' +check_bad_preset "duplicate key" $'dnd=on\ndnd=on\n' "bad.conf:2:" +check_bad_preset "bad value before duplicate" $'dnd=on\ndnd=maybe\n' "maybe is not a value for dnd" +check_bad_preset "empty file" "" +check_bad_preset "only comment" $'# nothing\n' +check_bad_preset "leading space" $' dnd=on\n' +check_bad_preset "spaces around equals" $'dnd = on\n' + +write_user_preset ok $'# ok\n \ndnd=on\n' +run_mode ok +expect_eq "$T" "ok exit code" "$RC" "0" +expect_eq "$T" "ok dnd" "$(<"$S/dnd")" "on" +pass "$T" + +# 15. a user preset overrides a built-in one +reset_world +T="a user preset overrides a built-in one" +write_user_preset focus $'# Mine\nbar=hidden\n' +run_mode list +expect_eq "$T" "list exit code" "$RC" "0" +printf '%s\n' "$OUT" | grep -Fxq "$(printf 'focus\tMine')" || + fail "$T" "list missing user focus summary: $OUT" +focus_lines=$(printf '%s\n' "$OUT" | grep -c "^$(printf 'focus\t')") +expect_eq "$T" "focus line count" "$focus_lines" "1" +run_mode focus +expect_eq "$T" "focus exit code" "$RC" "0" +[[ -f $BAR_FLAG ]] || fail "$T" "bar flag missing after user focus" +expect_eq "$T" "dnd unchanged" "$(<"$S/dnd")" "off" +pass "$T" + +# 16. unknown and reserved names are refused +reset_world +T="unknown and reserved names are refused" +mkdir -p "$HOME/.config/omarchy" "$USER_MODES_DIR" +printf 'bar=hidden\n' >"$HOME/.config/omarchy/evil.conf" +write_user_preset off $'bar=hidden\n' +write_user_preset status $'bar=hidden\n' +write_user_preset list $'bar=hidden\n' + +check_refused() { + local label="$1" + shift + run_mode "$@" + expect_eq "$T" "$label exit code" "$RC" "2" + [[ ! -e $BAR_FLAG ]] || fail "$T" "$label set bar flag" +} + +check_refused "no args" +check_refused "nosuch" nosuch +check_refused "path traversal" ../evil +check_refused "uppercase" Focus +check_refused "empty string" "" +check_refused "single space" " " +check_refused "status extra" status extra +check_refused "focus extra" focus extra +check_refused "list extra" list extra +check_refused "off extra" off extra +check_refused "bare --json" --json + +run_mode list +expect_eq "$T" "list exit code" "$RC" "0" +if printf '%s\n' "$OUT" | grep -Eq "^(off|status|list)$(printf '\t')"; then + fail "$T" "list included reserved name: $OUT" +fi +run_mode off +expect_eq "$T" "off exit code" "$RC" "0" +expect_eq "$T" "off stdout" "$OUT" "none" +[[ ! -e $BAR_FLAG ]] || fail "$T" "off set bar flag" +pass "$T" + +# 17. status reports none and the active mode +reset_world +T="status reports none and the active mode" +run_mode status +expect_eq "$T" "status none exit code" "$RC" "0" +expect_eq "$T" "status none stdout" "$OUT" "none" +run_mode status --json +expect_eq "$T" "status json none exit code" "$RC" "0" +expect_eq "$T" "status json none stdout" "$OUT" '{"mode":null}' +run_mode +expect_eq "$T" "no args exit code" "$RC" "2" + +run_mode focus +expect_eq "$T" "focus exit code" "$RC" "0" +run_mode status +expect_eq "$T" "status active exit code" "$RC" "0" +mapfile -t status_lines <<<"$OUT" +expect_eq "$T" "status line count" "${#status_lines[@]}" "2" +[[ ${status_lines[0]} =~ ^focus\ since\ [0-9]{4}-[0-9]{2}-[0-9]{2}T ]] || + fail "$T" "status line 1 mismatch: ${status_lines[0]}" +expect_eq "$T" "status line 2" "${status_lines[1]}" "dnd=on (was off)" + +run_mode status --json +expect_eq "$T" "status json active exit code" "$RC" "0" +json_no_since=$(jq -c 'del(.since)' <<<"$OUT") +expect_eq "$T" "status json without since" "$json_no_since" '{"mode":"focus","settings":{"dnd":{"prior":"off","applied":"on"}}}' +pass "$T" + +# 18. the mode-set hook gets the mode name and off +reset_world +T="the mode-set hook gets the mode name and off" +mkdir -p "$HOME/.config/omarchy/hooks" +cat >"$HOME/.config/omarchy/hooks/mode-set" <<'EOF' +echo "$1" >>"$HOME/hook.log" +EOF +run_mode focus +expect_eq "$T" "focus exit code" "$RC" "0" +run_mode off +expect_eq "$T" "first off exit code" "$RC" "0" +run_mode off +expect_eq "$T" "second off exit code" "$RC" "0" +run_mode nosuch +expect_eq "$T" "nosuch exit code" "$RC" "2" +expect_eq "$T" "hook.log" "$(<"$HOME/hook.log")" $'focus\noff' +pass "$T" + +# 19. a corrupt state file is reported, not trusted +reset_world +T="a corrupt state file is reported, not trusted" +check_corrupt_state() { + local label="$1" content="$2" + mkdir -p "$(dirname "$STATE_FILE")" + printf '%s' "$content" >"$STATE_FILE" + local cmd + for cmd in status focus off; do + run_mode "$cmd" + expect_eq "$T" "$label $cmd exit code" "$RC" "1" + expect_eq "$T" "$label $cmd state unchanged" "$(<"$STATE_FILE")" "$content" + expect_eq "$T" "$label $cmd dnd unchanged" "$(<"$S/dnd")" "off" + done +} + +check_corrupt_state "not json" "not json" +check_corrupt_state "missing settings and since" '{"mode":"x"}' +check_corrupt_state "missing since" '{"mode":"x","settings":{}}' +check_corrupt_state "empty file" "" +pass "$T" + +# 20. off drops a state entry for an unknown setting +reset_world +T="off drops a state entry for an unknown setting" +mkdir -p "$(dirname "$STATE_FILE")" +printf '%s\n' '{"mode":"x","since":"2026-01-01T00:00:00+00:00","settings":{"volume":{"prior":"a","applied":"b"}}}' >"$STATE_FILE" +run_mode off +expect_eq "$T" "off exit code" "$RC" "0" +[[ $ERR == *"dropping unknown setting volume"* ]] || + fail "$T" "stderr missing unknown setting warning: $ERR" +[[ ! -e $STATE_FILE ]] || fail "$T" "state file still exists after off" +pass "$T" + +# 21. a switch carries an old setting it could not restore +reset_world +T="a switch carries an old setting it could not restore" +run_mode focus +expect_eq "$T" "focus exit code" "$RC" "0" +touch "$S/shell-down" +run_mode travel +expect_eq "$T" "travel exit code" "$RC" "1" +expect_eq "$T" "state mode" "$(jq -r .mode "$STATE_FILE")" "travel" +expect_eq "$T" "state keys" "$(jq -c '.settings | keys_unsorted' "$STATE_FILE")" '["dnd","power-profile","animations"]' +expect_eq "$T" "state dnd" "$(jq -c .settings.dnd "$STATE_FILE")" '{"prior":"off","applied":"on"}' +rm -f "$S/shell-down" +run_mode off +expect_eq "$T" "off exit code" "$RC" "0" +expect_eq "$T" "dnd after off" "$(<"$S/dnd")" "off" +expect_eq "$T" "profile after off" "$(<"$S/pp")" "balanced" +[[ ! -e $NO_ANIM_FLAG ]] || fail "$T" "no-animations flag still exists after off" +[[ ! -e $STATE_FILE ]] || fail "$T" "state file still exists after off" +pass "$T" + +# 22. a switch to a different value of a shared setting keeps the first baseline +reset_world +T="a switch to a different value of a shared setting keeps the first baseline" +write_user_preset a $'power-profile=power-saver\n' +write_user_preset b $'power-profile=performance\n' +run_mode a +expect_eq "$T" "a exit code" "$RC" "0" +run_mode b +expect_eq "$T" "b exit code" "$RC" "0" +expect_eq "$T" "profile after b" "$(<"$S/pp")" "performance" +expect_eq "$T" "state power-profile" "$(jq -c '.settings["power-profile"]' "$STATE_FILE")" '{"prior":"balanced","applied":"performance"}' +run_mode off +expect_eq "$T" "off exit code" "$RC" "0" +expect_eq "$T" "profile after off" "$(<"$S/pp")" "balanced" +[[ ! -e $STATE_FILE ]] || fail "$T" "state file still exists after off" +pass "$T" + +# 23. a state that cannot be written stops the mode before it changes anything +reset_world +T="a state that cannot be written stops the mode before it changes anything" +mkdir -p "$HOME/.local/state/omarchy/mode.json.tmp" +run_mode focus +expect_eq "$T" "focus exit code" "$RC" "1" +[[ $ERR == *"could not write"* ]] || fail "$T" "stderr missing 'could not write': $ERR" +expect_eq "$T" "dnd unchanged" "$(<"$S/dnd")" "off" +[[ ! -e $S/setdnd.log ]] || fail "$T" "setdnd.log exists after failed state write" +[[ ! -e $STATE_FILE ]] || fail "$T" "state file exists after failed state write" +pass "$T" + +# 24. off keeps an entry whose restore write failed +reset_world +T="off keeps an entry whose restore write failed" +run_mode focus +expect_eq "$T" "focus exit code" "$RC" "0" +touch "$S/setdnd-fail" +run_mode off +expect_eq "$T" "first off exit code" "$RC" "1" +[[ $ERR == *"could not restore dnd"* ]] || fail "$T" "stderr missing 'could not restore dnd': $ERR" +expect_eq "$T" "state dnd" "$(jq -c .settings.dnd "$STATE_FILE")" '{"prior":"off","applied":"on"}' +expect_eq "$T" "dnd still on" "$(<"$S/dnd")" "on" +rm -f "$S/setdnd-fail" +run_mode off +expect_eq "$T" "second off exit code" "$RC" "0" +expect_eq "$T" "dnd after second off" "$(<"$S/dnd")" "off" +[[ ! -e $STATE_FILE ]] || fail "$T" "state file still exists after second off" +pass "$T" + +# 25. a switch does not take over a setting the old mode could not release +reset_world +T="a switch does not take over a setting the old mode could not release" +write_user_preset quiet $'dnd=off\n' +run_mode focus +expect_eq "$T" "focus exit code" "$RC" "0" +touch "$S/setdnd-fail" +run_mode quiet +expect_eq "$T" "quiet exit code" "$RC" "1" +[[ $ERR == *"dnd is still held by the previous mode, skipping"* ]] || + fail "$T" "stderr missing held message: $ERR" +expect_eq "$T" "state mode" "$(jq -r .mode "$STATE_FILE")" "quiet" +expect_eq "$T" "state settings" "$(jq -c .settings "$STATE_FILE")" '{"dnd":{"prior":"off","applied":"on"}}' +expect_eq "$T" "dnd still on" "$(<"$S/dnd")" "on" +expect_eq "$T" "setdnd.log" "$(<"$S/setdnd.log")" $'on\noff' +rm -f "$S/setdnd-fail" +run_mode off +expect_eq "$T" "off exit code" "$RC" "0" +expect_eq "$T" "dnd after off" "$(<"$S/dnd")" "off" +[[ ! -e $STATE_FILE ]] || fail "$T" "state file still exists after off" +pass "$T" From 761fe966e52735cdffadb9d8592ec900bf74b2a3 Mon Sep 17 00:00:00 2001 From: Joshua Pruitt <50942+photuris@users.noreply.github.com> Date: Tue, 1 Dec 2026 00:58:53 -0600 Subject: [PATCH 08/15] Harden omarchy mode state reading and stderr handling --- bin/omarchy-mode | 67 +++++++++++++++++++++++---------------- test/shell.d/mode-test.sh | 34 ++++++++++++++++++++ 2 files changed, 74 insertions(+), 27 deletions(-) diff --git a/bin/omarchy-mode b/bin/omarchy-mode index bce344dc4a97..45122c353f8c 100755 --- a/bin/omarchy-mode +++ b/bin/omarchy-mode @@ -62,7 +62,7 @@ is_allowed_value() { local key="$1" value="$2" allowed while IFS= read -r allowed; do - if [[ $allowed == "$value" ]]; then + if [[ $value == $allowed ]]; then return 0 fi done < <(setting_allowed_values "$key") @@ -125,7 +125,7 @@ setting_write() { case $key in dnd) - if output=$(omarchy-shell notifications setDnd "$value" 2>/dev/null) && [[ $output == "$value" ]]; then + if output=$(omarchy-shell notifications setDnd "$value" 2>/dev/null) && [[ $output == $value ]]; then omarchy-shell -q omarchy.indicators refresh >/dev/null 2>&1 return 0 else @@ -169,7 +169,7 @@ acquire_lock() { fi if ! flock "$LOCK_FD" 2>/dev/null; then - exec {LOCK_FD}>&- 2>/dev/null + { exec {LOCK_FD}>&-; } 2>/dev/null LOCK_FD="" warn "could not lock $STATE_DIR" return 1 @@ -180,7 +180,7 @@ acquire_lock() { release_lock() { if [[ -n $LOCK_FD ]]; then - exec {LOCK_FD}>&- 2>/dev/null + { exec {LOCK_FD}>&-; } 2>/dev/null LOCK_FD="" fi } @@ -188,17 +188,20 @@ release_lock() { validate_state_raw() { local raw="$1" - # Slurping catches empty files and trailing JSON values as well as bad types. + # Slurping catches empty files and trailing JSON values; rejecting control + # characters lets parse_state_raw split fields on NUL safely. jq -se ' + def no_ctrl: type == "string" and (explode | all(. >= 32)); length == 1 and (.[0] | type == "object") - and (.[0].mode | type == "string") - and (.[0].since | type == "string") + and (.[0].mode | no_ctrl) + and (.[0].since | no_ctrl) and (.[0].settings | type == "object") and (.[0].settings | to_entries | all( - (.value | type == "object") - and (.value.prior | type == "string") - and (.value.applied | type == "string") + (.key | no_ctrl) + and (.value | type == "object") + and (.value.prior | no_ctrl) + and (.value.applied | no_ctrl) )) ' <<<"$raw" >/dev/null 2>&1 } @@ -312,7 +315,7 @@ resolve_preset() { parse_preset() { local path="$1" - local line="" key="" value="" + local line="" key="" value="" err="" fd="" local line_num=0 local tab=$'\t' local kv_re='^([abcdefghijklmnopqrstuvwxyz][abcdefghijklmnopqrstuvwxyz-]*)=([abcdefghijklmnopqrstuvwxyz][abcdefghijklmnopqrstuvwxyz-]*)$' @@ -320,12 +323,17 @@ parse_preset() { PRESET_KEYS=() PRESET_VALUES=() + if ! { exec {fd}<"$path"; } 2>/dev/null; then + warn "$path: cannot be read" + return 2 + fi + while IFS= read -r line || [[ -n $line ]]; do line_num=$(( line_num + 1 )) if [[ $line == *$'\r'* ]]; then - warn "$path:$line_num: not a key=value line" - return 2 + err="$path:$line_num: not a key=value line" + break elif [[ -z ${line//[ $tab]/} ]]; then continue elif [[ $line == "#"* ]]; then @@ -334,25 +342,30 @@ parse_preset() { key=${BASH_REMATCH[1]} value=${BASH_REMATCH[2]} if ! is_known_setting "$key"; then - warn "$path:$line_num: unknown setting $key" - return 2 + err="$path:$line_num: unknown setting $key" + break elif ! is_allowed_value "$key" "$value"; then - warn "$path:$line_num: $value is not a value for $key" - return 2 + err="$path:$line_num: $value is not a value for $key" + break elif [[ -n ${PRESET_VALUES[$key]:-} ]]; then - warn "$path:$line_num: $key is set twice" - return 2 + err="$path:$line_num: $key is set twice" + break else PRESET_KEYS+=("$key") PRESET_VALUES["$key"]="$value" fi else - warn "$path:$line_num: not a key=value line" - return 2 + err="$path:$line_num: not a key=value line" + break fi - done <"$path" + done <&"$fd" - if (( ${#PRESET_KEYS[@]} == 0 )); then + { exec {fd}<&-; } 2>/dev/null + + if [[ -n $err ]]; then + warn "$err" + return 2 + elif (( ${#PRESET_KEYS[@]} == 0 )); then warn "$path: no settings" return 2 else @@ -389,11 +402,11 @@ restore_entry() { return 1 fi - if [[ $current != "$applied" ]]; then + if [[ $current != $applied ]]; then return 0 fi - if [[ $current == "$prior" ]]; then + if [[ $current == $prior ]]; then return 0 fi @@ -567,7 +580,7 @@ cmd_apply() { preset_class["$key"]="skip" elif [[ -n ${OLD_INDEX[$key]:-} ]]; then old_idx=${OLD_INDEX[$key]} - if [[ $current == "${STATE_APPLIEDS[$old_idx]}" && ${PRESET_VALUES[$key]} == "${STATE_APPLIEDS[$old_idx]}" ]]; then + if [[ ${STATE_APPLIEDS[$old_idx]} == $current && ${STATE_APPLIEDS[$old_idx]} == ${PRESET_VALUES[$key]} ]]; then preset_class["$key"]="carried" carried_keys["$key"]=1 carried_count=$(( carried_count + 1 )) @@ -629,7 +642,7 @@ cmd_apply() { fi for (( i = 0; i < ${#fresh_keys[@]}; i++ )); do - if [[ ${fresh_priors[$i]} != "${fresh_applieds[$i]}" ]]; then + if [[ ${fresh_priors[$i]} != ${fresh_applieds[$i]} ]]; then if ! setting_write "${fresh_keys[$i]}" "${fresh_applieds[$i]}"; then warn "could not set ${fresh_keys[$i]}" had_failure=1 diff --git a/test/shell.d/mode-test.sh b/test/shell.d/mode-test.sh index adb5642ec45e..07267b2e476f 100755 --- a/test/shell.d/mode-test.sh +++ b/test/shell.d/mode-test.sh @@ -368,6 +368,24 @@ check_bad_preset "only comment" $'# nothing\n' check_bad_preset "leading space" $' dnd=on\n' check_bad_preset "spaces around equals" $'dnd = on\n' +if (( EUID != 0 )); then + write_user_preset bad $'dnd=on\n' + chmod 000 "$USER_MODES_DIR/bad.conf" + run_mode bad + chmod 644 "$USER_MODES_DIR/bad.conf" + expect_eq "$T" "unreadable preset exit code" "$RC" "2" + mapfile -t err_lines <<<"$ERR" + expect_eq "$T" "unreadable preset stderr line count" "${#err_lines[@]}" "1" + [[ ${err_lines[0]} == "omarchy-mode: "* ]] || + fail "$T" "unreadable preset stderr missing prefix: $ERR" + [[ ${err_lines[0]} == *"cannot be read"* ]] || + fail "$T" "unreadable preset stderr missing 'cannot be read': $ERR" + [[ ${err_lines[0]} == *"$USER_MODES_DIR/bad.conf"* ]] || + fail "$T" "unreadable preset stderr missing path: $ERR" + expect_eq "$T" "unreadable preset dnd" "$(<"$S/dnd")" "off" + [[ ! -e $STATE_FILE ]] || fail "$T" "unreadable preset created state file" +fi + write_user_preset ok $'# ok\n \ndnd=on\n' run_mode ok expect_eq "$T" "ok exit code" "$RC" "0" @@ -496,6 +514,7 @@ check_corrupt_state "not json" "not json" check_corrupt_state "missing settings and since" '{"mode":"x"}' check_corrupt_state "missing since" '{"mode":"x","settings":{}}' check_corrupt_state "empty file" "" +check_corrupt_state "control char in state" '{"mode":"present","since":"2026-01-01T00:00:00+00:00","settings":{"bar":{"prior":"shown\u0000bad","applied":"hidden"},"dnd":{"prior":"off","applied":"on"}}}' pass "$T" # 20. off drops a state entry for an unknown setting @@ -557,6 +576,21 @@ expect_eq "$T" "focus exit code" "$RC" "1" expect_eq "$T" "dnd unchanged" "$(<"$S/dnd")" "off" [[ ! -e $S/setdnd.log ]] || fail "$T" "setdnd.log exists after failed state write" [[ ! -e $STATE_FILE ]] || fail "$T" "state file exists after failed state write" + +reset_world +mkdir -p "$tmp_dir/bad-flock" +cat >"$tmp_dir/bad-flock/flock" <<'EOF' +#!/bin/bash +exit 1 +EOF +chmod +x "$tmp_dir/bad-flock/flock" +saved_path=$PATH +export PATH="$tmp_dir/bad-flock:$PATH" +run_mode focus +export PATH="$saved_path" +expect_eq "$T" "flock failure exit code" "$RC" "1" +expect_eq "$T" "flock failure stderr" "$ERR" "omarchy-mode: could not lock $HOME/.local/state/omarchy" +expect_eq "$T" "flock failure dnd" "$(<"$S/dnd")" "off" pass "$T" # 24. off keeps an entry whose restore write failed From 35019c2b457f1fc8f3114d464fe632095f26a305 Mon Sep 17 00:00:00 2001 From: Joshua Pruitt <50942+photuris@users.noreply.github.com> Date: Tue, 1 Dec 2026 01:05:51 -0600 Subject: [PATCH 09/15] Document omarchy mode and its mode-set hook --- default/agents/skills/omarchy/hooks.md | 1 + manual/13-toggles-idle-screensaver.md | 34 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/default/agents/skills/omarchy/hooks.md b/default/agents/skills/omarchy/hooks.md index 85eea06a0391..d36dbf742dcb 100644 --- a/default/agents/skills/omarchy/hooks.md +++ b/default/agents/skills/omarchy/hooks.md @@ -13,6 +13,7 @@ file first, if one exists. ~/.config/omarchy/hooks/ ├── battery-low.d/ # Low battery (percentage in $1) ├── font-set.d/ # After font change (font name in $1) +├── mode-set.d/ # After `omarchy mode` applies a mode or turns it off (mode name or `off` in $1) ├── post-boot.d/ # After the desktop starts ├── post-update.d/ # Late in `omarchy update`, after packages, migrations and service restarts ├── pre-refresh-pacman.d/ # After `omarchy refresh pacman` re-syncs the package config, before it updates packages diff --git a/manual/13-toggles-idle-screensaver.md b/manual/13-toggles-idle-screensaver.md index 71da2a1def2f..61caf35a7087 100644 --- a/manual/13-toggles-idle-screensaver.md +++ b/manual/13-toggles-idle-screensaver.md @@ -38,6 +38,40 @@ omarchy-toggle-enabled screensaver-off && echo "screensaver is off" The flags are named for the off state — `screensaver-off`, `suspend-off`, `bar-off` — so their presence means the feature is disabled. +### Modes + +A mode flips several of those switches in one command and puts them back when you're done. Three come built in: + +| Mode | What it sets | +| ---- | ------------ | +| `focus` | Notifications silenced | +| `present` | Notifications silenced, stay awake, menu bar hidden | +| `travel` | Power-saver profile, animations off | + +```bash +omarchy mode present +omarchy mode off +``` + +`omarchy mode off` returns each setting to what it was before the mode, so do not disturb stays on if it was already on. A setting you changed yourself while the mode was active is left as you set it. Switching straight from one mode to another keeps the original values, and `omarchy mode status` shows the active mode and what it holds. A setting the machine doesn't have, such as power profiles in most virtual machines, is skipped. + +Your own modes go in `~/.config/omarchy/modes/.conf`, one `setting=value` per line, and show up in `omarchy mode list`. A file named after a built-in mode replaces it. + +``` +# Recording a screencast +dnd=on +stay-awake=on +animations=off +``` + +| Setting | Values | +| ------- | ------ | +| `dnd` | `on`, `off` | +| `stay-awake` | `on`, `off` | +| `bar` | `shown`, `hidden` | +| `animations` | `on`, `off` | +| `power-profile` | `power-saver`, `balanced`, `performance` | + ### Indicators in the bar When a mode is on, you get a small glyph in the middle of the top bar next to the clock. That's the indicators widget, and it carries screen recording, pending reminders, night light, do not disturb, and stay awake, plus warnings while passwordless sudo is on or someone is connected to a remote session. From 40bb1d117ad95491d8fd318eeb61d74e9e1bd8ee Mon Sep 17 00:00:00 2001 From: Joshua Pruitt <50942+photuris@users.noreply.github.com> Date: Tue, 1 Dec 2026 01:25:59 -0600 Subject: [PATCH 10/15] Verify omarchy mode writes and restore automatic animations exactly --- bin/omarchy-mode | 65 +++++++++++++++++++++-- test/shell.d/mode-test.sh | 106 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 166 insertions(+), 5 deletions(-) diff --git a/bin/omarchy-mode b/bin/omarchy-mode index 45122c353f8c..6722a06b8fd8 100755 --- a/bin/omarchy-mode +++ b/bin/omarchy-mode @@ -54,6 +54,13 @@ setting_allowed_values() { esac } +setting_state_values() { + case $1 in + animations) printf '%s\n' on off auto ;; + *) setting_allowed_values "$1" ;; + esac +} + is_known_setting() { setting_allowed_values "$1" >/dev/null } @@ -70,6 +77,18 @@ is_allowed_value() { return 1 } +is_state_value() { + local key="$1" value="$2" allowed + + while IFS= read -r allowed; do + if [[ $value == $allowed ]]; then + return 0 + fi + done < <(setting_state_values "$key") + + return 1 +} + setting_read() { local key="$1" value="" @@ -100,8 +119,10 @@ setting_read() { return 0 ;; animations) - if omarchy-hyprland-animations-off >/dev/null 2>&1; then + if omarchy-hyprland-toggle-enabled no-animations >/dev/null 2>&1; then printf 'off\n' + elif omarchy-hyprland-animations-off >/dev/null 2>&1; then + printf 'auto\n' else printf 'on\n' fi @@ -147,7 +168,14 @@ setting_write() { fi ;; animations) - omarchy-toggle-animations "$value" >/dev/null 2>&1 + if [[ $value == "auto" ]]; then + omarchy-toggle animations-without-gpu off >/dev/null 2>&1 && + omarchy-hyprland-toggle no-animations off >/dev/null 2>&1 + elif [[ $value == "on" || $value == "off" ]]; then + omarchy-toggle-animations "$value" >/dev/null 2>&1 + else + return 1 + fi ;; power-profile) omarchy-powerprofiles-set autodetect "$value" >/dev/null 2>&1 @@ -156,6 +184,33 @@ setting_write() { esac } +setting_apply() { + local key="${1:-}" value="${2:-}" actual="" + + if [[ -z $value ]]; then + return 1 + fi + + if ! setting_write "$key" "$value"; then + return 1 + fi + + if [[ $key == "animations" && $value == "auto" ]]; then + # In a GPU session both flags are absent and setting_read returns on, + # so verify neither override flag remains instead of re-reading. + if ! omarchy-toggle-enabled animations-without-gpu >/dev/null 2>&1 && + ! omarchy-hyprland-toggle-enabled no-animations >/dev/null 2>&1; then + return 0 + else + return 1 + fi + elif actual=$(setting_read "$key") && [[ $actual == $value ]]; then + return 0 + else + return 1 + fi +} + acquire_lock() { if ! mkdir -p "$STATE_DIR" 2>/dev/null; then warn "could not lock $STATE_DIR" @@ -392,7 +447,7 @@ restore_entry() { return 0 fi - if ! is_allowed_value "$key" "$prior" || ! is_allowed_value "$key" "$applied"; then + if ! is_state_value "$key" "$prior" || ! is_state_value "$key" "$applied"; then warn "dropping invalid entry $key" return 0 fi @@ -410,7 +465,7 @@ restore_entry() { return 0 fi - if setting_write "$key" "$prior"; then + if setting_apply "$key" "$prior"; then return 0 else warn "could not restore $key" @@ -643,7 +698,7 @@ cmd_apply() { for (( i = 0; i < ${#fresh_keys[@]}; i++ )); do if [[ ${fresh_priors[$i]} != ${fresh_applieds[$i]} ]]; then - if ! setting_write "${fresh_keys[$i]}" "${fresh_applieds[$i]}"; then + if ! setting_apply "${fresh_keys[$i]}" "${fresh_applieds[$i]}"; then warn "could not set ${fresh_keys[$i]}" had_failure=1 fi diff --git a/test/shell.d/mode-test.sh b/test/shell.d/mode-test.sh index 07267b2e476f..67525d1a3cb2 100755 --- a/test/shell.d/mode-test.sh +++ b/test/shell.d/mode-test.sh @@ -92,6 +92,7 @@ export PATH="$tmp_dir/stubs:$ROOT/bin:$PATH" STATE_FILE="$HOME/.local/state/omarchy/mode.json" NO_ANIM_FLAG="$HOME/.local/state/omarchy/toggles/hypr/no-animations.lua" +WITHOUT_GPU_FLAG="$HOME/.local/state/omarchy/toggles/animations-without-gpu" BAR_FLAG="$HOME/.local/state/omarchy/toggles/bar-off" STAY_AWAKE_FILE="$HOME/.local/state/omarchy/indicators/stay-awake" USER_MODES_DIR="$HOME/.config/omarchy/modes" @@ -112,7 +113,19 @@ run_mode() { ERR=$(<"$err_file") } +cpu_session() { + export HYPRLAND_INSTANCE_SIGNATURE=modetest + mkdir -p "$XDG_RUNTIME_DIR/hypr/modetest" + : >"$XDG_RUNTIME_DIR/hypr/modetest/omarchy-software-rendering" +} + +gpu_session() { + unset HYPRLAND_INSTANCE_SIGNATURE + rm -rf "$XDG_RUNTIME_DIR/hypr" +} + reset_world() { + gpu_session rm -rf "$HOME/.local/state/omarchy" "$HOME/.config/omarchy" "$HOME/hook.log" rm -rf "$S"/* "$S"/.[!.]* "$S"/..?* 2>/dev/null || true printf 'off\n' >"$S/dnd" @@ -632,3 +645,96 @@ expect_eq "$T" "off exit code" "$RC" "0" expect_eq "$T" "dnd after off" "$(<"$S/dnd")" "off" [[ ! -e $STATE_FILE ]] || fail "$T" "state file still exists after off" pass "$T" + +# 26. a write that changed nothing keeps its entry +reset_world +T="a write that changed nothing keeps its entry" +if (( EUID == 0 )); then + skip "$T" +else + run_mode present + expect_eq "$T" "present exit code" "$RC" "0" + expect_eq "$T" "dnd after present" "$(<"$S/dnd")" "on" + [[ -f $STAY_AWAKE_FILE ]] || fail "$T" "stay-awake file missing after present" + [[ -f $BAR_FLAG ]] || fail "$T" "bar flag missing after present" + chmod 555 "$HOME/.local/state/omarchy/indicators" + run_mode off + chmod 755 "$HOME/.local/state/omarchy/indicators" + expect_eq "$T" "first off exit code" "$RC" "1" + [[ $ERR == *"could not restore stay-awake"* ]] || + fail "$T" "stderr missing 'could not restore stay-awake': $ERR" + expect_eq "$T" "state settings" "$(jq -c .settings "$STATE_FILE")" '{"stay-awake":{"prior":"off","applied":"on"}}' + expect_eq "$T" "dnd after first off" "$(<"$S/dnd")" "off" + [[ ! -e $BAR_FLAG ]] || fail "$T" "bar flag still exists after first off" + [[ -f $STAY_AWAKE_FILE ]] || fail "$T" "stay-awake file missing after first off" + run_mode off + expect_eq "$T" "second off exit code" "$RC" "0" + [[ ! -e $STAY_AWAKE_FILE ]] || fail "$T" "stay-awake file still exists after second off" + [[ ! -e $STATE_FILE ]] || fail "$T" "state file still exists after second off" + pass "$T" +fi + +# 27. a set that changed nothing is reported +reset_world +T="a set that changed nothing is reported" +if (( EUID == 0 )); then + skip "$T" +else + mkdir -p "$HOME/.local/state/omarchy/indicators" + chmod 555 "$HOME/.local/state/omarchy/indicators" + write_user_preset awake $'stay-awake=on\n' + run_mode awake + chmod 755 "$HOME/.local/state/omarchy/indicators" + expect_eq "$T" "awake exit code" "$RC" "1" + [[ $ERR == *"could not set stay-awake"* ]] || + fail "$T" "stderr missing 'could not set stay-awake': $ERR" + expect_eq "$T" "state mode" "$(jq -r .mode "$STATE_FILE")" "awake" + [[ ! -e $STAY_AWAKE_FILE ]] || fail "$T" "stay-awake file exists after failed set" + run_mode off + expect_eq "$T" "off exit code" "$RC" "0" + [[ ! -e $STATE_FILE ]] || fail "$T" "state file still exists after off" + pass "$T" +fi + +# 28. leaving a mode restores automatic animations without the permanent toggle +reset_world +T="leaving a mode restores automatic animations without the permanent toggle" +cpu_session +write_user_preset lively $'animations=on\n' +run_mode lively +expect_eq "$T" "lively exit code" "$RC" "0" +expect_eq "$T" "state animations" "$(jq -c .settings.animations "$STATE_FILE")" '{"prior":"auto","applied":"on"}' +[[ -f $WITHOUT_GPU_FLAG ]] || fail "$T" "without-gpu flag missing after lively" +[[ ! -e $NO_ANIM_FLAG ]] || fail "$T" "no-animations flag exists after lively" +run_mode off +expect_eq "$T" "off exit code" "$RC" "0" +[[ ! -e $WITHOUT_GPU_FLAG ]] || fail "$T" "without-gpu flag still exists after off" +[[ ! -e $NO_ANIM_FLAG ]] || fail "$T" "no-animations flag exists after off" +[[ ! -e $STATE_FILE ]] || fail "$T" "state file still exists after off" +gpu_session +pass "$T" + +# 29. turning animations off from the automatic state is undone exactly +reset_world +T="turning animations off from the automatic state is undone exactly" +cpu_session +run_mode travel +expect_eq "$T" "travel exit code" "$RC" "0" +expect_eq "$T" "state animations" "$(jq -c .settings.animations "$STATE_FILE")" '{"prior":"auto","applied":"off"}' +[[ -f $NO_ANIM_FLAG ]] || fail "$T" "no-animations flag missing after travel" +run_mode status +expect_eq "$T" "status exit code" "$RC" "0" +printf '%s\n' "$OUT" | grep -Fxq "animations=off (was auto)" || + fail "$T" "status missing 'animations=off (was auto)': $OUT" +run_mode off +expect_eq "$T" "off exit code" "$RC" "0" +[[ ! -e $WITHOUT_GPU_FLAG ]] || fail "$T" "without-gpu flag still exists after off" +[[ ! -e $NO_ANIM_FLAG ]] || fail "$T" "no-animations flag still exists after off" +[[ ! -e $STATE_FILE ]] || fail "$T" "state file still exists after off" +write_user_preset bad $'animations=auto\n' +run_mode bad +expect_eq "$T" "bad exit code" "$RC" "2" +[[ $ERR == *"auto is not a value for animations"* ]] || + fail "$T" "stderr missing 'auto is not a value for animations': $ERR" +gpu_session +pass "$T" From 50b242d4e251a2a6800fd2e0da4039eefbfc2445 Mon Sep 17 00:00:00 2001 From: Joshua Pruitt <50942+photuris@users.noreply.github.com> Date: Tue, 1 Dec 2026 01:41:43 -0600 Subject: [PATCH 11/15] Restore the animation flag files exactly and finish half-done restores --- bin/omarchy-mode | 152 +++++++++++++++++++++++++++++--------- test/shell.d/mode-test.sh | 109 ++++++++++++++++++++++++--- 2 files changed, 216 insertions(+), 45 deletions(-) diff --git a/bin/omarchy-mode b/bin/omarchy-mode index 6722a06b8fd8..0fc5b3233bb3 100755 --- a/bin/omarchy-mode +++ b/bin/omarchy-mode @@ -27,6 +27,7 @@ STATE_KEYS=() STATE_PRIORS=() STATE_APPLIEDS=() declare -A OLD_INDEX=() +RESTORE_KEPT_APPLIED="" warn() { printf 'omarchy-mode: %s\n' "$1" >&2 @@ -54,9 +55,9 @@ setting_allowed_values() { esac } -setting_state_values() { +setting_snapshot_values() { case $1 in - animations) printf '%s\n' on off auto ;; + animations) printf '%s\n' default off on-without-gpu off-without-gpu ;; *) setting_allowed_values "$1" ;; esac } @@ -77,14 +78,14 @@ is_allowed_value() { return 1 } -is_state_value() { +is_snapshot_value() { local key="$1" value="$2" allowed while IFS= read -r allowed; do if [[ $value == $allowed ]]; then return 0 fi - done < <(setting_state_values "$key") + done < <(setting_snapshot_values "$key") return 1 } @@ -119,10 +120,8 @@ setting_read() { return 0 ;; animations) - if omarchy-hyprland-toggle-enabled no-animations >/dev/null 2>&1; then + if omarchy-hyprland-animations-off >/dev/null 2>&1; then printf 'off\n' - elif omarchy-hyprland-animations-off >/dev/null 2>&1; then - printf 'auto\n' else printf 'on\n' fi @@ -141,6 +140,31 @@ setting_read() { esac } +setting_snapshot() { + local key="${1:-}" has_p=0 has_w=0 + + if [[ $key == "animations" ]]; then + if omarchy-hyprland-toggle-enabled no-animations >/dev/null 2>&1; then + has_p=1 + fi + if omarchy-toggle-enabled animations-without-gpu >/dev/null 2>&1; then + has_w=1 + fi + if (( has_p && has_w )); then + printf 'off-without-gpu\n' + elif (( has_p )); then + printf 'off\n' + elif (( has_w )); then + printf 'on-without-gpu\n' + else + printf 'default\n' + fi + return 0 + else + setting_read "$key" + fi +} + setting_write() { local key="$1" value="$2" output="" @@ -168,10 +192,7 @@ setting_write() { fi ;; animations) - if [[ $value == "auto" ]]; then - omarchy-toggle animations-without-gpu off >/dev/null 2>&1 && - omarchy-hyprland-toggle no-animations off >/dev/null 2>&1 - elif [[ $value == "on" || $value == "off" ]]; then + if [[ $value == "on" || $value == "off" ]]; then omarchy-toggle-animations "$value" >/dev/null 2>&1 else return 1 @@ -184,6 +205,32 @@ setting_write() { esac } +setting_restore() { + local key="${1:-}" snapshot="${2:-}" w_action="" p_action="" rc=0 + + if [[ $key == "animations" ]]; then + case $snapshot in + default) w_action="off"; p_action="off" ;; + off) w_action="off"; p_action="on" ;; + on-without-gpu) w_action="on"; p_action="off" ;; + off-without-gpu) w_action="on"; p_action="on" ;; + *) return 1 ;; + esac + + # Update the without-gpu flag before the Hyprland toggle reloads Hyprland, + # and run the second step even when the first fails. + if ! omarchy-toggle animations-without-gpu "$w_action" >/dev/null 2>&1; then + rc=1 + fi + if ! omarchy-hyprland-toggle no-animations "$p_action" >/dev/null 2>&1; then + rc=1 + fi + return "$rc" + else + setting_write "$key" "$snapshot" + fi +} + setting_apply() { local key="${1:-}" value="${2:-}" actual="" @@ -195,16 +242,25 @@ setting_apply() { return 1 fi - if [[ $key == "animations" && $value == "auto" ]]; then - # In a GPU session both flags are absent and setting_read returns on, - # so verify neither override flag remains instead of re-reading. - if ! omarchy-toggle-enabled animations-without-gpu >/dev/null 2>&1 && - ! omarchy-hyprland-toggle-enabled no-animations >/dev/null 2>&1; then - return 0 - else - return 1 - fi - elif actual=$(setting_read "$key") && [[ $actual == $value ]]; then + if actual=$(setting_read "$key") && [[ $actual == $value ]]; then + return 0 + else + return 1 + fi +} + +setting_put_back() { + local key="${1:-}" snapshot="${2:-}" actual="" + + if [[ -z $snapshot ]]; then + return 1 + fi + + if ! setting_restore "$key" "$snapshot"; then + return 1 + fi + + if actual=$(setting_snapshot "$key") && [[ $actual == $snapshot ]]; then return 0 else return 1 @@ -437,17 +493,19 @@ preset_summary() { fi } -# Returns 0 when done, 1 when the entry must be kept. +# Returns 0 when done, 1 when the entry must be kept (with RESTORE_KEPT_APPLIED set). restore_entry() { local key="$1" prior="$2" applied="$3" - local current="" + local current="" snapshot="" after="" + + RESTORE_KEPT_APPLIED="$applied" if ! is_known_setting "$key"; then warn "dropping unknown setting $key" return 0 fi - if ! is_state_value "$key" "$prior" || ! is_state_value "$key" "$applied"; then + if ! is_snapshot_value "$key" "$prior" || ! is_allowed_value "$key" "$applied"; then warn "dropping invalid entry $key" return 0 fi @@ -461,14 +519,22 @@ restore_entry() { return 0 fi - if [[ $current == $prior ]]; then + if ! snapshot=$(setting_snapshot "$key"); then + warn "$key is unavailable, not restored" + return 1 + fi + + if [[ $snapshot == $prior ]]; then return 0 fi - if setting_apply "$key" "$prior"; then + if setting_put_back "$key" "$prior"; then return 0 else warn "could not restore $key" + if after=$(setting_read "$key"); then + RESTORE_KEPT_APPLIED="$after" + fi return 1 fi } @@ -557,7 +623,7 @@ cmd_off() { for (( i = 0; i < ${#STATE_KEYS[@]}; i++ )); do if ! restore_entry "${STATE_KEYS[$i]}" "${STATE_PRIORS[$i]}" "${STATE_APPLIEDS[$i]}"; then - kept_entries+=("${STATE_KEYS[$i]}" "${STATE_PRIORS[$i]}" "${STATE_APPLIEDS[$i]}") + kept_entries+=("${STATE_KEYS[$i]}" "${STATE_PRIORS[$i]}" "$RESTORE_KEPT_APPLIED") fi done @@ -584,17 +650,18 @@ cmd_off() { cmd_apply() { local name="$1" local preset_path="" rc=0 - local key="" current="" prior="" applied="" since="" - local old_idx=0 i=0 - local carried_count=0 fresh_count=0 had_failure=0 + local key="" current="" snapshot="" prior="" applied="" after="" since="" + local old_idx=0 entry_idx=0 i=0 + local carried_count=0 fresh_count=0 had_failure=0 write_failed=0 local -A preset_class=() local -A carried_keys=() local -A kept_keys=() local -a kept_entries=() local -a new_entries=() local -a fresh_keys=() - local -a fresh_priors=() + local -a fresh_values=() local -a fresh_applieds=() + local -a fresh_entry_indices=() local -a all_entries=() if ! is_valid_mode_name "$name"; then @@ -661,7 +728,7 @@ cmd_apply() { continue fi if ! restore_entry "$key" "${STATE_PRIORS[$i]}" "${STATE_APPLIEDS[$i]}"; then - kept_entries+=("$key" "${STATE_PRIORS[$i]}" "${STATE_APPLIEDS[$i]}") + kept_entries+=("$key" "${STATE_PRIORS[$i]}" "$RESTORE_KEPT_APPLIED") if [[ -n $key ]]; then kept_keys["$key"]=1 fi @@ -676,13 +743,15 @@ cmd_apply() { elif [[ ${preset_class[$key]} == "fresh" ]]; then if [[ -n ${kept_keys[$key]:-} ]]; then warn "$key is still held by the previous mode, skipping" - elif current=$(setting_read "$key"); then - prior="$current" + elif current=$(setting_read "$key") && snapshot=$(setting_snapshot "$key"); then + prior="$snapshot" applied=${PRESET_VALUES[$key]} + entry_idx=$(( ${#kept_entries[@]} + ${#new_entries[@]} )) new_entries+=("$key" "$prior" "$applied") fresh_keys+=("$key") - fresh_priors+=("$prior") + fresh_values+=("$current") fresh_applieds+=("$applied") + fresh_entry_indices+=("$entry_idx") else warn "$key is unavailable, skipping" fi @@ -697,14 +766,25 @@ cmd_apply() { fi for (( i = 0; i < ${#fresh_keys[@]}; i++ )); do - if [[ ${fresh_priors[$i]} != ${fresh_applieds[$i]} ]]; then + if [[ ${fresh_values[$i]} != ${fresh_applieds[$i]} ]]; then if ! setting_apply "${fresh_keys[$i]}" "${fresh_applieds[$i]}"; then warn "could not set ${fresh_keys[$i]}" had_failure=1 + write_failed=1 + if after=$(setting_read "${fresh_keys[$i]}"); then + all_entries[$(( fresh_entry_indices[$i] + 2 ))]="$after" + fi fi fi done + if (( write_failed )); then + if ! write_state "$name" "$since" "${all_entries[@]}"; then + release_lock + return 1 + fi + fi + release_lock omarchy-hook mode-set "$name" >/dev/null cmd_status text || had_failure=1 diff --git a/test/shell.d/mode-test.sh b/test/shell.d/mode-test.sh index 67525d1a3cb2..ed72b64458e6 100755 --- a/test/shell.d/mode-test.sh +++ b/test/shell.d/mode-test.sh @@ -73,7 +73,16 @@ cat >"$tmp_dir/stubs/busctl" <<'EOF' printf 'b false\n' EOF -for stub in omarchy-restart-hyprctl omarchy-notification-send hyprctl; do +cat >"$tmp_dir/stubs/hyprctl" <<'EOF' +#!/bin/bash + +if [[ -e $STUB_WORLD/hypr-animations-on ]]; then + printf '{"bool":true}\n' +fi +exit 0 +EOF + +for stub in omarchy-restart-hyprctl omarchy-notification-send; do cat >"$tmp_dir/stubs/$stub" <<'EOF' #!/bin/bash exit 0 @@ -703,7 +712,7 @@ cpu_session write_user_preset lively $'animations=on\n' run_mode lively expect_eq "$T" "lively exit code" "$RC" "0" -expect_eq "$T" "state animations" "$(jq -c .settings.animations "$STATE_FILE")" '{"prior":"auto","applied":"on"}' +expect_eq "$T" "state animations" "$(jq -c .settings.animations "$STATE_FILE")" '{"prior":"default","applied":"on"}' [[ -f $WITHOUT_GPU_FLAG ]] || fail "$T" "without-gpu flag missing after lively" [[ ! -e $NO_ANIM_FLAG ]] || fail "$T" "no-animations flag exists after lively" run_mode off @@ -720,21 +729,103 @@ T="turning animations off from the automatic state is undone exactly" cpu_session run_mode travel expect_eq "$T" "travel exit code" "$RC" "0" -expect_eq "$T" "state animations" "$(jq -c .settings.animations "$STATE_FILE")" '{"prior":"auto","applied":"off"}' -[[ -f $NO_ANIM_FLAG ]] || fail "$T" "no-animations flag missing after travel" +expect_eq "$T" "state animations" "$(jq -c .settings.animations "$STATE_FILE")" '{"prior":"default","applied":"off"}' +[[ ! -e $WITHOUT_GPU_FLAG ]] || fail "$T" "without-gpu flag exists after travel" +[[ ! -e $NO_ANIM_FLAG ]] || fail "$T" "no-animations flag exists after travel" run_mode status expect_eq "$T" "status exit code" "$RC" "0" -printf '%s\n' "$OUT" | grep -Fxq "animations=off (was auto)" || - fail "$T" "status missing 'animations=off (was auto)': $OUT" +printf '%s\n' "$OUT" | grep -Fxq "animations=off (was default)" || + fail "$T" "status missing 'animations=off (was default)': $OUT" run_mode off expect_eq "$T" "off exit code" "$RC" "0" [[ ! -e $WITHOUT_GPU_FLAG ]] || fail "$T" "without-gpu flag still exists after off" [[ ! -e $NO_ANIM_FLAG ]] || fail "$T" "no-animations flag still exists after off" [[ ! -e $STATE_FILE ]] || fail "$T" "state file still exists after off" -write_user_preset bad $'animations=auto\n' +touch "$S/hypr-animations-on" +write_user_preset still $'animations=off\n' +run_mode still +expect_eq "$T" "still exit code" "$RC" "0" +[[ -f $NO_ANIM_FLAG ]] || fail "$T" "no-animations flag missing after still" +run_mode off +expect_eq "$T" "still off exit code" "$RC" "0" +[[ ! -e $WITHOUT_GPU_FLAG ]] || fail "$T" "without-gpu flag exists after still off" +[[ ! -e $NO_ANIM_FLAG ]] || fail "$T" "no-animations flag still exists after still off" +[[ ! -e $STATE_FILE ]] || fail "$T" "state file still exists after still off" +write_user_preset bad $'animations=default\n' run_mode bad expect_eq "$T" "bad exit code" "$RC" "2" -[[ $ERR == *"auto is not a value for animations"* ]] || - fail "$T" "stderr missing 'auto is not a value for animations': $ERR" +[[ $ERR == *"default is not a value for animations"* ]] || + fail "$T" "stderr missing 'default is not a value for animations': $ERR" +gpu_session +pass "$T" + +# 30. a restore that stopped half-way is finished by the next off +reset_world +T="a restore that stopped half-way is finished by the next off" +if (( EUID == 0 )); then + skip "$T" +else + omarchy-toggle animations-without-gpu on + write_user_preset still $'animations=off\n' + run_mode still + expect_eq "$T" "still exit code" "$RC" "0" + expect_eq "$T" "state animations after still" "$(jq -c .settings.animations "$STATE_FILE")" '{"prior":"on-without-gpu","applied":"off"}' + [[ -f $NO_ANIM_FLAG ]] || fail "$T" "no-animations flag missing after still" + [[ ! -e $WITHOUT_GPU_FLAG ]] || fail "$T" "without-gpu flag exists after still" + chmod 555 "$HOME/.local/state/omarchy/toggles" + run_mode off + chmod 755 "$HOME/.local/state/omarchy/toggles" + expect_eq "$T" "first off exit code" "$RC" "1" + [[ $ERR == *"could not restore animations"* ]] || + fail "$T" "stderr missing 'could not restore animations': $ERR" + expect_eq "$T" "state animations after first off" "$(jq -c .settings.animations "$STATE_FILE")" '{"prior":"on-without-gpu","applied":"on"}' + [[ ! -e $NO_ANIM_FLAG ]] || fail "$T" "no-animations flag still exists after first off" + [[ ! -e $WITHOUT_GPU_FLAG ]] || fail "$T" "without-gpu flag exists after first off" + run_mode off + expect_eq "$T" "second off exit code" "$RC" "0" + [[ -f $WITHOUT_GPU_FLAG ]] || fail "$T" "without-gpu flag missing after second off" + [[ ! -e $NO_ANIM_FLAG ]] || fail "$T" "no-animations flag exists after second off" + [[ ! -e $STATE_FILE ]] || fail "$T" "state file still exists after second off" + pass "$T" +fi + +# 31. a mode puts both animation flag files back as they were +T="a mode puts both animation flag files back as they were" +reset_world +omarchy-toggle animations-without-gpu on +write_user_preset still $'animations=off\n' +run_mode still +expect_eq "$T" "case 1 still exit code" "$RC" "0" +run_mode off +expect_eq "$T" "case 1 off exit code" "$RC" "0" +[[ -f $WITHOUT_GPU_FLAG ]] || fail "$T" "case 1 without-gpu flag missing after off" +[[ ! -e $NO_ANIM_FLAG ]] || fail "$T" "case 1 no-animations flag exists after off" +[[ ! -e $STATE_FILE ]] || fail "$T" "case 1 state file still exists after off" + +reset_world +cpu_session +omarchy-hyprland-toggle no-animations on >/dev/null +omarchy-toggle animations-without-gpu on +write_user_preset lively $'animations=on\n' +run_mode lively +expect_eq "$T" "case 2 lively exit code" "$RC" "0" +expect_eq "$T" "case 2 state animations" "$(jq -c .settings.animations "$STATE_FILE")" '{"prior":"off-without-gpu","applied":"on"}' +run_mode off +expect_eq "$T" "case 2 off exit code" "$RC" "0" +[[ -f $NO_ANIM_FLAG ]] || fail "$T" "case 2 no-animations flag missing after off" +[[ -f $WITHOUT_GPU_FLAG ]] || fail "$T" "case 2 without-gpu flag missing after off" +[[ ! -e $STATE_FILE ]] || fail "$T" "case 2 state file still exists after off" gpu_session + +reset_world +omarchy-hyprland-toggle no-animations on >/dev/null +write_user_preset lively $'animations=on\n' +run_mode lively +expect_eq "$T" "case 3 lively exit code" "$RC" "0" +[[ ! -e $NO_ANIM_FLAG ]] || fail "$T" "case 3 no-animations flag exists after lively" +run_mode off +expect_eq "$T" "case 3 off exit code" "$RC" "0" +[[ -f $NO_ANIM_FLAG ]] || fail "$T" "case 3 no-animations flag missing after off" +[[ ! -e $WITHOUT_GPU_FLAG ]] || fail "$T" "case 3 without-gpu flag exists after off" +[[ ! -e $STATE_FILE ]] || fail "$T" "case 3 state file still exists after off" pass "$T" From d5653e7bf7d049ccf0ce07f15cc6c8ebd4a3229c Mon Sep 17 00:00:00 2001 From: Taksh Date: Wed, 2 Dec 2026 08:11:32 +0530 Subject: [PATCH 12/15] Preserve user preferences while desktop modes are active --- bin/omarchy-mode | 51 +++++++++++++++++++++++++++++++++------ test/shell.d/mode-test.sh | 35 +++++++++++++++++++++++---- 2 files changed, 73 insertions(+), 13 deletions(-) diff --git a/bin/omarchy-mode b/bin/omarchy-mode index 0fc5b3233bb3..bec4ddd257b7 100755 --- a/bin/omarchy-mode +++ b/bin/omarchy-mode @@ -26,6 +26,7 @@ STATE_SINCE="" STATE_KEYS=() STATE_PRIORS=() STATE_APPLIEDS=() +declare -A STATE_APPLIED_SNAPSHOTS=() declare -A OLD_INDEX=() RESTORE_KEPT_APPLIED="" @@ -199,7 +200,10 @@ setting_write() { fi ;; power-profile) - omarchy-powerprofiles-set autodetect "$value" >/dev/null 2>&1 + # Modes hold a temporary profile; saved AC/battery preferences belong to + # the user and must survive a power-source change while a mode is active. + powerprofilesctl set "$value" >/dev/null 2>&1 || return + omarchy-shell -q battery refreshPowerProfile >/dev/null 2>&1 || true ;; *) return 1 ;; esac @@ -313,19 +317,21 @@ validate_state_raw() { and (.value | type == "object") and (.value.prior | no_ctrl) and (.value.applied | no_ctrl) + and ((.value.appliedSnapshot // "") | no_ctrl) )) ' <<<"$raw" >/dev/null 2>&1 } parse_state_raw() { local raw="$1" - local key prior applied idx=0 + local key prior applied applied_snapshot idx=0 STATE_MODE="" STATE_SINCE="" STATE_KEYS=() STATE_PRIORS=() STATE_APPLIEDS=() + STATE_APPLIED_SNAPSHOTS=() OLD_INDEX=() { @@ -333,10 +339,12 @@ parse_state_raw() { IFS= read -r -d '' STATE_SINCE while IFS= read -r -d '' key && IFS= read -r -d '' prior && - IFS= read -r -d '' applied; do + IFS= read -r -d '' applied && + IFS= read -r -d '' applied_snapshot; do STATE_KEYS+=("$key") STATE_PRIORS+=("$prior") STATE_APPLIEDS+=("$applied") + STATE_APPLIED_SNAPSHOTS["$key"]="$applied_snapshot" if [[ -n $key ]]; then OLD_INDEX["$key"]=$idx fi @@ -346,7 +354,7 @@ parse_state_raw() { jq -j ' .mode, "\u0000", .since, "\u0000", - (.settings | to_entries[] | .key, "\u0000", .value.prior, "\u0000", .value.applied, "\u0000") + (.settings | to_entries[] | .key, "\u0000", .value.prior, "\u0000", .value.applied, "\u0000", (.value.appliedSnapshot // ""), "\u0000") ' <<<"$raw" 2>/dev/null ) } @@ -374,17 +382,25 @@ read_state_file() { } write_state() { - local mode="$1" since="$2" + local mode="$1" since="$2" key + local -a snapshots=() shift 2 + for key in "${!STATE_APPLIED_SNAPSHOTS[@]}"; do + if [[ -n ${STATE_APPLIED_SNAPSHOTS[$key]} ]]; then + snapshots+=("$key" "${STATE_APPLIED_SNAPSHOTS[$key]}") + fi + done + local snapshots_json + snapshots_json=$(jq -cn '$ARGS.positional | . as $a | reduce range(0; length; 2) as $i ({}; .[$a[$i]] = $a[$i + 1])' --args "${snapshots[@]}") || return 1 if ! { - jq -cn --arg mode "$mode" --arg since "$since" ' + jq -cn --arg mode "$mode" --arg since "$since" --argjson snapshots "$snapshots_json" ' ($ARGS.positional) as $a | { mode: $mode, since: $since, settings: ( - reduce range(0; ($a | length); 3) as $i ({}; . + {($a[$i]): {prior: $a[$i + 1], applied: $a[$i + 2]}}) + reduce range(0; ($a | length); 3) as $i ({}; . + {($a[$i]): {prior: $a[$i + 1], applied: $a[$i + 2]} + (if $snapshots[$a[$i]] then {appliedSnapshot: $snapshots[$a[$i]]} else {} end)}) ) } ' --args "$@" >"$STATE_TMP" @@ -524,6 +540,10 @@ restore_entry() { return 1 fi + if [[ -n ${STATE_APPLIED_SNAPSHOTS[$key]:-} && $snapshot != "${STATE_APPLIED_SNAPSHOTS[$key]}" ]]; then + return 0 + fi + if [[ $snapshot == $prior ]]; then return 0 fi @@ -534,6 +554,9 @@ restore_entry() { warn "could not restore $key" if after=$(setting_read "$key"); then RESTORE_KEPT_APPLIED="$after" + if [[ $key == "animations" ]]; then + STATE_APPLIED_SNAPSHOTS["$key"]=$(setting_snapshot "$key") + fi fi return 1 fi @@ -687,6 +710,7 @@ cmd_apply() { STATE_KEYS=() STATE_PRIORS=() STATE_APPLIEDS=() + STATE_APPLIED_SNAPSHOTS=() OLD_INDEX=() read_state_file @@ -702,7 +726,8 @@ cmd_apply() { preset_class["$key"]="skip" elif [[ -n ${OLD_INDEX[$key]:-} ]]; then old_idx=${OLD_INDEX[$key]} - if [[ ${STATE_APPLIEDS[$old_idx]} == $current && ${STATE_APPLIEDS[$old_idx]} == ${PRESET_VALUES[$key]} ]]; then + if [[ ${STATE_APPLIEDS[$old_idx]} == $current && ${STATE_APPLIEDS[$old_idx]} == ${PRESET_VALUES[$key]} ]] && + { [[ -z ${STATE_APPLIED_SNAPSHOTS[$key]:-} ]] || [[ $(setting_snapshot "$key") == "${STATE_APPLIED_SNAPSHOTS[$key]}" ]]; }; then preset_class["$key"]="carried" carried_keys["$key"]=1 carried_count=$(( carried_count + 1 )) @@ -745,6 +770,9 @@ cmd_apply() { warn "$key is still held by the previous mode, skipping" elif current=$(setting_read "$key") && snapshot=$(setting_snapshot "$key"); then prior="$snapshot" + if [[ $key == "animations" ]]; then + STATE_APPLIED_SNAPSHOTS["$key"]="$snapshot" + fi applied=${PRESET_VALUES[$key]} entry_idx=$(( ${#kept_entries[@]} + ${#new_entries[@]} )) new_entries+=("$key" "$prior" "$applied") @@ -778,6 +806,13 @@ cmd_apply() { fi done + for key in "${fresh_keys[@]}"; do + if [[ $key == "animations" ]] && snapshot=$(setting_snapshot "$key"); then + STATE_APPLIED_SNAPSHOTS["$key"]="$snapshot" + write_failed=1 + fi + done + if (( write_failed )); then if ! write_state "$name" "$since" "${all_entries[@]}"; then release_lock diff --git a/test/shell.d/mode-test.sh b/test/shell.d/mode-test.sh index ed72b64458e6..7dd278b112ac 100755 --- a/test/shell.d/mode-test.sh +++ b/test/shell.d/mode-test.sh @@ -712,7 +712,7 @@ cpu_session write_user_preset lively $'animations=on\n' run_mode lively expect_eq "$T" "lively exit code" "$RC" "0" -expect_eq "$T" "state animations" "$(jq -c .settings.animations "$STATE_FILE")" '{"prior":"default","applied":"on"}' +expect_eq "$T" "state animations" "$(jq -c '.settings.animations | {prior, applied}' "$STATE_FILE")" '{"prior":"default","applied":"on"}' [[ -f $WITHOUT_GPU_FLAG ]] || fail "$T" "without-gpu flag missing after lively" [[ ! -e $NO_ANIM_FLAG ]] || fail "$T" "no-animations flag exists after lively" run_mode off @@ -729,7 +729,7 @@ T="turning animations off from the automatic state is undone exactly" cpu_session run_mode travel expect_eq "$T" "travel exit code" "$RC" "0" -expect_eq "$T" "state animations" "$(jq -c .settings.animations "$STATE_FILE")" '{"prior":"default","applied":"off"}' +expect_eq "$T" "state animations" "$(jq -c '.settings.animations | {prior, applied}' "$STATE_FILE")" '{"prior":"default","applied":"off"}' [[ ! -e $WITHOUT_GPU_FLAG ]] || fail "$T" "without-gpu flag exists after travel" [[ ! -e $NO_ANIM_FLAG ]] || fail "$T" "no-animations flag exists after travel" run_mode status @@ -769,7 +769,7 @@ else write_user_preset still $'animations=off\n' run_mode still expect_eq "$T" "still exit code" "$RC" "0" - expect_eq "$T" "state animations after still" "$(jq -c .settings.animations "$STATE_FILE")" '{"prior":"on-without-gpu","applied":"off"}' + expect_eq "$T" "state animations after still" "$(jq -c '.settings.animations | {prior, applied}' "$STATE_FILE")" '{"prior":"on-without-gpu","applied":"off"}' [[ -f $NO_ANIM_FLAG ]] || fail "$T" "no-animations flag missing after still" [[ ! -e $WITHOUT_GPU_FLAG ]] || fail "$T" "without-gpu flag exists after still" chmod 555 "$HOME/.local/state/omarchy/toggles" @@ -778,7 +778,7 @@ else expect_eq "$T" "first off exit code" "$RC" "1" [[ $ERR == *"could not restore animations"* ]] || fail "$T" "stderr missing 'could not restore animations': $ERR" - expect_eq "$T" "state animations after first off" "$(jq -c .settings.animations "$STATE_FILE")" '{"prior":"on-without-gpu","applied":"on"}' + expect_eq "$T" "state animations after first off" "$(jq -c '.settings.animations | {prior, applied}' "$STATE_FILE")" '{"prior":"on-without-gpu","applied":"on"}' [[ ! -e $NO_ANIM_FLAG ]] || fail "$T" "no-animations flag still exists after first off" [[ ! -e $WITHOUT_GPU_FLAG ]] || fail "$T" "without-gpu flag exists after first off" run_mode off @@ -809,7 +809,7 @@ omarchy-toggle animations-without-gpu on write_user_preset lively $'animations=on\n' run_mode lively expect_eq "$T" "case 2 lively exit code" "$RC" "0" -expect_eq "$T" "case 2 state animations" "$(jq -c .settings.animations "$STATE_FILE")" '{"prior":"off-without-gpu","applied":"on"}' +expect_eq "$T" "case 2 state animations" "$(jq -c '.settings.animations | {prior, applied}' "$STATE_FILE")" '{"prior":"off-without-gpu","applied":"on"}' run_mode off expect_eq "$T" "case 2 off exit code" "$RC" "0" [[ -f $NO_ANIM_FLAG ]] || fail "$T" "case 2 no-animations flag missing after off" @@ -829,3 +829,28 @@ expect_eq "$T" "case 3 off exit code" "$RC" "0" [[ ! -e $WITHOUT_GPU_FLAG ]] || fail "$T" "case 3 without-gpu flag exists after off" [[ ! -e $STATE_FILE ]] || fail "$T" "case 3 state file still exists after off" pass "$T" + +# A persistent animation choice made during a no-op mode survives leaving it. +reset_world +cpu_session +run_mode travel +expect_eq "manual animation choice" "travel exit code" "$RC" "0" +omarchy-toggle-animations off >/dev/null +run_mode off +expect_eq "manual animation choice" "off exit code" "$RC" "0" +[[ -f $NO_ANIM_FLAG ]] || fail "manual animation choice survives leaving the mode" +pass "manual animation choice survives leaving a no-op mode" + +# Temporary profiles never replace either saved power-source preference. +reset_world +mkdir -p "$HOME/.local/state/omarchy/powerprofiles" +printf 'performance\n' >"$HOME/.local/state/omarchy/powerprofiles/ac" +printf 'balanced\n' >"$HOME/.local/state/omarchy/powerprofiles/battery" +run_mode travel +expect_eq "temporary power profile" "travel exit code" "$RC" "0" +printf 'balanced\n' >"$S/pp" +run_mode off +expect_eq "temporary power profile" "off exit code" "$RC" "0" +expect_eq "temporary power profile" "saved ac" "$(<"$HOME/.local/state/omarchy/powerprofiles/ac")" "performance" +expect_eq "temporary power profile" "saved battery" "$(<"$HOME/.local/state/omarchy/powerprofiles/battery")" "balanced" +pass "power-source preferences survive a mode and unplugging" From 9c59ce99a412357481a0caeb405c63e9925d703a Mon Sep 17 00:00:00 2001 From: Taksh Date: Wed, 2 Dec 2026 08:11:32 +0530 Subject: [PATCH 13/15] Check doctor listing behavior without freezing the catalog --- test/shell.d/doctor-test.sh | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/test/shell.d/doctor-test.sh b/test/shell.d/doctor-test.sh index b8cd6dcf6301..9720ad325420 100644 --- a/test/shell.d/doctor-test.sh +++ b/test/shell.d/doctor-test.sh @@ -586,12 +586,13 @@ expect "40: runtime-dir run creates the df probe marker" "present" "$([[ -e $tes new_tree list_out=$(run_doctor --list 2>/dev/null) -expect "41: --list prints 8 lines" "8" "$(wc -l <<<"$list_out")" -expect "41: --list ids match CHECKS order" \ - "runtime-dir shell notifications user-services migrations restarts omarchy-path theme" \ - "$(awk '{print $1}' <<<"$list_out" | paste -sd' ' -)" +list_ids=$(awk '{print $1}' <<<"$list_out" | paste -sd' ' -) +for required in runtime-dir shell notifications user-services migrations restarts omarchy-path theme; do + [[ " $list_ids " == *" $required "* ]] || fail "41: --list includes $required" +done +expect "41: --list ids are unique" "$(awk '{print $1}' <<<"$list_out" | sort)" "$(awk '{print $1}' <<<"$list_out" | sort -u)" list_combo=$(run_doctor --json --list theme 2>/dev/null) -expect "41: --json --list theme prints the same 8 lines" "$list_out" "$list_combo" +expect "41: --json --list theme prints the same entries" "$list_out" "$list_combo" dup_out=$(doctor theme theme) expect "42: duplicate check id runs once" "1" "$(jq -r '.checks | length' <<<"$dup_out")" @@ -604,7 +605,7 @@ expect "43: --json top-level keys are exact" "checks omarchy version" "$(jq -r ' expect "43: --json version is 1" "1" "$(jq -r '.version' <<<"$full_json")" expect "43: --json omarchy is stub" "stub" "$(jq -r '.omarchy' <<<"$full_json")" expect "43: --json check ids equal CHECKS in order" \ - "runtime-dir shell notifications user-services migrations restarts omarchy-path theme" \ + "$list_ids" \ "$(jq -r '.checks | map(.id) | join(" ")' <<<"$full_json")" expect "43: --json every check has exact four keys" \ "detail hint id result" \ @@ -642,7 +643,7 @@ fi router_status=0 router_out=$(OMARCHY_PATH="$ROOT" "$ROOT/bin/omarchy" doctor --list 2>/dev/null) || router_status=$? expect "46: router omarchy doctor --list exits 0" "0" "$router_status" -expect "46: router omarchy doctor --list prints 8 lines" "8" "$(wc -l <<<"$router_out")" +expect "46: router lists the same check ids" "$list_ids" "$(awk '{print $1}' <<<"$router_out" | paste -sd' ' -)" new_tree setup_stale_logs From 49fe3c6d004a90dbab6a150e69906f5aebd135a1 Mon Sep 17 00:00:00 2001 From: Taksh Date: Wed, 2 Dec 2026 08:22:53 +0530 Subject: [PATCH 14/15] Keep interrupted animation writes recoverable --- bin/omarchy-mode | 17 +++++++++-------- test/shell.d/mode-test.sh | 12 ++++++++++++ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/bin/omarchy-mode b/bin/omarchy-mode index bec4ddd257b7..96afb6903a1e 100755 --- a/bin/omarchy-mode +++ b/bin/omarchy-mode @@ -344,8 +344,8 @@ parse_state_raw() { STATE_KEYS+=("$key") STATE_PRIORS+=("$prior") STATE_APPLIEDS+=("$applied") - STATE_APPLIED_SNAPSHOTS["$key"]="$applied_snapshot" if [[ -n $key ]]; then + STATE_APPLIED_SNAPSHOTS["$key"]="$applied_snapshot" OLD_INDEX["$key"]=$idx fi idx=$(( idx + 1 )) @@ -770,9 +770,9 @@ cmd_apply() { warn "$key is still held by the previous mode, skipping" elif current=$(setting_read "$key") && snapshot=$(setting_snapshot "$key"); then prior="$snapshot" - if [[ $key == "animations" ]]; then - STATE_APPLIED_SNAPSHOTS["$key"]="$snapshot" - fi + # No applied snapshot means this write is unfinished. Recovery must + # not mistake an interrupted mode's own flag changes for user edits. + unset 'STATE_APPLIED_SNAPSHOTS[$key]' applied=${PRESET_VALUES[$key]} entry_idx=$(( ${#kept_entries[@]} + ${#new_entries[@]} )) new_entries+=("$key" "$prior" "$applied") @@ -804,12 +804,13 @@ cmd_apply() { fi fi fi - done - - for key in "${fresh_keys[@]}"; do + key=${fresh_keys[$i]} if [[ $key == "animations" ]] && snapshot=$(setting_snapshot "$key"); then STATE_APPLIED_SNAPSHOTS["$key"]="$snapshot" - write_failed=1 + if ! write_state "$name" "$since" "${all_entries[@]}"; then + release_lock + return 1 + fi fi done diff --git a/test/shell.d/mode-test.sh b/test/shell.d/mode-test.sh index 7dd278b112ac..cd8339dc29a2 100755 --- a/test/shell.d/mode-test.sh +++ b/test/shell.d/mode-test.sh @@ -854,3 +854,15 @@ expect_eq "temporary power profile" "off exit code" "$RC" "0" expect_eq "temporary power profile" "saved ac" "$(<"$HOME/.local/state/omarchy/powerprofiles/ac")" "performance" expect_eq "temporary power profile" "saved battery" "$(<"$HOME/.local/state/omarchy/powerprofiles/battery")" "balanced" pass "power-source preferences survive a mode and unplugging" + +# Killing a later setting must not turn a mode's completed write into a user edit. +reset_world +touch "$S/hypr-animations-on" "$S/kill-on-setdnd" +write_user_preset cut-flags $'animations=off\ndnd=on\n' +setsid -w "$ROOT/bin/omarchy-mode" cut-flags >/dev/null 2>&1 || true +[[ -f $NO_ANIM_FLAG ]] || fail "interrupted mode had applied animations" +rm "$S/kill-on-setdnd" +run_mode off +expect_eq "interrupted animation apply" "off exit code" "$RC" "0" +[[ ! -e $NO_ANIM_FLAG ]] || fail "interrupted mode restores animation flags" +pass "an interruption in a later setting keeps animation restoration recoverable" From ede302cd614447a1c3b7bc2e6a848f471f40155c Mon Sep 17 00:00:00 2001 From: Taksh Date: Wed, 2 Dec 2026 08:56:24 +0530 Subject: [PATCH 15/15] Keep CLI checks focused on command behavior --- test/shell.d/doctor-test.sh | 6 ------ test/shell.d/mode-test.sh | 8 +++++--- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/test/shell.d/doctor-test.sh b/test/shell.d/doctor-test.sh index 9720ad325420..949b43531780 100644 --- a/test/shell.d/doctor-test.sh +++ b/test/shell.d/doctor-test.sh @@ -88,12 +88,6 @@ expect() { pass "$description" } -# Verify escalation stubs record their names before any doctor test runs. -"$stub_bin/sudo" || true -"$stub_bin/pkexec" || true -expect "sudo and pkexec stubs record their names in the marker file" $'sudo\npkexec' "$(<"$test_tmp/escalated")" -rm -f "$test_tmp/escalated" - new_tree() { fake=$(mktemp -d "$test_tmp/tree.XXXXXX") mkdir -p "$fake/home/.local/state/omarchy/current/theme" "$fake/run" \ diff --git a/test/shell.d/mode-test.sh b/test/shell.d/mode-test.sh index cd8339dc29a2..942a3abb3c45 100755 --- a/test/shell.d/mode-test.sh +++ b/test/shell.d/mode-test.sh @@ -154,13 +154,15 @@ expect_eq() { fi } -# 1. list prints the three built-in presets with summaries +# 1. list prints required built-in presets with summaries reset_world -T="list prints the three built-in presets with summaries" +T="list prints required built-in presets with summaries" run_mode list expect_eq "$T" "exit code" "$RC" "0" expected=$(printf 'focus\tSilence notifications\npresent\tSilence notifications, stay awake and hide the bar\ntravel\tSave power with the power-saver profile and no animations') -expect_eq "$T" "stdout" "$OUT" "$expected" +while IFS= read -r row; do + grep -Fxq "$row" <<<"$OUT" || fail "$T" "missing preset: $row" +done <<<"$expected" pass "$T" # 2. focus turns DND on and off restores it off