Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 64 additions & 24 deletions bin/omarchy-theme-set-tmux
Original file line number Diff line number Diff line change
Expand Up @@ -11,70 +11,106 @@ 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

[[ -f $GUM_ENV ]] || return

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
}

Expand Down Expand Up @@ -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
6 changes: 5 additions & 1 deletion test/cli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'
Expand Down
70 changes: 70 additions & 0 deletions test/shell.d/theme-set-tmux-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/bin/bash

source "$(dirname "$0")/base-test.sh"

require_command tmux

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 '"$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"
mkdir -p "$FAKE_BIN" "$CURRENT_THEME"

cat >"$FAKE_BIN/tmux" <<EOF
#!/bin/bash
printf '%s\n' "\$1" >>"$TMUX_LOG"
exec "$REAL_TMUX" -S "$TMUX_SOCKET" -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"

# 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
HOME="$TEST_HOME" "$FAKE_BIN/tmux" new-session -d -s "$session" "exec sleep 600"
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"