From a3a9472799cbe2f0593db25060462e05d3ba6a64 Mon Sep 17 00:00:00 2001 From: winter <149337190+WinterEkisha@users.noreply.github.com> Date: Wed, 18 Nov 2026 16:25:13 -0800 Subject: [PATCH] feat: wezterm theme switching --- home/Winter/common/features/nvimConf.nix | 34 ++++--- .../common/features/wezterm/wezterm.lua | 91 ++++++++++++++----- hosts/Winter/Reconix/configuration.nix | 4 - hosts/Winter/common/example.nix | 3 - 4 files changed, 88 insertions(+), 44 deletions(-) diff --git a/home/Winter/common/features/nvimConf.nix b/home/Winter/common/features/nvimConf.nix index f776554..f7855d9 100644 --- a/home/Winter/common/features/nvimConf.nix +++ b/home/Winter/common/features/nvimConf.nix @@ -33,16 +33,32 @@ relativenumber = true; }; + # Define the global helper function so you can run :lua set_wezterm_theme('solid') anytime + extraConfigLua = '' + _G.set_wezterm_theme = function(theme) + local val = theme or "" + local b64 = vim.base64.encode(val) + local seq = string.format('\x1b]1337;SetUserVar=%s=%s\x07', 'WEZTERM_THEME', b64) + + if vim.env.TMUX then + seq = string.format('\x1bPtmux;\x1b%s\x1b\\', seq) + end + + pcall(vim.fn.chansend, vim.v.stderr, seq) + end + ''; + autoGroups = { WezTermUserVar = { clear = true; }; }; + autoCmd = [ # --- Sets tab spacing for file types --- { event = [ "FileType" ]; - pattern = [ "html" "css" "javascript" "typescript" "nix" "lua" ]; + pattern = [ "html" "css" "javascript" "typescript" "nix" "lua" ]; callback = { __raw = '' function() @@ -53,34 +69,26 @@ ''; }; } + # --- Set default WezTerm profile on entry/resume --- { event = [ "UIEnter" "VimResume" ]; group = "WezTermUserVar"; callback = { __raw = '' function() - local b64 = vim.base64.encode('true') - local seq = string.format('\x1b]1337;SetUserVar=%s=%s\x07', 'IS_NVIM', b64) - if vim.env.TMUX then - seq = string.format('\x1bPtmux;\x1b%s\x1b\\', seq) - end - pcall(vim.fn.chansend, vim.v.stderr, seq) + set_wezterm_theme('nvim') end ''; }; } + # --- Clear WezTerm profile (revert to base config) on exit/suspend --- { event = [ "VimLeavePre" "VimSuspend" ]; group = "WezTermUserVar"; callback = { __raw = '' function() - local b64 = vim.base64.encode('false') - local seq = string.format('\x1b]1337;SetUserVar=%s=%s\x07', 'IS_NVIM', b64) - if vim.env.TMUX then - seq = string.format('\x1bPtmux;\x1b%s\x1b\\', seq) - end - pcall(vim.fn.chansend, vim.v.stderr, seq) + set_wezterm_theme("") end ''; }; diff --git a/home/Winter/common/features/wezterm/wezterm.lua b/home/Winter/common/features/wezterm/wezterm.lua index 240153a..5dc12ce 100644 --- a/home/Winter/common/features/wezterm/wezterm.lua +++ b/home/Winter/common/features/wezterm/wezterm.lua @@ -4,7 +4,7 @@ local config = wezterm.config_builder() -- Launch fish directly instead of your system login shell config.default_prog = { 'fish', '-l' } --- Appearance & Tab Bar +-- Base Appearance & Tab Bar (Default / Fallback state) config.window_background_opacity = 0.60 config.wayland_window_background_blur = true config.hide_tab_bar_if_only_one_tab = true @@ -21,47 +21,90 @@ config.colors = { foreground = '#dddddd', } -local bg_image_path = wezterm.config_dir .. '/texture.png' +-- Helper to safely check if a file exists before applying it +local function file_exists(path) + if not path then + return false + end + local f = io.open(path, 'r') + if f then + f:close() + return true + end + return false +end -local function sync_nvim_appearance(window, pane) +-- Define arbitrary theme profiles here. +-- Keys correspond to the value of the WEZTERM_THEME user variable. +local profiles = { + nvim = { + colors = { foreground = '#ffffff' }, + window_background_image = wezterm.config_dir .. '/texture.png', + window_background_opacity = 1.0, + }, + solid = { + colors = { foreground = '#eeeeee', background = '#1e1e2e' }, + window_background_opacity = 1.0, + }, + light = { + colors = { foreground = '#111111', background = '#f5f5f5' }, + window_background_opacity = 0.95, + }, +} + +-- Track the active profile per window so we don't trigger redundant reloads +local active_window_profiles = {} + +local function sync_appearance(window, pane) if not pane then return end local user_vars = pane:get_user_vars() - local is_nvim = user_vars.IS_NVIM == 'true' - local overrides = window:get_config_overrides() or {} + local target_profile = user_vars.WEZTERM_THEME or 'default' + local win_id = window:window_id() - -- Avoid redundant config reloads if state hasn't changed - local currently_active = overrides.window_background_image ~= nil - if is_nvim == currently_active then + -- Skip if this window is already using the requested profile + if active_window_profiles[win_id] == target_profile then return end - if is_nvim then - overrides.colors = { - foreground = '#ffffff', - } - overrides.window_background_opacity = 1.0 - overrides.window_background_image = bg_image_path - else - overrides.colors = nil - overrides.window_background_opacity = nil - overrides.window_background_image = nil - end + local profile_settings = profiles[target_profile] - window:set_config_overrides(overrides) + if profile_settings then + local overrides = {} + + -- Copy arbitrary settings from the chosen profile + for key, value in pairs(profile_settings) do + overrides[key] = value + end + + -- Fallback: If a background image is specified, verify the file exists. + -- If missing, drop the image override and restore base opacity so text stays readable. + if overrides.window_background_image and not file_exists(overrides.window_background_image) then + wezterm.log_warn('Background image not found: ' .. overrides.window_background_image) + overrides.window_background_image = nil + overrides.window_background_opacity = nil + end + + window:set_config_overrides(overrides) + active_window_profiles[win_id] = target_profile + else + -- Reset to base config if the variable is unset, empty, or unrecognized + window:set_config_overrides({}) + active_window_profiles[win_id] = 'default' + end end wezterm.on('user-var-changed', function(window, pane, name, _value) - if name == 'IS_NVIM' then - sync_nvim_appearance(window, pane) + if name == 'WEZTERM_THEME' then + sync_appearance(window, pane) end end) --- Also sync when switching tabs or closing panes +-- Keep state synced when switching tabs or closing panes wezterm.on('update-status', function(window, pane) - sync_nvim_appearance(window, pane) + sync_appearance(window, pane) end) return config diff --git a/hosts/Winter/Reconix/configuration.nix b/hosts/Winter/Reconix/configuration.nix index 2448242..d24841a 100644 --- a/hosts/Winter/Reconix/configuration.nix +++ b/hosts/Winter/Reconix/configuration.nix @@ -25,7 +25,6 @@ in ../common/users/Winter/default.nix ../common/features/DesktopNiri.nix - ../common/features/DesktopKDEPlasma.nix ../common/features/Applications.nix #../common/features/servers/minecraft.nix @@ -61,9 +60,6 @@ in home-manager = { extraSpecialArgs = { inherit inputs; }; backupFileExtension = "backup"; - users = { - Winter = import ../../home/Winter/Winter/${hostname}.nix; - }; }; environment.systemPackages = with pkgs; [ diff --git a/hosts/Winter/common/example.nix b/hosts/Winter/common/example.nix index 36ffcdc..38259b7 100644 --- a/hosts/Winter/common/example.nix +++ b/hosts/Winter/common/example.nix @@ -60,9 +60,6 @@ in home-manager = { extraSpecialArgs = { inherit inputs; }; backupFileExtension = "backup"; - users = { - EXAMPLE = import ../../../home/EXAMPLE/EXAMPLE/${hostname}.nix; # FIXME - }; }; environment.systemPackages = with pkgs; [