feat: wezterm theme switching

This commit is contained in:
WinterEkisha committed 2026-11-18 16:25:13 -08:00
1 parent 58ec82092c
commit a3a9472799
4 files changed
+88 -44

No files matched your search

+67 -24
View File
@@ -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