theme auto switch and clarity changes

This commit is contained in:
Jeremie Fraeys 2024-12-18 21:26:00 -05:00
parent a867c70a02
commit f47fc99ebd
2 changed files with 28 additions and 30 deletions

View file

@ -8,16 +8,12 @@ local function get_appearance()
if wezterm.gui then
return wezterm.gui.get_appearance()
end
return "Dark"
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)
if appearance:find("Dark") then
return schemes.dark
else
return schemes.light
end
return appearance:find("Dark") and schemes.dark or schemes.light
end
-- Color schemes for light and dark appearances
@ -44,57 +40,59 @@ local tab_bar_colors = {
},
}
-- Check if a command is available (uses POSIX standard)
-- Check if a command is available (more reliable implementation)
local function is_command_available(cmd)
local handle = io.popen("command -v " .. cmd .. " >/dev/null 2>&1; echo $?")
if handle == nil then
return false
end
local result = handle:read("*a"):gsub("%s+", "")
handle:close()
return tonumber(result) == 0
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.")
wezterm.log_info("Starship is not installed. Skipping palette update.")
return
end
local starship_config_path = os.getenv("XDG_CONFIG_HOME") .. "/starship.toml"
if not starship_config_path or starship_config_path == "" then
wezterm.log_error("XDG_CONFIG_HOME is not set or starship.toml not found.")
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()
local replace_command
-- 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)
-- 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)
else
local sed_inplace = wezterm.target_triple:find("darwin") and "sed -i ''" or "sed -i"
replace_command =
string.format("%s 's/palette = .*/palette = \"%s\"/' %s", sed_inplace, 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.")
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()
wezterm.log_info("Appearance detected: " .. appearance)
config.color_scheme = scheme_for_appearance(appearance, color_schemes)
wezterm.log_info("Using color scheme: " .. config.color_scheme)
config.colors = { tab_bar = scheme_for_appearance(appearance, tab_bar_colors) }
-- Update the Starship palette based on appearance
if appearance:find("Dark") then
update_starship_palette("monokai_pro")
else
update_starship_palette("solarized_light")
end
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

View file

@ -6,7 +6,7 @@ local act = wezterm.action
local theme = require("theme-switcher")
-- local kill_workspace = require("kill-workspace")
local sessionizer = require("sessionizer.plugin.init")
local gpu_adapters = require("gpu-adapter")
-- local gpu_adapters = require("gpu-adapter")
local config = {}