Integrate seamless, automatic, and persistent colorscheme switching for all neovim

plugins in my config
This commit is contained in:
Martin Larsson 2025-07-24 00:39:00 +02:00
parent 76a2e58ab8
commit 9c5a1479b7
7 changed files with 277 additions and 262 deletions

View file

@ -1,16 +0,0 @@
local has_generated = false
local colors = require("ayu.colors")
colors.generate(true)
local M = {}
function M.get()
if not has_generated then
colors.generate(true)
has_generated = true
end
return colors
end
return M

View file

@ -1,5 +1,6 @@
local theme_colors = require("ayu.colors") local utils = require("utils")
theme_colors.generate(true)
local function setup_colors()
-- These are not apart of the Ayu color theme, however, I needed these -- These are not apart of the Ayu color theme, however, I needed these
-- colors while still fitting in with the rest -- colors while still fitting in with the rest
local ayu_turquoise = "#5CCFE6" local ayu_turquoise = "#5CCFE6"
@ -8,8 +9,8 @@ local ayu_dark_blue = "#3A7BD5"
local colors = { local colors = {
info = ayu_dark_blue, info = ayu_dark_blue,
hint = ayu_turquoise, hint = ayu_turquoise,
warning = theme_colors.warning, warning = utils.ayu_colors.warning,
error = theme_colors.error, error = utils.ayu_colors.error,
} }
for _, highlight in ipairs({ for _, highlight in ipairs({
@ -24,6 +25,10 @@ for _, highlight in ipairs({
}) do }) do
vim.api.nvim_set_hl(0, highlight[1], highlight[2]) vim.api.nvim_set_hl(0, highlight[1], highlight[2])
end end
end
utils.create_user_event_cb("ColorsyncThemeChanged", setup_colors, "ColorsyncEvents")
setup_colors()
local sev = vim.diagnostic.severity local sev = vim.diagnostic.severity
vim.diagnostic.config({ vim.diagnostic.config({

View file

@ -1,5 +1,5 @@
local uv = vim.loop local uv = vim.loop
local api = vim.api local utils = require("utils")
local function force_color_from_reference_on_others(others, reference) local function force_color_from_reference_on_others(others, reference)
local reference_hl = vim.api.nvim_get_hl(0, { name = reference }) local reference_hl = vim.api.nvim_get_hl(0, { name = reference })
@ -28,10 +28,17 @@ end
return { return {
"Shatur/neovim-ayu", "Shatur/neovim-ayu",
priority = 1010,
config = function() config = function()
local function set_colorscheme()
utils.colorsync_theme = vim.fn.system({ "colorsync", "get" }):gsub("%s+", "")
vim.o.background = utils.colorsync_theme == "ayulight" and "light" or "dark"
local is_mirage = utils.colorsync_theme == "ayumirage"
local ayu = require("ayu") local ayu = require("ayu")
local colors = require("ayu.colors") utils.ayu_colors = require("ayu.colors")
colors.generate(true) utils.ayu_colors.generate(is_mirage)
local overrides = { local overrides = {
global_variable = { global_variable = {
@ -43,21 +50,21 @@ return {
}, },
namespace = { namespace = {
italic = true, italic = true,
fg = colors.markup, fg = utils.ayu_colors.markup,
}, },
pre_process = { pre_process = {
fg = colors.keyword, fg = utils.ayu_colors.keyword,
}, },
default_type = { default_type = {
fg = colors.regexp fg = utils.ayu_colors.regexp
}, },
type = { type = {
fg = colors.entity, fg = utils.ayu_colors.entity,
}, },
} }
ayu.setup({ ayu.setup({
mirage = true, mirage = is_mirage,
terminal = false, terminal = false,
overrides = { overrides = {
-- TRANSPARENCY -- TRANSPARENCY
@ -106,26 +113,24 @@ return {
}, },
}) })
local function set_colorscheme() ayu.colorscheme()
local active_theme = vim.fn.system({ "colorsync", "get" }):gsub("%s+", "")
if active_theme == "ayulight" then
api.nvim_command("colorscheme ayu-light")
elseif active_theme == "ayumirage" then
api.nvim_command("colorscheme ayu-mirage")
else
api.nvim_command("colorscheme ayu-dark")
end
end end
vim.api.nvim_create_augroup("ColorsyncEvents", { clear = true })
local filepath = os.getenv("HOME") .. "/.local/state/colorsync/current" local filepath = os.getenv("HOME") .. "/.local/state/colorsync/current"
local handle = uv.new_fs_event() local handle = uv.new_fs_event()
handle:start(filepath, {}, function(err, filename, status) handle:start(filepath, {}, function(err)
if err then if err then
vim.notify("Error watching: " .. filepath .. "\n" .. err) vim.notify("Error watching: " .. filepath .. "\n" .. err)
return return
end end
vim.schedule(set_colorscheme) vim.schedule(function()
set_colorscheme()
vim.api.nvim_exec_autocmds("User", { pattern = "ColorsyncThemeChanged" })
vim.api.nvim_exec_autocmds("ColorScheme", {})
end)
end) end)
set_colorscheme() set_colorscheme()

View file

@ -1,3 +1,5 @@
local utils = require("utils")
return { return {
"b0o/incline.nvim", "b0o/incline.nvim",
dependencies = { dependencies = {
@ -10,9 +12,9 @@ return {
} }
} }
}, },
event = "VeryLazy", config = function()
lazy = true, local function setup_incline()
opts = { require("incline").setup({
window = { window = {
padding = 0, padding = 0,
}, },
@ -105,8 +107,8 @@ return {
end end
return { return {
guibg = "#1e2030", guibg = utils.ayu_colors.panel_bg,
guifg = "#cad3f5", guifg = utils.ayu_colors.fg,
{ " " }, { " " },
{ get_diagnostic_label() }, { get_diagnostic_label() },
{ get_git_diff() }, { get_git_diff() },
@ -116,5 +118,10 @@ return {
{ " " } { " " }
} }
end, end,
}, })
end
utils.create_user_event_cb("ColorsyncThemeChanged", setup_incline, "ColorsyncEvents")
setup_incline()
end
} }

View file

@ -1,3 +1,5 @@
local utils = require("utils")
local saved_hlsearch = false local saved_hlsearch = false
local saved_highlights = {} local saved_highlights = {}
@ -23,7 +25,7 @@ end
local function save_and_set_invisible_inlay_hints_hl() local function save_and_set_invisible_inlay_hints_hl()
saved_highlights = vim.api.nvim_get_hl(0, { name = "LspInlayHint" }) saved_highlights = vim.api.nvim_get_hl(0, { name = "LspInlayHint" })
vim.api.nvim_set_hl(0, "LspInlayHint", { fg = require("colors").get().bg, bg = "none" }) vim.api.nvim_set_hl(0, "LspInlayHint", { fg = utils.ayu_colors.bg, bg = "none" })
end end
local function restore_inlay_hints_hl() local function restore_inlay_hints_hl()

View file

@ -1,3 +1,5 @@
local utils = require("utils")
local function resize_mode() local function resize_mode()
if require("window_management").is_in_resizing_mode() then if require("window_management").is_in_resizing_mode() then
return "▲ Resizing ▼ " return "▲ Resizing ▼ "
@ -26,7 +28,9 @@ return {
}, },
event = "VeryLazy", event = "VeryLazy",
lazy = true, lazy = true,
opts = { config = function()
local function setup_lualine()
require("lualine").setup({
options = { options = {
theme = "ayu", theme = "ayu",
globalstatus = true, globalstatus = true,
@ -66,5 +70,10 @@ return {
lualine_z = {} lualine_z = {}
}, },
tabline = {}, tabline = {},
} })
end
utils.create_user_event_cb("ColorsyncThemeChanged", setup_lualine, "ColorsyncEvents")
setup_lualine()
end
} }

View file

@ -1,5 +1,8 @@
local M = {} local M = {}
M.colorsync_theme = nil
M.ayu_colors = nil
function M.set_keymap_list(keymap_list, mode) function M.set_keymap_list(keymap_list, mode)
mode = mode or "n" mode = mode or "n"
M.foreach(keymap_list, function(mapping) M.foreach(keymap_list, function(mapping)