From 95a2a1ce2bcae61d83ead0c513daa60ae29293a8 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 11 Oct 2026 16:14:57 +0200 Subject: [PATCH 1/3] Sync the theme into tmux with one source-file instead of a client per variable Co-Authored-By: Claude Opus 5.5 --- bin/omarchy-theme-set-tmux | 88 +++++++++++++++++++++-------- test/cli | 6 +- test/shell.d/theme-set-tmux-test.sh | 63 +++++++++++++++++++++ 3 files changed, 132 insertions(+), 25 deletions(-) create mode 100644 test/shell.d/theme-set-tmux-test.sh diff --git a/bin/omarchy-theme-set-tmux b/bin/omarchy-theme-set-tmux index d2e0e0e52612..3857d2799e09 100755 --- a/bin/omarchy-theme-set-tmux +++ b/bin/omarchy-theme-set-tmux @@ -11,19 +11,63 @@ if ! tmux list-sessions >/dev/null 2>&1; then exit 0 fi -set_tmux_environment() { +# Every setting reaches the tmux server as one script. A tmux client per +# variable per session costs several hundred forks on each theme switch. +TMUX_COMMANDS=() +TMUX_SESSIONS=() +declare -A THEME_COLORS=() + +# tmux takes single-quoted text literally; a single quote inside one closes +# it, joins a double-quoted quote, and reopens it. The quoting stays in this +# shell: a command substitution per argument would fork once per argument. +queue_tmux_command() { + local argument quoted line="" quote="'" + + for argument in "$@"; do + quoted="${argument//$quote/$quote\"$quote\"$quote}" + line+="${line:+ }$quote$quoted$quote" + done + + TMUX_COMMANDS+=("$line") +} + +queue_tmux_environment() { local key="$1" local value="$2" local session - tmux set-environment -g "$key" "$value" 2>/dev/null || return 0 + queue_tmux_command set-environment -g "$key" "$value" + + for session in "${TMUX_SESSIONS[@]}"; do + queue_tmux_command set-environment -t "$session" "$key" "$value" + done +} + +# source-file reports a line that fails, such as a session that closed in the +# meantime, and carries on with the rest. +apply_tmux_commands() { + (( ${#TMUX_COMMANDS[@]} > 0 )) || return 0 + + printf '%s\n' "${TMUX_COMMANDS[@]}" | tmux source-file - >/dev/null 2>&1 || true +} + +load_tmux_sessions() { + local session while IFS= read -r session; do - [[ -n $session ]] || continue - tmux set-environment -t "$session" "$key" "$value" 2>/dev/null || true + [[ -n $session ]] && TMUX_SESSIONS+=("$session") done < <(tmux list-sessions -F "#{session_id}" 2>/dev/null) } +# One resolver run covers every colour below. +load_theme_colors() { + local key value + + while IFS=$'\t' read -r key value; do + THEME_COLORS[$key]="$value" + done < <(omarchy-theme-color --file "$COLORS_TOML" --all 2>/dev/null) +} + sync_theme_environment() { local key value @@ -31,50 +75,42 @@ sync_theme_environment() { while IFS=$'\t' read -r key value; do [[ -n $key && -n $value ]] || continue - set_tmux_environment "$key" "$value" + queue_tmux_environment "$key" "$value" done < <(awk -F'"' '/hl\.env\("[A-Z0-9_]+", "[^"]+"\)/ { print $2 "\t" $4 }' "$GUM_ENV") } sync_colorfgbg() { local colorfgbg="15;0" - # theme_color resolves mode from colors.toml, a legacy light.mode file, or - # background luminance, so a bare light check covers all theme styles. - if [[ $(theme_color mode) == "light" ]]; then + # omarchy-theme-color resolves mode from colors.toml, a legacy light.mode + # file, or background luminance, so a bare light check covers all theme styles. + if [[ ${THEME_COLORS[mode]} == "light" ]]; then colorfgbg="0;15" fi - set_tmux_environment COLORFGBG "$colorfgbg" + queue_tmux_environment COLORFGBG "$colorfgbg" } theme_osc_sequences() { omarchy-theme-osc "$COLORS_TOML" } -theme_color() { - omarchy-theme-color --file "$COLORS_TOML" "$@" -} - sync_tmux_window_style() { - local background foreground cursor - - [[ -f $COLORS_TOML ]] || return + local foreground="${THEME_COLORS[foreground]}" + local background="${THEME_COLORS[background]}" + local cursor="${THEME_COLORS[cursor]}" - foreground=$(theme_color foreground) - background=$(theme_color background) - cursor=$(theme_color cursor) + [[ -f $COLORS_TOML && -n $foreground && -n $background ]] || return - [[ -n $foreground && -n $background ]] || return - - tmux set-option -g window-style "fg=$foreground,bg=$background" 2>/dev/null || true - tmux set-option -g window-active-style "fg=$foreground,bg=$background" 2>/dev/null || true + queue_tmux_command set-option -g window-style "fg=$foreground,bg=$background" + queue_tmux_command set-option -g window-active-style "fg=$foreground,bg=$background" # Apps like nvim clear the per-pane cursor colour with OSC 112. Without a # global fallback, tmux then resets the outer terminal's cursor, which in # terminals that can't reload their config (foot) restores the colour from # whatever theme was active when the terminal launched. if [[ -n $cursor ]]; then - tmux set-option -g cursor-colour "$cursor" 2>/dev/null || true + queue_tmux_command set-option -g cursor-colour "$cursor" fi } @@ -112,9 +148,13 @@ refresh_tmux_clients() { done < <(tmux list-clients -F "#{client_name}" 2>/dev/null) } +load_tmux_sessions +load_theme_colors + sync_theme_environment sync_colorfgbg sync_tmux_window_style +apply_tmux_commands sync_tmux_pane_colors signal_tmux_panes refresh_tmux_clients diff --git a/test/cli b/test/cli index 37a4ae762118..148dfebbd0e7 100755 --- a/test/cli +++ b/test/cli @@ -541,6 +541,10 @@ case "$1" in [[ $2 == "-F" ]] && echo "session-1" exit 0 ;; + source-file) + cat >>"${TMUX_LOG:-/dev/null}" + exit 0 + ;; set-environment|set-option|refresh-client) echo "$*" >>"${TMUX_LOG:-/dev/null}" exit 0 @@ -556,7 +560,7 @@ EOF chmod +x "$FAKE_BIN/tmux" TMUX_LOG="$PI_TMPDIR/tmux.log" PATH="$FAKE_BIN:$ROOT/bin:$PATH" HOME="$PI_TMPDIR" "$ROOT/bin/omarchy-theme-set-tmux" -grep -q 'set-environment -g COLORFGBG 0;15' "$PI_TMPDIR/tmux.log" || fail "tmux sync reads mode from colors.toml" +grep -Fxq "'set-environment' '-g' 'COLORFGBG' '0;15'" "$PI_TMPDIR/tmux.log" || fail "tmux sync reads mode from colors.toml" pass "tmux sync reads mode from colors.toml" cat >"$FAKE_BIN/pgrep" <<'EOF' diff --git a/test/shell.d/theme-set-tmux-test.sh b/test/shell.d/theme-set-tmux-test.sh new file mode 100644 index 000000000000..f6bcf328f2f4 --- /dev/null +++ b/test/shell.d/theme-set-tmux-test.sh @@ -0,0 +1,63 @@ +#!/bin/bash + +source "$(dirname "$0")/base-test.sh" + +require_command tmux + +TEST_HOME=$(mktemp -d) +# A tmux socket path must stay short, so the scratch server lives in /tmp. +TMUX_TMPDIR=$(mktemp -d /tmp/omarchy-tmux-test.XXXXXX) +export TMUX_TMPDIR +unset TMUX +trap 'tmux kill-server 2>/dev/null; rm -rf "$TEST_HOME" "$TMUX_TMPDIR"' EXIT + +FAKE_BIN="$TEST_HOME/bin" +CURRENT_THEME="$TEST_HOME/.local/state/omarchy/current/theme" +TMUX_LOG="$TEST_HOME/tmux.log" +REAL_TMUX=$(command -v tmux) +mkdir -p "$FAKE_BIN" "$CURRENT_THEME" + +cat >"$FAKE_BIN/tmux" <>"$TMUX_LOG" +exec "$REAL_TMUX" -f /dev/null "\$@" +EOF +chmod +x "$FAKE_BIN/tmux" + +cat >"$CURRENT_THEME/colors.toml" <<'EOF' +background = "#f4f4f4" +foreground = "#222222" +bright_foreground = "#111111" +EOF + +{ + for i in $(seq 1 40); do + printf 'hl.env("GUM_VAR_%s", "#%06x")\n' "$i" "$i" + done + printf '%s\n' "hl.env(\"QUOTED\", \"it's \$HOME ~ #{session_name} a;b\")" +} >"$CURRENT_THEME/gum_env.lua" + +for session in one two three; do + "$FAKE_BIN/tmux" new-session -d -s "$session" +done +: >"$TMUX_LOG" + +PATH="$FAKE_BIN:$ROOT/bin:$PATH" HOME="$TEST_HOME" "$ROOT/bin/omarchy-theme-set-tmux" + +for target in -g "-t one" "-t two" "-t three"; do + [[ $(tmux show-environment $target GUM_VAR_40) == "GUM_VAR_40=#000028" ]] || fail "tmux theme sync sets gum variables ($target)" + [[ $(tmux show-environment $target COLORFGBG) == "COLORFGBG=0;15" ]] || fail "tmux theme sync sets a light COLORFGBG ($target)" +done +pass "tmux theme sync sets theme variables globally and in every session" + +[[ $(tmux show-environment -t two QUOTED) == "QUOTED=it's \$HOME ~ #{session_name} a;b" ]] || \ + fail "tmux theme sync passes values through literally" "$(tmux show-environment -t two QUOTED)" +pass "tmux theme sync passes values through literally" + +[[ $(tmux show-options -gv window-style) == "fg=#222222,bg=#f4f4f4" ]] || fail "tmux theme sync sets the window style" +[[ $(tmux show-options -gv window-active-style) == "fg=#222222,bg=#f4f4f4" ]] || fail "tmux theme sync sets the active window style" +[[ $(tmux show-options -gv cursor-colour) == "#111111" ]] || fail "tmux theme sync sets the cursor colour" +pass "tmux theme sync sets window styles and the cursor colour" + +(( $(wc -l <"$TMUX_LOG") <= 8 )) || fail "tmux theme sync batches its settings into a few tmux clients" "$(sort "$TMUX_LOG" | uniq -c)" +pass "tmux theme sync batches its settings into a few tmux clients" From 76bf5ef03dce9f9fea2f1c57a92999a470b58ef9 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 11 Oct 2026 16:21:15 +0200 Subject: [PATCH 2/3] Pin the tmux theme test to its own socket Co-Authored-By: Claude Opus 5.5 --- test/shell.d/theme-set-tmux-test.sh | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/test/shell.d/theme-set-tmux-test.sh b/test/shell.d/theme-set-tmux-test.sh index f6bcf328f2f4..331943ebfc3b 100644 --- a/test/shell.d/theme-set-tmux-test.sh +++ b/test/shell.d/theme-set-tmux-test.sh @@ -4,23 +4,28 @@ source "$(dirname "$0")/base-test.sh" require_command tmux -TEST_HOME=$(mktemp -d) -# A tmux socket path must stay short, so the scratch server lives in /tmp. -TMUX_TMPDIR=$(mktemp -d /tmp/omarchy-tmux-test.XXXXXX) -export TMUX_TMPDIR +REAL_TMUX=$(command -v tmux) +TEST_HOME=$(mktemp -d) || fail "create a scratch home" +# A tmux socket path must stay short, so the scratch server's socket lives in +# /tmp. Every tmux call names it, so none can reach the user's own server. +SOCKET_DIR=$(mktemp -d /tmp/omarchy-tmux-test.XXXXXX) || fail "create a scratch tmux socket directory" +TMUX_SOCKET="$SOCKET_DIR/tmux" unset TMUX -trap 'tmux kill-server 2>/dev/null; rm -rf "$TEST_HOME" "$TMUX_TMPDIR"' EXIT +trap '"$REAL_TMUX" -S "$TMUX_SOCKET" kill-server 2>/dev/null; rm -rf "$TEST_HOME" "$SOCKET_DIR"' EXIT + +tmux() { + "$REAL_TMUX" -S "$TMUX_SOCKET" "$@" +} FAKE_BIN="$TEST_HOME/bin" CURRENT_THEME="$TEST_HOME/.local/state/omarchy/current/theme" TMUX_LOG="$TEST_HOME/tmux.log" -REAL_TMUX=$(command -v tmux) mkdir -p "$FAKE_BIN" "$CURRENT_THEME" cat >"$FAKE_BIN/tmux" <>"$TMUX_LOG" -exec "$REAL_TMUX" -f /dev/null "\$@" +exec "$REAL_TMUX" -S "$TMUX_SOCKET" -f /dev/null "\$@" EOF chmod +x "$FAKE_BIN/tmux" From 7e5cce9ccbb3aab09a648d404a39f7293da8a36c Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 11 Oct 2026 16:26:03 +0200 Subject: [PATCH 3/3] Start the tmux theme test's panes under the scratch home Co-Authored-By: Claude Opus 5.5 --- test/shell.d/theme-set-tmux-test.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/shell.d/theme-set-tmux-test.sh b/test/shell.d/theme-set-tmux-test.sh index 331943ebfc3b..e07dca3c0ef6 100644 --- a/test/shell.d/theme-set-tmux-test.sh +++ b/test/shell.d/theme-set-tmux-test.sh @@ -42,8 +42,10 @@ EOF printf '%s\n' "hl.env(\"QUOTED\", \"it's \$HOME ~ #{session_name} a;b\")" } >"$CURRENT_THEME/gum_env.lua" +# Panes run a plain sleep under the scratch home, so no shell startup file of +# the developer's runs or writes anywhere. for session in one two three; do - "$FAKE_BIN/tmux" new-session -d -s "$session" + HOME="$TEST_HOME" "$FAKE_BIN/tmux" new-session -d -s "$session" "exec sleep 600" done : >"$TMUX_LOG"