100 lines
3.1 KiB
Lua
100 lines
3.1 KiB
Lua
-- Two sources, one owner:
|
|
-- external ~/.local/state/theme/current.json, written by theme-apply.nu on the Linux desktop.
|
|
-- `variant` is dark/light and `ports.nvim` the colorscheme. When it exists it wins.
|
|
-- cache <stdpath state>/theme_cache.json, written on every ColorScheme event, read before
|
|
-- any colorscheme loads so a restart does not flash the wrong background.
|
|
local M = {}
|
|
|
|
M.default = "catppuccin"
|
|
|
|
local cache_file = vim.fn.stdpath("state") .. "/theme_cache.json"
|
|
|
|
function M.external_path()
|
|
return (vim.env.XDG_STATE_HOME or (vim.env.HOME .. "/.local/state")) .. "/theme/current.json"
|
|
end
|
|
|
|
function M.has_external()
|
|
return vim.uv.fs_stat(M.external_path()) ~= nil
|
|
end
|
|
|
|
local function read_json(path)
|
|
local f = io.open(path, "r")
|
|
if not f then
|
|
return nil
|
|
end
|
|
local text = f:read("*a")
|
|
f:close()
|
|
local ok, data = pcall(vim.json.decode, text)
|
|
return ok and type(data) == "table" and data or nil
|
|
end
|
|
|
|
local function valid_background(bg)
|
|
return bg == "dark" or bg == "light"
|
|
end
|
|
|
|
-- catppuccin with flavour "auto" reports "catppuccin-mocha"; re-applying that would pin the flavour.
|
|
function M.logical_name(name)
|
|
return name and name:gsub("^catppuccin%-.*", "catppuccin") or name
|
|
end
|
|
|
|
-- A half-written file decodes to nil; the next change event retries.
|
|
function M.parse_external(state)
|
|
if type(state) ~= "table" or type(state.ports) ~= "table" or type(state.ports.nvim) ~= "string" then
|
|
return nil
|
|
end
|
|
return { background = state.variant == "light" and "light" or "dark", colorscheme = state.ports.nvim }
|
|
end
|
|
|
|
function M.apply_external()
|
|
local state = M.parse_external(read_json(M.external_path()))
|
|
if not state then
|
|
return false
|
|
end
|
|
vim.o.background = state.background
|
|
pcall(vim.cmd.colorscheme, state.colorscheme)
|
|
return true
|
|
end
|
|
|
|
-- Switch background and re-apply the current colorscheme (auto-dark-mode callbacks).
|
|
function M.set_background(bg)
|
|
vim.o.background = bg
|
|
pcall(vim.cmd.colorscheme, M.logical_name(vim.g.colors_name) or M.default)
|
|
end
|
|
|
|
-- Call once catppuccin is configured.
|
|
function M.setup()
|
|
local cache = read_json(cache_file) or {}
|
|
if valid_background(cache.background) then
|
|
vim.o.background = cache.background
|
|
end
|
|
|
|
vim.api.nvim_create_autocmd("ColorScheme", {
|
|
group = vim.api.nvim_create_augroup("core.theme", { clear = true }),
|
|
callback = function()
|
|
if valid_background(vim.o.background) and vim.g.colors_name then
|
|
vim.fn.mkdir(vim.fs.dirname(cache_file), "p")
|
|
local f = io.open(cache_file, "w")
|
|
if f then
|
|
f:write(vim.json.encode({ background = vim.o.background, colorscheme = M.logical_name(vim.g.colors_name) }))
|
|
f:close()
|
|
end
|
|
end
|
|
end,
|
|
})
|
|
|
|
if not pcall(vim.cmd.colorscheme, cache.colorscheme or M.default) then
|
|
vim.cmd.colorscheme(M.default)
|
|
end
|
|
|
|
if M.has_external() then
|
|
M.apply_external()
|
|
-- theme-apply.nu rewrites the file in place, so a plain file watch is enough.
|
|
M.watcher = vim.uv.new_fs_event()
|
|
if M.watcher then
|
|
M.watcher:start(M.external_path(), {}, vim.schedule_wrap(M.apply_external))
|
|
end
|
|
end
|
|
end
|
|
|
|
return M
|