From 4030bb4813dfeb72efbe7624ec2763060945f1d7 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 23 May 2026 13:46:27 +0200 Subject: [PATCH] Ensure tmux coloring is updated live too --- bin/omarchy-theme-osc | 37 +++++++++++ bin/omarchy-theme-set | 1 + bin/omarchy-theme-set-foot | 16 ++--- bin/omarchy-theme-set-tmux | 126 +++++++++++++++++++++++++++++++++++++ 4 files changed, 168 insertions(+), 12 deletions(-) create mode 100755 bin/omarchy-theme-osc create mode 100755 bin/omarchy-theme-set-tmux diff --git a/bin/omarchy-theme-osc b/bin/omarchy-theme-osc new file mode 100755 index 00000000..28f18388 --- /dev/null +++ b/bin/omarchy-theme-osc @@ -0,0 +1,37 @@ +#!/bin/bash + +# omarchy:summary=Print OSC sequences for an Omarchy color theme +# omarchy:hidden=true + +theme="${1:-$HOME/.config/omarchy/current/theme/colors.toml}" + +[[ -f $theme ]] || exit 0 + +awk -F= ' + function clean(value) { + gsub(/^[[:space:]]+|[[:space:]]+$/, "", value) + if (value ~ /^"/) { + sub(/^"/, "", value) + sub(/".*$/, "", value) + } + return value + } + + { + key = $1 + gsub(/^[[:space:]]+|[[:space:]]+$/, "", key) + value = clean($2) + } + + key == "foreground" { printf "\033]10;%s\007", value } + key == "background" { printf "\033]11;%s\007", value } + key == "cursor" { printf "\033]12;%s\007", value } + key == "selection_background" { printf "\033]17;%s\007", value } + key == "selection_foreground" { printf "\033]19;%s\007", value } + key ~ /^color[0-9]+$/ { + color_index = substr(key, 6) + 0 + if (color_index >= 0 && color_index <= 15) { + printf "\033]4;%d;%s\007", color_index, value + } + } +' "$theme" diff --git a/bin/omarchy-theme-set b/bin/omarchy-theme-set index 1f0c58ad..64ca6033 100755 --- a/bin/omarchy-theme-set +++ b/bin/omarchy-theme-set @@ -172,6 +172,7 @@ post_theme_commands=( omarchy-restart-opencode omarchy-restart-helix omarchy-theme-set-foot + omarchy-theme-set-tmux omarchy-theme-set-gnome omarchy-theme-set-pi omarchy-theme-set-browser diff --git a/bin/omarchy-theme-set-foot b/bin/omarchy-theme-set-foot index f7268d22..74ad3741 100755 --- a/bin/omarchy-theme-set-foot +++ b/bin/omarchy-theme-set-foot @@ -3,22 +3,14 @@ # omarchy:summary=Apply current Omarchy theme colors to running Foot terminals # omarchy:hidden=true -foot_theme=~/.config/omarchy/current/theme/foot.ini +colors_toml=~/.config/omarchy/current/theme/colors.toml -if [[ ! -f $foot_theme ]] || ! pgrep -x foot >/dev/null; then +if [[ ! -f $colors_toml ]] || ! pgrep -x foot >/dev/null; then exit 0 fi -foot_osc=$(awk -F= ' - function color(value) { return "#" value } - /^foreground=/ { printf "\033]10;%s\007", color($2) } - /^background=/ { printf "\033]11;%s\007", color($2) } - /^cursor=/ { split($2, parts, " "); printf "\033]12;%s\007", color(parts[2]) } - /^selection-background=/ { printf "\033]17;%s\007", color($2) } - /^selection-foreground=/ { printf "\033]19;%s\007", color($2) } - /^regular[0-7]=/ { split($1, parts, "regular"); printf "\033]4;%d;%s\007", parts[2], color($2) } - /^bright[0-7]=/ { split($1, parts, "bright"); printf "\033]4;%d;%s\007", parts[2] + 8, color($2) } -' "$foot_theme") +foot_osc=$(omarchy-theme-osc "$colors_toml") +[[ -n $foot_osc ]] || exit 0 for foot_pid in $(pgrep -x foot); do for child_pid in $(pgrep -P "$foot_pid"); do diff --git a/bin/omarchy-theme-set-tmux b/bin/omarchy-theme-set-tmux new file mode 100755 index 00000000..4b778fdd --- /dev/null +++ b/bin/omarchy-theme-set-tmux @@ -0,0 +1,126 @@ +#!/bin/bash + +# omarchy:summary=Sync current Omarchy theme environment into tmux +# omarchy:hidden=true + +CURRENT_THEME_PATH="$HOME/.config/omarchy/current/theme" +GUM_ENV="$CURRENT_THEME_PATH/gum_env.lua" +COLORS_TOML="$CURRENT_THEME_PATH/colors.toml" + +if ! tmux list-sessions >/dev/null 2>&1; then + exit 0 +fi + +set_tmux_environment() { + local key="$1" + local value="$2" + local session + + tmux set-environment -g "$key" "$value" 2>/dev/null || return 0 + + while IFS= read -r session; do + [[ -n $session ]] || continue + tmux set-environment -t "$session" "$key" "$value" 2>/dev/null || true + done < <(tmux list-sessions -F "#{session_id}" 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" + done < <(awk -F'"' '/hl\.env\("[A-Z0-9_]+", "[^"]+"\)/ { print $2 "\t" $4 }' "$GUM_ENV") +} + +sync_colorfgbg() { + local colorfgbg="15;0" + + if [[ -f $CURRENT_THEME_PATH/light.mode ]]; then + colorfgbg="0;15" + fi + + set_tmux_environment COLORFGBG "$colorfgbg" +} + +theme_osc_sequences() { + omarchy-theme-osc "$COLORS_TOML" +} + +theme_color() { + local key="$1" + + awk -F= -v key="$key" ' + { + field = $1 + gsub(/^[[:space:]]+|[[:space:]]+$/, "", field) + if (field == key) { + value = $2 + gsub(/^[[:space:]]+|[[:space:]]+$/, "", value) + if (value ~ /^"/) { + sub(/^"/, "", value) + sub(/".*$/, "", value) + } + print value + exit + } + } + ' "$COLORS_TOML" +} + +sync_tmux_window_style() { + local background foreground + + [[ -f $COLORS_TOML ]] || return + + foreground=$(theme_color foreground) + background=$(theme_color background) + + [[ -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 +} + +sync_tmux_pane_colors() { + local pane_tty theme_osc + + [[ -f $COLORS_TOML ]] || return + + theme_osc=$(theme_osc_sequences) + [[ -n $theme_osc ]] || return + + while IFS= read -r pane_tty; do + [[ $pane_tty == /dev/pts/* ]] || continue + printf '%b' "$theme_osc" >"$pane_tty" 2>/dev/null || true + done < <(tmux list-panes -a -F "#{pane_tty}" 2>/dev/null | sort -u) +} + +signal_tmux_panes() { + local pane_tty tpgid + + while IFS= read -r pane_tty; do + [[ $pane_tty == /dev/pts/* ]] || continue + tpgid=$(ps -o tpgid= -t "${pane_tty#/dev/}" 2>/dev/null | awk 'NF && $1 > 0 { print $1; exit }') + [[ -n $tpgid ]] || continue + kill -WINCH "-$tpgid" 2>/dev/null || true + done < <(tmux list-panes -a -F "#{pane_tty}" 2>/dev/null | sort -u) +} + +refresh_tmux_clients() { + local client + + while IFS= read -r client; do + [[ -n $client ]] || continue + tmux refresh-client -t "$client" 2>/dev/null || true + done < <(tmux list-clients -F "#{client_name}" 2>/dev/null) +} + +sync_theme_environment +sync_colorfgbg +sync_tmux_window_style +sync_tmux_pane_colors +signal_tmux_panes +refresh_tmux_clients