From 3045b8bff449456fc833ee7447bf9692a2bd5c4e Mon Sep 17 00:00:00 2001 From: beeb5k Date: Sat, 20 Jun 2026 01:45:46 +0530 Subject: colorscheme_persistence plugin --- plugin/colorscheme_persistence.lua | 48 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 plugin/colorscheme_persistence.lua (limited to 'plugin/colorscheme_persistence.lua') diff --git a/plugin/colorscheme_persistence.lua b/plugin/colorscheme_persistence.lua new file mode 100644 index 0000000..0b43f3f --- /dev/null +++ b/plugin/colorscheme_persistence.lua @@ -0,0 +1,48 @@ +local theme_cache_path = vim.fn.stdpath("data") .. "/last_theme.txt" +local is_programmatic_change = false + +local function load_theme() + local theme = nil + + local f = io.open(theme_cache_path, "r") + if f then + theme = f:read("*l") + f:close() + end + + if theme then + theme = theme:match("^%s*(.-)%s*$") -- Trim any whitespace + end + + is_programmatic_change = true + local status = pcall(vim.cmd.colorscheme, theme) + + if not status then + vim.cmd.colorscheme("habamax") + end +end + +local group = vim.api.nvim_create_augroup("ThemePersistance", { clear = true }) + +vim.api.nvim_create_autocmd("ColorScheme", { + group = group, + callback = function(args) + if is_programmatic_change then + is_programmatic_change = false + return + end + + local theme_name = args.match + + vim.uv.fs_open(theme_cache_path, "w", 438, function(err, fd) + if err or not fd then + return + end + vim.uv.fs_write(fd, theme_name, -1, function() + vim.uv.fs_close(fd) + end) + end) + end, +}) + +load_theme() -- cgit v1.3.1