diff options
| author | brustybee <bee@4d2.org> | 2026-03-09 17:02:03 +0530 |
|---|---|---|
| committer | brustybee <bee@4d2.org> | 2026-03-09 17:02:03 +0530 |
| commit | 972e5f9c68cc433c5fc26adf92cd25b293a3776a (patch) | |
| tree | c5930854787316181d07854048d9627538017ceb /lua/colorscheme_persistence.lua | |
One shot upload
Diffstat (limited to 'lua/colorscheme_persistence.lua')
| -rw-r--r-- | lua/colorscheme_persistence.lua | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/lua/colorscheme_persistence.lua b/lua/colorscheme_persistence.lua new file mode 100644 index 0000000..e5f6cda --- /dev/null +++ b/lua/colorscheme_persistence.lua @@ -0,0 +1,50 @@ +local M = {} + +-- (~/.local/share/nvim/last_theme.txt) +local theme_cache_path = vim.fn.stdpath("data") .. "/last_theme.txt" + +M.is_programmatic_change = false + +function M.load_theme() + local theme = "" + + local f = io.open(theme_cache_path, "r") + if f then + theme = f:read("*all"):gsub("%s+", "") + f:close() + end + + if theme == "" then + theme = nixCats.extra("colorscheme") + end + + M.is_programmatic_change = true + local status, _ = pcall(vim.cmd.colorscheme, theme) + + -- just to be safe + if not status then + vim.cmd.colorscheme("habamax") + end +end + +vim.api.nvim_create_autocmd("ColorScheme", { + callback = function(args) + -- If the change was programmatic (like at startup), don't overwrite the file + if M.is_programmatic_change then + M.is_programmatic_change = false -- Reset for the next manual change + return + end + + -- Save the new theme name to the file + local theme_name = args.match + local f = io.open(theme_cache_path, "w") + if f then + f:write(theme_name) + f:close() + end + end, +}) + +M.load_theme() + +return M |
