.dotfiles/wezterm/.config/wezterm/theme-switcher.lua
Jeremie Fraeys 6ae3048c28 more changes
2025-05-16 03:23:25 -04:00

101 lines
3.6 KiB
Lua

local wezterm = require("wezterm")
local os = require("os")
local M = {}
-- Determine the appearance (light or dark)
local function get_appearance()
if wezterm.gui then
return wezterm.gui.get_appearance()
end
return os.getenv("DEFAULT_APPEARANCE") or "Dark" -- Default fallback
end
-- Set the color scheme and tab bar colors based on appearance
local function scheme_for_appearance(appearance, schemes)
return appearance:find("Dark") and schemes.dark or schemes.light
end
-- Color schemes for light and dark appearances
local color_schemes = {
dark = "Monokai (dark) (terminal.sexy)",
light = "Solarized (light) (terminal.sexy)",
}
-- Custom tab bar colors for light and dark themes
local tab_bar_colors = {
dark = {
active_tab = { bg_color = "#1c1f24", fg_color = "#f8f8f2" },
inactive_tab = { bg_color = "#3e4451", fg_color = "#a0a0a0" },
inactive_tab_hover = { bg_color = "#4e545e", fg_color = "#c0c0c0" },
new_tab = { bg_color = "#1c1f24", fg_color = "#f8f8f2" },
new_tab_hover = { bg_color = "#4e545e", fg_color = "#c0c0c0" },
},
light = {
active_tab = { bg_color = "#eee8d5", fg_color = "#073642" },
inactive_tab = { bg_color = "#93a1a1", fg_color = "#586e75" },
inactive_tab_hover = { bg_color = "#839496", fg_color = "#586e75" },
new_tab = { bg_color = "#eee8d5", fg_color = "#073642" },
new_tab_hover = { bg_color = "#839496", fg_color = "#586e75" },
},
}
-- Check if a command is available (more reliable implementation)
local function is_command_available(cmd)
wezterm.log_info(os.execute("/usr/bin/env bash -c 'command -v " .. cmd .. "'"))
wezterm.log_info("WezTerm PATH: " .. os.getenv("PATH"))
return os.execute("type " .. cmd .. " >/dev/null 2>&1") == 0
end
-- Function to update the Starship palette in the starship.toml file
local function update_starship_palette(palette_name)
if not is_command_available("starship") then
wezterm.log_info("Starship is not installed. Skipping palette update.")
return
end
local starship_config_path = os.getenv("XDG_CONFIG_HOME") and os.getenv("XDG_CONFIG_HOME") .. "/starship.toml"
or os.getenv("HOME") .. "/.config/starship.toml"
local file = io.open(starship_config_path, "r")
if not file then
wezterm.log_error("starship.toml file not found at: " .. starship_config_path)
return
end
file:close()
-- Determine sed command
local sed_inplace = wezterm.target_triple and wezterm.target_triple:find("darwin") and "sed -i ''" or "sed -i"
local replace_command =
string.format("%s 's/palette = .*/palette = \"%s\"/' %s", sed_inplace, palette_name, starship_config_path)
wezterm.log_info("Starship config path: " .. starship_config_path)
wezterm.log_info("Generated reaplce command: " .. replace_command)
-- Use `sd` if available for consistency
if is_command_available("sd") then
replace_command = string.format("sd 'palette = .+' 'palette = \"%s\"' %s", palette_name, starship_config_path)
end
-- Execute the replace command
local result = os.execute(replace_command)
if result ~= 0 then
wezterm.log_error("Failed to update Starship palette in " .. starship_config_path)
else
wezterm.log_info("Successfully updated Starship palette to: " .. palette_name)
end
end
-- Apply the configuration and update Starship palette
function M.apply_to_config(config)
local appearance = get_appearance()
config.color_scheme = scheme_for_appearance(appearance, color_schemes)
config.colors = { tab_bar = scheme_for_appearance(appearance, tab_bar_colors) }
-- Update the Starship palette based on appearance
local palette_name = appearance:find("Dark") and "monokai_pro" or "solarized_light"
wezterm.log_info("Starship palette set to: " .. palette_name)
update_starship_palette(palette_name)
end
return M