summaryrefslogtreecommitdiff
path: root/plugin/colorscheme_persistence.lua
diff options
context:
space:
mode:
authorbeeb5k <beebeeb5k@gmail.com>2026-06-20 01:45:46 +0530
committerbeeb5k <beebeeb5k@gmail.com>2026-06-20 01:45:46 +0530
commit3045b8bff449456fc833ee7447bf9692a2bd5c4e (patch)
treefb96c71105e6aa2099427d880d1642101ffd0dad /plugin/colorscheme_persistence.lua
parent8039ec1ab5bb80062ed8604fc9e67af6fcf71d7d (diff)
colorscheme_persistence plugin
Diffstat (limited to 'plugin/colorscheme_persistence.lua')
-rw-r--r--plugin/colorscheme_persistence.lua48
1 files changed, 48 insertions, 0 deletions
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()