Extract shared shell.json editing helpers for the bar commands

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
David Heinemeier Hansson
2026-07-24 12:16:51 -07:00
co-authored by Claude Opus 5
parent 603408a73d
commit 2baf967e06
3 changed files with 66 additions and 118 deletions
+1 -57
View File
@@ -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 <<USAGE
@@ -32,60 +30,6 @@ Examples:
USAGE
}
fail() {
echo "omarchy-bar: $*" >&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() {
+3 -61
View File
@@ -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 <<USAGE
@@ -52,31 +50,9 @@ Examples:
USAGE
}
fail() {
echo "omarchy-bar-plugin: $*" >&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"
}
+62
View File
@@ -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
}