Files
omarchy/bin/omarchy-theme-set-vscode

48 lines
1.8 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# Note: We cannot use `jq` to update settings.json because its JSONC (allows comments),
# which jq doesnt support.
# Parameters: EDITOR_CMD SETTINGS_PATH SKIP_FLAG EDITOR_NAME
EDITOR_CMD="${1:-code}"
SETTINGS_PATH="${2:-$HOME/.config/Code/User/settings.json}"
SKIP_FLAG="${3:-$HOME/.local/state/omarchy/toggles/skip-vscode-theme-changes}"
EDITOR_NAME="${4:-VS Code}"
VS_CODE_THEME="$HOME/.config/omarchy/current/theme/vscode.json"
if omarchy-cmd-present "$EDITOR_CMD" && [[ ! -f "$SKIP_FLAG" ]]; then
if [[ -f "$VS_CODE_THEME" ]]; then
theme_name=$(jq -r '.name' "$VS_CODE_THEME")
extension=$(jq -r '.extension' "$VS_CODE_THEME")
# Install $EDITOR_NAME theme extension
if [[ -n "$extension" ]] && ! "$EDITOR_CMD" --list-extensions | grep -Fxq "$extension"; then
"$EDITOR_CMD" --install-extension "$extension" >/dev/null
fi
# Create config file if there isn't already one
mkdir -p "$(dirname "$SETTINGS_PATH")"
if [[ ! -f "$SETTINGS_PATH" ]]; then
printf '{\n}\n' >"$SETTINGS_PATH"
fi
# Create a `workbench.colorTheme` entry in settings.
if ! grep -q '"workbench.colorTheme"' "$SETTINGS_PATH"; then
# Insert `"workbench.colorTheme": "",` immediately after the first `{`
# Use sed's first-match range (0,/{/) to only replace the first `{`
sed -i --follow-symlinks -E '0,/\{/{s/\{/{\ "workbench.colorTheme": "",/}' "$SETTINGS_PATH"
fi
# Update theme
sed -i --follow-symlinks -E \
"s/(\"workbench.colorTheme\"[[:space:]]*:[[:space:]]*\")[^\"]*(\")/\1$theme_name\2/" \
"$SETTINGS_PATH"
else
# Remove theme from settings.json when the theme doesn't have $EDITOR_NAME support
if [[ -f "$SETTINGS_PATH" ]]; then
sed -i --follow-symlinks -E 's/\"workbench\.colorTheme\"[[:space:]]*:[^,}]*,?//' "$SETTINGS_PATH"
fi
fi
fi