2021-09-16 20:32:29 +00:00
|
|
|
-- Update colors based on environment variables
|
2021-09-16 01:13:17 +00:00
|
|
|
function _G.update_colors()
|
2021-12-01 17:48:53 +00:00
|
|
|
local function maybe_set(scope, name, val, force)
|
|
|
|
force = force or false
|
2021-12-15 23:24:05 +00:00
|
|
|
local changed = vim[scope][name] ~= val
|
|
|
|
if changed or force then
|
|
|
|
if scope == "g" and name == "colors_name" then
|
|
|
|
-- Colorscheme is different. Use this instead of setting colors_name directly
|
2021-12-17 04:51:13 +00:00
|
|
|
-- Try to set the colorscheme. If not loaded, skip the error
|
|
|
|
local status, _ = pcall(vim.cmd, "colorscheme " .. val)
|
|
|
|
if not status then
|
|
|
|
vim.notify("Failed to set colorscheme to " .. val .. ". Maybe it's not yet loaded")
|
|
|
|
end
|
2021-12-15 23:24:05 +00:00
|
|
|
else
|
|
|
|
vim[scope][name] = val
|
|
|
|
end
|
|
|
|
return changed
|
2021-12-01 17:48:53 +00:00
|
|
|
end
|
2021-09-01 17:03:05 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Set colorscheme based on env
|
2021-09-16 20:32:29 +00:00
|
|
|
local utils = require("utils")
|
2023-06-14 00:27:12 +00:00
|
|
|
local default_color = "wombat_lush"
|
2021-09-01 17:03:05 +00:00
|
|
|
local env_color = utils.env_default("VIM_COLOR", default_color)
|
|
|
|
env_color = utils.env_default("NVIM_COLOR", env_color)
|
|
|
|
|
|
|
|
-- Read dark mode
|
2021-09-16 20:32:29 +00:00
|
|
|
local mode = utils.env_default("IS_DARKMODE", "dark")
|
2021-09-01 17:03:05 +00:00
|
|
|
if vim.g.is_mac == 1 then
|
|
|
|
local cmd = "defaults read -g AppleInterfaceStyle 2>/dev/null || echo Light"
|
|
|
|
mode = vim.fn.system(cmd):gsub("\n", ""):lower()
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Update background and theme
|
|
|
|
local change = false
|
|
|
|
if mode == "dark" then
|
|
|
|
env_color = utils.env_default("VIM_COLOR_DARK", env_color)
|
|
|
|
env_color = utils.env_default("NVIM_COLOR_DARK", env_color)
|
|
|
|
change = maybe_set("o", "background", "dark")
|
2021-12-15 23:24:05 +00:00
|
|
|
change = maybe_set("g", "colors_name", env_color, false) or change
|
2021-09-01 17:03:05 +00:00
|
|
|
elseif mode == "light" then
|
|
|
|
env_color = utils.env_default("VIM_COLOR_LIGHT", env_color)
|
|
|
|
env_color = utils.env_default("NVIM_COLOR_LIGHT", env_color)
|
|
|
|
change = maybe_set("o", "background", "light")
|
2021-12-15 23:24:05 +00:00
|
|
|
change = maybe_set("g", "colors_name", env_color, false) or change
|
2021-09-01 17:03:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Update status line theme
|
2021-09-16 20:32:29 +00:00
|
|
|
if change then
|
|
|
|
if vim.fn.exists(":AirlineRefresh") == 1 then
|
|
|
|
vim.cmd(":AirlineRefresh")
|
|
|
|
elseif utils.is_plugin_loaded("lualine.nvim") then
|
2021-10-25 16:22:54 +00:00
|
|
|
require("plugins.lualine").config_lualine()
|
2021-09-01 17:03:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-16 01:13:17 +00:00
|
|
|
return change and "Changed color to " .. env_color .. " with mode " .. mode or "No change"
|
2021-09-01 17:03:05 +00:00
|
|
|
end
|
|
|
|
|
2021-09-16 20:32:29 +00:00
|
|
|
-- Don't need the autocommand when dark-notify is installed
|
|
|
|
local utils = require("utils")
|
|
|
|
if not utils.is_plugin_loaded("dark-notify") then
|
2024-05-29 20:16:28 +00:00
|
|
|
vim.api.nvim_create_autocmd({ "FocusGained" }, {
|
|
|
|
pattern = "*",
|
|
|
|
callback = _G.update_colors,
|
|
|
|
group = vim.api.nvim_create_augroup("auto_colors", { clear = true }),
|
|
|
|
})
|
2021-09-16 20:32:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Initial setting of colors
|
2021-09-16 01:13:17 +00:00
|
|
|
_G.update_colors()
|