#!/usr/bin/env bash # set-wezterm-profile.sh — SessionStart hook: emit WezTerm theme escape sequence # # Derives the profile name from CLAUDE_CONFIG_DIR (e.g. ~/.claude-octopus → octopus) # and emits a SetUserVar escape sequence so WezTerm fires its user-var-changed # event and applies the matching theme from wezterm.lua. # # Hook subprocesses have no controlling terminal, so the escape sequence is # written to the pts device found by walking up the process tree. # Safe in non-WezTerm terminals (escape sequence is silently ignored). # # Usage (settings.json): # "SessionStart": [{ "hooks": [{ "type": "command", # "command": "~/.claude/hooks/set-wezterm-profile.sh" }] }] set -euo pipefail profile_dir="${CLAUDE_CONFIG_DIR:-$HOME/.claude}" profile_name="${profile_dir##*/.claude-}" # No profile suffix means this is the default profile — nothing to do if [[ "$profile_name" == "$profile_dir" ]]; then exit 0 fi # Emit the escape sequence to the terminal device. # Hook subprocesses have no controlling terminal (/dev/tty unavailable), # so walk up the process tree to find the first ancestor with a pts device. tty_device="" pid=$$ while [[ "$pid" -gt 1 ]]; do pts=$(ps -o tty= -p "$pid" 2>/dev/null | tr -d ' ') if [[ -n "$pts" && "$pts" != "?" && -w "/dev/$pts" ]]; then tty_device="/dev/$pts" break fi pid=$(ps -o ppid= -p "$pid" 2>/dev/null | tr -d ' ') done if [[ -n "$tty_device" ]]; then printf '\e]1337;SetUserVar=%s=%s\a' CLAUDE_PROFILE \ "$(printf '%s' "$profile_name" | base64)" >"$tty_device" 2>/dev/null || true fi