feat: add theme handling
This commit is contained in:
1 parent
9b0446c5f6
commit
0d9335a7b0
4 files changed
+128
-8
No files matched your search
@@ -0,0 +1,99 @@
|
||||
-- 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
|
||||
@@ -1,12 +1,14 @@
|
||||
-- All three installed; only the active one loads. lazy.nvim loads the others
|
||||
-- on :colorscheme.
|
||||
local schemes = { "catppuccin", "tokyonight", "kanagawa" }
|
||||
-- All installed; only the active one loads, lazy.nvim loads the others on :colorscheme.
|
||||
-- On the Linux desktop theme-apply.nu drives the colorscheme (see core/theme.lua);
|
||||
-- without that file auto-dark-mode follows the system appearance.
|
||||
local theme = require("core.theme")
|
||||
local schemes = { "catppuccin", "tokyonight", "rose-pine", "kanagawa" }
|
||||
|
||||
local function cycle()
|
||||
local current = vim.g.colors_name or ""
|
||||
local idx = 1
|
||||
for i, name in ipairs(schemes) do
|
||||
if current:find("^" .. name) then
|
||||
if vim.startswith(current, name) then
|
||||
idx = i
|
||||
end
|
||||
end
|
||||
@@ -24,11 +26,29 @@ return {
|
||||
init = function()
|
||||
vim.keymap.set("n", "<leader>tc", cycle, { desc = "Cycle colorscheme" })
|
||||
end,
|
||||
config = function()
|
||||
require("catppuccin").setup({})
|
||||
vim.cmd.colorscheme("catppuccin")
|
||||
opts = {
|
||||
flavour = "auto",
|
||||
background = { light = "latte", dark = "mocha" },
|
||||
},
|
||||
config = function(_, opts)
|
||||
require("catppuccin").setup(opts)
|
||||
theme.setup()
|
||||
end,
|
||||
},
|
||||
{ "folke/tokyonight.nvim", lazy = true },
|
||||
{ "rose-pine/neovim", name = "rose-pine", lazy = true },
|
||||
{ "rebelot/kanagawa.nvim", lazy = true },
|
||||
{
|
||||
"f-person/auto-dark-mode.nvim",
|
||||
lazy = false,
|
||||
cond = not theme.has_external(),
|
||||
opts = {
|
||||
set_dark_mode = function()
|
||||
theme.set_background("dark")
|
||||
end,
|
||||
set_light_mode = function()
|
||||
theme.set_background("light")
|
||||
end,
|
||||
},
|
||||
},
|
||||
}
|
||||
Reference in new issue
Block a user