diff --git a/bin/omarchy-theme-set b/bin/omarchy-theme-set index d987d97aa675..66b75bcf0cf7 100755 --- a/bin/omarchy-theme-set +++ b/bin/omarchy-theme-set @@ -198,6 +198,7 @@ post_theme_commands=( omarchy-theme-set-gnome omarchy-theme-set-pi omarchy-theme-set-claude + omarchy-theme-set-grok omarchy-theme-set-browser omarchy-theme-set-vscode omarchy-theme-set-obsidian diff --git a/bin/omarchy-theme-set-grok b/bin/omarchy-theme-set-grok new file mode 100755 index 000000000000..f1953034e711 --- /dev/null +++ b/bin/omarchy-theme-set-grok @@ -0,0 +1,247 @@ +#!/bin/bash + +# omarchy:summary=Map the active Omarchy theme to a built-in Grok TUI theme +# omarchy:args=[--activate] +# omarchy:hidden=true + +# Grok's only live external retint path is theme=auto + OS light/dark (the +# system-appearance watcher). Writing an explicit [ui].theme only applies on +# the next Grok start. So when following, we set theme=auto, pick the closest +# built-in for auto_dark_theme / auto_light_theme, and rely on theme-set-gnome +# flipping the desktop color-scheme for live light↔dark. +# +# Follow mode is sticky until the user leaves auto: +# - omarchy-theme-set-grok --activate (or install/user/theme.sh) starts it +# - each Omarchy theme switch updates mappings while [ui].theme is "auto" +# - if they pick a fixed Grok theme, we stop and leave them alone +# - --activate again opts back in + +set -euo pipefail + +GROK_HOME="${GROK_HOME:-$HOME/.grok}" +GROK_CONFIG_PATH="$GROK_HOME/config.toml" +GROK_FOLLOW_FLAG="$HOME/.local/state/omarchy/grok-theme-follow" +CURRENT_THEME_PATH="$HOME/.local/state/omarchy/current/theme" +GROK_ACTIVATE=0 + +usage() { + echo "Usage: omarchy-theme-set-grok [--activate]" +} + +for arg in "$@"; do + case "$arg" in + --activate) + GROK_ACTIVATE=1 + ;; + -h | --help) + usage + exit 0 + ;; + *) + usage >&2 + exit 1 + ;; + esac +done + +# No-op when Grok is not installed. Finalize runs --activate unconditionally +# under set -e, so this must not exit 1 (otherwise btop theme linking is skipped). +# Interactive hand-runs still get a stderr note when stdout is a TTY. +if [[ ! -d $GROK_HOME ]]; then + if (( GROK_ACTIVATE == 1 )) && [[ -t 1 ]]; then + echo "Grok config directory missing: $GROK_HOME" >&2 + echo "Install Grok first, then re-run omarchy-theme-set-grok --activate." >&2 + fi + exit 0 +fi + +# Read [ui].theme (or legacy top-level theme) from config.toml. +current_ui_theme() { + local config="$GROK_CONFIG_PATH" + + [[ -f $config ]] || return 0 + + python3 - "$config" <<'PY' +import pathlib +import sys +import tomllib + +path = pathlib.Path(sys.argv[1]) +try: + data = tomllib.loads(path.read_text()) +except tomllib.TOMLDecodeError: + print("") + raise SystemExit(0) + +ui = data.get("ui") +if isinstance(ui, dict) and "theme" in ui: + print(ui["theme"] if ui["theme"] is not None else "") +elif "theme" in data: + # Legacy top-level theme key + print(data["theme"] if data["theme"] is not None else "") +else: + print("") +PY +} + +stop_following() { + rm -f "$GROK_FOLLOW_FLAG" +} + +start_following() { + mkdir -p "$(dirname "$GROK_FOLLOW_FLAG")" + : >"$GROK_FOLLOW_FLAG" +} + +if (( GROK_ACTIVATE == 1 )); then + start_following +elif [[ ! -f $GROK_FOLLOW_FLAG ]]; then + exit 0 +else + # Still following only while Grok remains on auto (or theme unset). + theme_now=$(current_ui_theme) + if [[ -n $theme_now && $theme_now != "auto" && $theme_now != "system" ]]; then + stop_following + exit 0 + fi +fi + +omarchy_theme_slug() { + local path + + if [[ -f $HOME/.local/state/omarchy/current/theme.name ]]; then + tr -d '[:space:]' <"$HOME/.local/state/omarchy/current/theme.name" + return + fi + + path=$(readlink -f "$CURRENT_THEME_PATH" 2>/dev/null || true) + if [[ -n $path ]]; then + basename "$path" + return + fi + + printf '%s' "" +} + +# Closest dark built-in for the current Omarchy slug (never "auto" / "grokday"). +map_grok_dark_theme() { + local slug + + slug=$(omarchy_theme_slug) + + case "$slug" in + tokyo-night | tokyonight | tokyo | nord | catppuccin | kanagawa | everforest | ethereal) + printf '%s' "tokyonight" + ;; + rose-pine | rosepine | rose-pine-moon | rosepine-moon) + printf '%s' "rosepine-moon" + ;; + matte-black | vantablack | hackerman | osaka-jade | ristretto | solitude) + printf '%s' "oscura-midnight" + ;; + *) + printf '%s' "groknight" + ;; + esac +} + +# Write [ui] theme + auto_* keys, preserving the rest of config.toml. +write_grok_ui_keys() { + local config="$GROK_CONFIG_PATH" + local theme="$1" + local auto_dark="$2" + local auto_light="$3" + + mkdir -p "$(dirname "$config")" + + python3 - "$config" "$theme" "$auto_dark" "$auto_light" <<'PY' +import pathlib +import re +import sys + +path = pathlib.Path(sys.argv[1]) +wanted = { + "theme": sys.argv[2], + "auto_dark_theme": sys.argv[3], + "auto_light_theme": sys.argv[4], +} + +text = path.read_text() if path.exists() else "" +lines = text.splitlines(keepends=True) +out = [] +section = None +ui_seen = False +written = {k: False for k in wanted} + +def emit_missing_ui_keys(): + for key, value in wanted.items(): + if not written[key]: + out.append(f'{key} = "{value}"\n') + written[key] = True + +for line in lines: + stripped = line.strip() + if stripped.startswith("[") and stripped.endswith("]"): + if section == "ui": + emit_missing_ui_keys() + name = stripped[1:-1].strip() + section = name + if name == "ui": + ui_seen = True + out.append(line) + continue + + m = re.match(r"^([A-Za-z0-9_]+)\s*=", stripped) + if m and section == "ui" and m.group(1) in wanted: + key = m.group(1) + out.append(f'{key} = "{wanted[key]}"\n') + written[key] = True + continue + + if m and section is None and m.group(1) == "theme": + # Legacy top-level theme — drop; live under [ui]. + continue + + out.append(line) + +if section == "ui": + emit_missing_ui_keys() + +if not ui_seen: + body = "".join(out) + if body and not body.endswith("\n"): + out.append("\n") + if out and out[-1].strip() != "": + out.append("\n") + out.append("[ui]\n") + for key, value in wanted.items(): + out.append(f'{key} = "{value}"\n') + +tmp = path.with_suffix(path.suffix + ".tmp") +tmp.write_text("".join(out)) +tmp.replace(path) +PY +} + +dark_theme=$(map_grok_dark_theme) +light_theme="grokday" + +# theme=auto so running sessions follow OS light/dark (Omarchy sets that via GNOME). +# auto_dark_theme carries the closest dark built-in for the current Omarchy slug. +write_grok_ui_keys "auto" "$dark_theme" "$light_theme" + +# Interactive only — finalize/install redirect stdout and should stay quiet +# (same as pi's silent activate). Config + follow flag are still written above. +if (( GROK_ACTIVATE == 1 )) && [[ -t 1 ]]; then + echo "Grok will follow Omarchy light/dark (auto mode)." + echo " auto_dark_theme = $dark_theme" + echo " auto_light_theme = $light_theme" + echo " config: $GROK_CONFIG_PATH" + echo + echo "Restart Grok once so it enters theme=auto. After that, light↔dark" + echo "desktop switches retint live. Same-polarity maps (e.g. tokyo-night" + echo "vs groknight) still need a restart or /theme to apply." + echo + echo "If you pick a fixed Grok theme later, Omarchy stops updating Grok until" + echo "you run: omarchy-theme-set-grok --activate" +fi diff --git a/install/user/theme.sh b/install/user/theme.sh index e318f5d6cb9a..b176680da2fb 100644 --- a/install/user/theme.sh +++ b/install/user/theme.sh @@ -10,6 +10,7 @@ if [[ ! -s $HOME/.local/state/omarchy/current/theme.name ]]; then fi fi omarchy-theme-set-pi --activate +omarchy-theme-set-grok --activate mkdir -p ~/.config/btop/themes ln -snf "$HOME/.local/state/omarchy/current/theme/btop.theme" ~/.config/btop/themes/current.theme diff --git a/test/cli b/test/cli index e8e5363c2bfd..2378cd4977ce 100755 --- a/test/cli +++ b/test/cli @@ -7,6 +7,7 @@ CLI="$ROOT/bin/omarchy" TMPDIR="" PI_TMPDIR="" THEME_TMPDIR="" +GROK_TMPDIR="" export PATH="$ROOT/bin:$PATH" @@ -37,6 +38,7 @@ cleanup() { [[ -n $TMPDIR && -d $TMPDIR ]] && rm -rf "$TMPDIR" [[ -n $PI_TMPDIR && -d $PI_TMPDIR ]] && rm -rf "$PI_TMPDIR" [[ -n $THEME_TMPDIR && -d $THEME_TMPDIR ]] && rm -rf "$THEME_TMPDIR" + [[ -n $GROK_TMPDIR && -d $GROK_TMPDIR ]] && rm -rf "$GROK_TMPDIR" } trap cleanup EXIT @@ -381,6 +383,127 @@ HOME="$PI_TMPDIR" CLAUDE_CONFIG_DIR="$CLAUDE_TEST_CONFIG" "$ROOT/bin/omarchy-the jq -e '.theme == "custom:omarchy" and .model == "keep-me"' "$CLAUDE_TEST_CONFIG/settings.json" >/dev/null pass "claude theme activation preserves configured settings" +GROK_TMPDIR=$(mktemp -d) +GROK_HOME_DIR="$GROK_TMPDIR/grok" +mkdir -p \ + "$GROK_TMPDIR/.local/state/omarchy/current/theme" \ + "$GROK_HOME_DIR" +printf 'lumon\n' >"$GROK_TMPDIR/.local/state/omarchy/current/theme.name" +printf 'mode = "dark"\n' >"$GROK_TMPDIR/.local/state/omarchy/current/theme/colors.toml" + +HOME="$GROK_TMPDIR" GROK_HOME="$GROK_HOME_DIR" "$ROOT/bin/omarchy-theme-set-grok" +[[ ! -f $GROK_HOME_DIR/config.toml ]] || fail "grok theme sync is a no-op before activate" +[[ ! -f $GROK_TMPDIR/.local/state/omarchy/grok-theme-follow ]] || fail "grok follow flag is not created before activate" +pass "grok theme sync is a no-op before activate" + +# Missing Grok home must not abort finalize (bash -e + theme.sh --activate). +set +e +HOME="$GROK_TMPDIR" GROK_HOME="$GROK_TMPDIR/no-such-grok" "$ROOT/bin/omarchy-theme-set-grok" --activate +activate_missing_status=$? +set -e +(( activate_missing_status == 0 )) || fail "grok activate is a no-op when Grok home is missing" +[[ ! -f $GROK_TMPDIR/.local/state/omarchy/grok-theme-follow ]] || fail "grok activate without home leaves no follow flag" +pass "grok activate is a no-op when Grok home is missing" + +HOME="$GROK_TMPDIR" GROK_HOME="$GROK_HOME_DIR" "$ROOT/bin/omarchy-theme-set-grok" --activate +[[ -f $GROK_HOME_DIR/config.toml ]] || fail "grok activate writes config.toml" +[[ -f $GROK_TMPDIR/.local/state/omarchy/grok-theme-follow ]] || fail "grok activate creates follow flag" +awk ' + /^\[ui\]$/ { in_ui = 1; next } + /^\[/ { in_ui = 0 } + in_ui && $0 == "theme = \"auto\"" { theme = 1 } + in_ui && $0 == "auto_dark_theme = \"groknight\"" { dark = 1 } + in_ui && $0 == "auto_light_theme = \"grokday\"" { light = 1 } + END { exit(theme && dark && light ? 0 : 1) } +' "$GROK_HOME_DIR/config.toml" || fail "grok activate enables auto mode with mapped dark/light themes" +pass "grok activate enables auto mode with mapped dark/light themes" + +# Fixture mirrors a real Grok config: marketplace array-of-tables + [ui] siblings. +cat >"$GROK_HOME_DIR/config.toml" <<'EOF' +[cli] +installer = "internal" + +[marketplace] +default_skills_installs_purged = true +official_marketplace_auto_installed = true + +[[marketplace.sources]] +name = "xAI Official" +git = "https://github.com/xai-org/plugin-marketplace.git" + +[ui] +max_thoughts_width = 120 +theme = "auto" +fork_secondary_model = "grok-4.5" +yolo = false +EOF +HOME="$GROK_TMPDIR" GROK_HOME="$GROK_HOME_DIR" "$ROOT/bin/omarchy-theme-set-grok" +awk ' + /^\[cli\]$/ { in_cli = 1; in_ui = 0; in_src = 0; next } + /^\[marketplace\]$/ { in_mkt = 1; in_cli = 0; in_ui = 0; in_src = 0; next } + /^\[\[marketplace\.sources\]\]$/ { in_src = 1; in_mkt = 0; in_cli = 0; in_ui = 0; next } + /^\[ui\]$/ { in_ui = 1; in_cli = 0; in_mkt = 0; in_src = 0; next } + /^\[/ { in_ui = 0; in_cli = 0; in_mkt = 0; in_src = 0 } + in_cli && $0 == "installer = \"internal\"" { cli = 1 } + in_mkt && $0 == "official_marketplace_auto_installed = true" { mkt = 1 } + in_src && $0 == "name = \"xAI Official\"" { src_name = 1 } + in_src && index($0, "plugin-marketplace") { src_git = 1 } + in_ui && $0 == "max_thoughts_width = 120" { width = 1 } + in_ui && $0 == "theme = \"auto\"" { theme = 1 } + in_ui && $0 == "auto_dark_theme = \"groknight\"" { dark = 1 } + in_ui && $0 == "fork_secondary_model = \"grok-4.5\"" { fork = 1 } + in_ui && $0 == "yolo = false" { yolo = 1 } + END { exit(cli && mkt && src_name && src_git && width && theme && dark && fork && yolo ? 0 : 1) } +' "$GROK_HOME_DIR/config.toml" || fail "grok theme sync preserves unrelated config keys while following" +pass "grok theme sync preserves unrelated config keys while following" + +printf 'tokyo-night\n' >"$GROK_TMPDIR/.local/state/omarchy/current/theme.name" +HOME="$GROK_TMPDIR" GROK_HOME="$GROK_HOME_DIR" "$ROOT/bin/omarchy-theme-set-grok" +awk ' + /^\[ui\]$/ { in_ui = 1; next } + /^\[/ { in_ui = 0 } + in_ui && $0 == "auto_dark_theme = \"tokyonight\"" { dark = 1 } + in_ui && $0 == "theme = \"auto\"" { theme = 1 } + END { exit(dark && theme ? 0 : 1) } +' "$GROK_HOME_DIR/config.toml" || fail "grok theme sync maps tokyo-night to tokyonight" +pass "grok theme sync maps tokyo-night to tokyonight" + +printf 'nord\n' >"$GROK_TMPDIR/.local/state/omarchy/current/theme.name" +HOME="$GROK_TMPDIR" GROK_HOME="$GROK_HOME_DIR" "$ROOT/bin/omarchy-theme-set-grok" +grep -Fx 'auto_dark_theme = "tokyonight"' "$GROK_HOME_DIR/config.toml" >/dev/null || \ + fail "grok theme sync maps nord to tokyonight" +pass "grok theme sync maps nord to tokyonight" + +# User picks a fixed Grok theme — stop following and leave it alone. +# Trailing comment exercises tomllib read (must not treat as unset/auto). +cat >"$GROK_HOME_DIR/config.toml" <<'EOF' +[ui] +theme = "oscura-midnight" # manual pick +auto_dark_theme = "tokyonight" +auto_light_theme = "grokday" +EOF +HOME="$GROK_TMPDIR" GROK_HOME="$GROK_HOME_DIR" "$ROOT/bin/omarchy-theme-set-grok" +[[ ! -f $GROK_TMPDIR/.local/state/omarchy/grok-theme-follow ]] || fail "grok follow stops when theme is not auto" +awk ' + /^\[ui\]$/ { in_ui = 1; next } + /^\[/ { in_ui = 0 } + in_ui && $0 ~ /^theme = "oscura-midnight"/ { theme = 1 } + in_ui && $0 == "auto_dark_theme = \"tokyonight\"" { dark = 1 } + END { exit(theme && dark ? 0 : 1) } +' "$GROK_HOME_DIR/config.toml" || fail "grok theme sync leaves a manual theme untouched" +pass "grok theme sync leaves a manual theme untouched" + +# Re-activate resumes following. +HOME="$GROK_TMPDIR" GROK_HOME="$GROK_HOME_DIR" "$ROOT/bin/omarchy-theme-set-grok" --activate >/dev/null +[[ -f $GROK_TMPDIR/.local/state/omarchy/grok-theme-follow ]] || fail "grok activate resumes follow" +awk ' + /^\[ui\]$/ { in_ui = 1; next } + /^\[/ { in_ui = 0 } + in_ui && $0 == "theme = \"auto\"" { theme = 1 } + END { exit(theme ? 0 : 1) } +' "$GROK_HOME_DIR/config.toml" || fail "grok re-activate restores theme=auto" +pass "grok re-activate restores theme=auto" + THEME_TMPDIR=$(mktemp -d) OMARCHY_PATH="$ROOT" HOME="$THEME_TMPDIR" OMARCHY_THEME_HEADLESS=1 "$ROOT/bin/omarchy-theme-set" "Tokyo Night" background_path=$(readlink -f "$THEME_TMPDIR/.local/state/omarchy/current/background" 2>/dev/null || true) diff --git a/test/shell.d/user-theme-test.sh b/test/shell.d/user-theme-test.sh index 0330f931de38..8b10c368dcd2 100644 --- a/test/shell.d/user-theme-test.sh +++ b/test/shell.d/user-theme-test.sh @@ -14,25 +14,39 @@ printf '%s\n' "$*" >>"$OMARCHY_TEST_THEME_CALLS" SH chmod +x "$mock_bin/omarchy-theme-set" -for command in omarchy-theme-set-pi; do - cat >"$mock_bin/$command" <<'SH' +# Record name + args so we assert finalize always activates pi/grok follow mode. +for command in omarchy-theme-set-pi omarchy-theme-set-grok; do + cat >"$mock_bin/$command" <>"\$OMARCHY_TEST_ACTIVATE_CALLS" SH chmod +x "$mock_bin/$command" done calls="$test_tmp/theme-calls" +activate_calls="$test_tmp/activate-calls" touch "$test_tmp/home/.config/chromium/SingletonLock" -HOME="$test_tmp/home" PATH="$mock_bin:$PATH" OMARCHY_TEST_THEME_CALLS="$calls" \ +: >"$activate_calls" +HOME="$test_tmp/home" PATH="$mock_bin:$PATH" \ + OMARCHY_TEST_THEME_CALLS="$calls" OMARCHY_TEST_ACTIVATE_CALLS="$activate_calls" \ bash "$ROOT/install/user/theme.sh" grep -Fx 'Tokyo Night' "$calls" >/dev/null || fail "user theme setup seeds Tokyo Night when no theme exists" [[ -f $test_tmp/home/.config/chromium/SingletonLock ]] || fail "runtime user theme setup preserves Chromium's singleton lock" +grep -Fx 'omarchy-theme-set-pi --activate' "$activate_calls" >/dev/null || \ + fail "user theme setup activates pi theme follow" +grep -Fx 'omarchy-theme-set-grok --activate' "$activate_calls" >/dev/null || \ + fail "user theme setup activates grok theme follow" : >"$calls" +: >"$activate_calls" printf 'Solitude\n' >"$test_tmp/home/.local/state/omarchy/current/theme.name" -HOME="$test_tmp/home" PATH="$mock_bin:$PATH" OMARCHY_TEST_THEME_CALLS="$calls" \ +HOME="$test_tmp/home" PATH="$mock_bin:$PATH" \ + OMARCHY_TEST_THEME_CALLS="$calls" OMARCHY_TEST_ACTIVATE_CALLS="$activate_calls" \ bash "$ROOT/install/user/theme.sh" [[ ! -s $calls ]] || fail "user theme setup preserves an existing theme" +grep -Fx 'omarchy-theme-set-pi --activate' "$activate_calls" >/dev/null || \ + fail "user theme setup re-activates pi when a theme already exists" +grep -Fx 'omarchy-theme-set-grok --activate' "$activate_calls" >/dev/null || \ + fail "user theme setup re-activates grok when a theme already exists" pass "user theme setup only seeds the default theme once"