From 2baf967e06d63d27a8cc599c9d52cfa68a5adcb0 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 24 Jul 2026 12:16:51 -0700 Subject: [PATCH] Extract shared shell.json editing helpers for the bar commands Co-Authored-By: Claude Opus 5 (1M context) --- bin/omarchy-bar | 58 +----------------------------------- bin/omarchy-bar-plugin | 64 ++-------------------------------------- bin/omarchy-shell-config | 62 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 118 deletions(-) create mode 100755 bin/omarchy-shell-config diff --git a/bin/omarchy-bar b/bin/omarchy-bar index eb41c96b..c0bca1b3 100755 --- a/bin/omarchy-bar +++ b/bin/omarchy-bar @@ -7,9 +7,7 @@ set -euo pipefail -CONFIG_FILE="$HOME/.config/omarchy/shell.json" -OMARCHY_ROOT="${OMARCHY_PATH:-}" -DEFAULTS_FILE="$OMARCHY_ROOT/config/omarchy/shell.json" +source omarchy-shell-config usage() { cat <&2 - exit 1 -} - -refresh_shell_config() { - if ! omarchy-shell shell reloadConfig >/dev/null 2>&1; then - omarchy-shell -q shell rescanPlugins >/dev/null 2>&1 || true - fi -} - -source_file() { - if [[ -s $CONFIG_FILE ]]; then - printf '%s\n' "$CONFIG_FILE" - else - printf '%s\n' "$DEFAULTS_FILE" - fi -} - -# jq pipeline that normalizes shell.json into a well-shaped object with -# version=1, bar.layout.{left,center,right} arrays, and plugins array. -NORMALIZE=' - def object_or_empty: if type == "object" then . else {} end; - def array_or_empty: if type == "array" then . else [] end; - object_or_empty - | .version = 1 - | .bar = (.bar | object_or_empty) - | .bar.layout = (.bar.layout | object_or_empty) - | .bar.layout.left = (.bar.layout.left | array_or_empty) - | .bar.layout.center = (.bar.layout.center | array_or_empty) - | .bar.layout.right = (.bar.layout.right | array_or_empty) - | .plugins = (.plugins | array_or_empty) -' - -# Apply a jq program to the source file and atomically write the result to the -# user config, then refresh the running shell. Extra args after the program are -# forwarded to jq (e.g. --arg/--argjson). -_BAR_TMP="" -cleanup_bar_tmp() { - if [[ -n ${_BAR_TMP:-} ]]; then rm -f "$_BAR_TMP"; fi -} -trap cleanup_bar_tmp EXIT - -commit() { - local program="$1" - shift - mkdir -p "$(dirname "$CONFIG_FILE")" - _BAR_TMP=$(mktemp) - jq -S -e "$@" "$program" "$(source_file)" >"$_BAR_TMP" || fail "could not update shell config" - mv "$_BAR_TMP" "$CONFIG_FILE" - _BAR_TMP="" - refresh_shell_config -} - # ------------------------------------------------------------------ validation bar_option_exists() { diff --git a/bin/omarchy-bar-plugin b/bin/omarchy-bar-plugin index 925acaf7..864f663a 100755 --- a/bin/omarchy-bar-plugin +++ b/bin/omarchy-bar-plugin @@ -7,9 +7,7 @@ set -euo pipefail -CONFIG_FILE="$HOME/.config/omarchy/shell.json" -OMARCHY_ROOT="${OMARCHY_PATH:-}" -DEFAULTS_FILE="$OMARCHY_ROOT/config/omarchy/shell.json" +source omarchy-shell-config usage() { cat <&2 - exit 1 -} - -refresh_shell_config() { - if ! omarchy-shell shell reloadConfig >/dev/null 2>&1; then - omarchy-shell -q shell rescanPlugins >/dev/null 2>&1 || true - fi -} - -source_file() { - if [[ -s $CONFIG_FILE ]]; then - printf '%s\n' "$CONFIG_FILE" - else - printf '%s\n' "$DEFAULTS_FILE" - fi -} - -# jq defs shared across mutations. All `def`s come first so the pipeline that -# follows them stays valid jq. `entry_id` and the shape helpers are used by both -# the normalize pipeline and the resolve_target/resolve_source helpers. +# jq defs used by the mutation pipelines. All `def`s come first so the pipeline +# that follows them stays valid jq; NORMALIZE adds its own shape helpers. JQ_DEFS=' - def object_or_empty: if type == "object" then . else {} end; - def array_or_empty: if type == "array" then . else [] end; def entry_id: if type == "object" then (.id // "" | tostring) else tostring end; def anchor_for($section): { left: "omarchy.workspaces", center: "omarchy.weather", right: "omarchy.tray" }[$section]; def find_all($id; $only): @@ -130,40 +106,6 @@ JQ_DEFS=' end; ' -# jq pipeline that normalizes shell.json into a well-shaped object with -# version=1, bar.layout.{left,center,right} arrays, and plugins array. Every -# mutation pipes through this so downstream jq can assume structure. -NORMALIZE=' - object_or_empty - | .version = 1 - | .bar = (.bar | object_or_empty) - | .bar.layout = (.bar.layout | object_or_empty) - | .bar.layout.left = (.bar.layout.left | array_or_empty) - | .bar.layout.center = (.bar.layout.center | array_or_empty) - | .bar.layout.right = (.bar.layout.right | array_or_empty) - | .plugins = (.plugins | array_or_empty) -' - -# Apply a jq program to the source file and atomically write the result to the -# user config, then refresh the running shell. Extra args after the program are -# forwarded to jq (e.g. --arg/--argjson). -_BAR_TMP="" -cleanup_bar_tmp() { - if [[ -n ${_BAR_TMP:-} ]]; then rm -f "$_BAR_TMP"; fi -} -trap cleanup_bar_tmp EXIT - -commit() { - local program="$1" - shift - mkdir -p "$(dirname "$CONFIG_FILE")" - _BAR_TMP=$(mktemp) - jq -S -e "$@" "$program" "$(source_file)" >"$_BAR_TMP" || fail "could not update shell config" - mv "$_BAR_TMP" "$CONFIG_FILE" - _BAR_TMP="" - refresh_shell_config -} - validate_section() { [[ $1 =~ ^(left|center|right)$ ]] || fail "section must be left, center, or right" } diff --git a/bin/omarchy-shell-config b/bin/omarchy-shell-config new file mode 100755 index 00000000..5cf0f639 --- /dev/null +++ b/bin/omarchy-shell-config @@ -0,0 +1,62 @@ +#!/bin/bash + +# omarchy:summary=Shared helpers for editing ~/.config/omarchy/shell.json (source this, don't run it). +# omarchy:hidden=true + +CONFIG_FILE="$HOME/.config/omarchy/shell.json" +DEFAULTS_FILE="$OMARCHY_PATH/config/omarchy/shell.json" + +fail() { + echo "${0##*/}: $*" >&2 + exit 1 +} + +refresh_shell_config() { + if ! omarchy-shell shell reloadConfig >/dev/null 2>&1; then + omarchy-shell -q shell rescanPlugins >/dev/null 2>&1 || true + fi +} + +source_file() { + if [[ -s $CONFIG_FILE ]]; then + printf '%s\n' "$CONFIG_FILE" + else + printf '%s\n' "$DEFAULTS_FILE" + fi +} + +# jq pipeline that normalizes shell.json into a well-shaped object with +# version=1, bar.layout.{left,center,right} arrays, and plugins array. Every +# mutation pipes through this so downstream jq can assume structure. +NORMALIZE=' + def object_or_empty: if type == "object" then . else {} end; + def array_or_empty: if type == "array" then . else [] end; + object_or_empty + | .version = 1 + | .bar = (.bar | object_or_empty) + | .bar.layout = (.bar.layout | object_or_empty) + | .bar.layout.left = (.bar.layout.left | array_or_empty) + | .bar.layout.center = (.bar.layout.center | array_or_empty) + | .bar.layout.right = (.bar.layout.right | array_or_empty) + | .plugins = (.plugins | array_or_empty) +' + +# Apply a jq program to the source file and atomically write the result to the +# user config, then refresh the running shell. Extra args after the program are +# forwarded to jq (e.g. --arg/--argjson). +_SHELL_CONFIG_TMP="" +cleanup_shell_config_tmp() { + if [[ -n $_SHELL_CONFIG_TMP ]]; then rm -f "$_SHELL_CONFIG_TMP"; fi +} +trap cleanup_shell_config_tmp EXIT + +commit() { + local program="$1" + shift + mkdir -p "$(dirname "$CONFIG_FILE")" + _SHELL_CONFIG_TMP=$(mktemp) + jq -S -e "$@" "$program" "$(source_file)" >"$_SHELL_CONFIG_TMP" || fail "could not update shell config" + mv "$_SHELL_CONFIG_TMP" "$CONFIG_FILE" + _SHELL_CONFIG_TMP="" + refresh_shell_config +}