From 0d9335a7b0d5cb1f49a519875702764833cdc2cf Mon Sep 17 00:00:00 2001 From: Johannes Kresner Date: Thu, 5 Nov 2026 15:12:49 +0100 Subject: [PATCH] feat: add theme handling --- lazy-lock.json | 1 + lua/core/theme.lua | 99 +++++++++++++++++++++++++++++++++++++ lua/plugins/colorscheme.lua | 34 ++++++++++--- nix/module.nix | 2 +- 4 files changed, 128 insertions(+), 8 deletions(-) create mode 100644 lua/core/theme.lua diff --git a/lazy-lock.json b/lazy-lock.json index b44a84e..a9f4095 100644 --- a/lazy-lock.json +++ b/lazy-lock.json @@ -31,6 +31,7 @@ "oil.nvim": { "branch": "master", "commit": "d3733d933833a0e59b813ecec99341527adf0c28" }, "plenary.nvim": { "branch": "master", "commit": "74b06c6c75e4eeb3108ec01852001636d85a932b" }, "render-markdown.nvim": { "branch": "main", "commit": "8e8795368391036643b622875a6da6a51bcd33f4" }, + "rose-pine": { "branch": "main", "commit": "ff483051a47e27d84bdef47703538df1ed9f4a47" }, "sidekick.nvim": { "branch": "main", "commit": "3d80a47e6375f6d647c9695d3afea6fa1b3275df" }, "snacks.nvim": { "branch": "main", "commit": "882c996cf28183f4d63640de0b4c02ec886d01f2" }, "tokyonight.nvim": { "branch": "main", "commit": "cdc07ac78467a233fd62c493de29a17e0cf2b2b6" }, diff --git a/lua/core/theme.lua b/lua/core/theme.lua new file mode 100644 index 0000000..0134e89 --- /dev/null +++ b/lua/core/theme.lua @@ -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 /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 diff --git a/lua/plugins/colorscheme.lua b/lua/plugins/colorscheme.lua index bd1e417..dd237d8 100644 --- a/lua/plugins/colorscheme.lua +++ b/lua/plugins/colorscheme.lua @@ -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", "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, + }, + }, } diff --git a/nix/module.nix b/nix/module.nix index 4e099ec..0b444f9 100644 --- a/nix/module.nix +++ b/nix/module.nix @@ -53,7 +53,7 @@ let intelephense # unfree: the host must allow it phpPackages.php-cs-fixer - phpPackages.phpstan + phpstan ]; # Kept out of home.packages to avoid colliding with other pythons/nodes.