Skip to content

Commit f94b960

Browse files
committed
feat(cache): auto-detect config updates & rebuild cache
see #147
1 parent b0e1429 commit f94b960

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

lua/cyberdream/cache.lua

+17-7
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ local config = require("cyberdream.config")
22
local util = require("cyberdream.util")
33

44
local M = {}
5-
local cache_file = vim.fn.stdpath("cache") .. "/cyberdream_cache.json"
5+
local theme_cache_file = vim.fn.stdpath("cache") .. "/cyberdream_cache.json"
66

77
--- build a cache file for a configured cyberdream theme
88
--- @param theme table
99
M.build = function(theme)
10-
local cache = io.open(cache_file, "w")
10+
local cache = io.open(theme_cache_file, "w")
1111
if not cache then
1212
util.notify("Failed to open cache file", "error")
1313
return
@@ -20,15 +20,15 @@ M.build = function(theme)
2020
end
2121
end
2222

23-
theme.fillchars = config.options.hide_fillchars
2423
theme.terminal_colors = #terminal_colors > 0 and terminal_colors or nil
24+
theme.config = config.options
2525

2626
cache:write(vim.json.encode(theme))
27-
util.notify("Cache file written to " .. cache_file)
27+
util.notify("Cache file written to " .. theme_cache_file)
2828
end
2929

3030
M.load_options = function(theme)
31-
if theme.fillchars then
31+
if theme.config.hide_fillchars then
3232
vim.opt.fillchars:append({
3333
horiz = " ",
3434
horizup = " ",
@@ -54,7 +54,7 @@ end
5454

5555
--- load a cache file for a configured cyberdream theme
5656
M.load = function()
57-
local cache = io.open(cache_file, "r")
57+
local cache = io.open(theme_cache_file, "r")
5858
if not cache then
5959
M.build(require("cyberdream.theme").setup())
6060
local notify = vim.defer_fn(function()
@@ -69,12 +69,22 @@ M.load = function()
6969
vim.api.nvim_set_hl(0, group, opts)
7070
end
7171

72+
-- check if config has changed
73+
if not vim.deep_equal(theme.config, config.options) then
74+
M.build(require("cyberdream.theme").setup())
75+
local notify = vim.defer_fn(function()
76+
util.notify(" Building cache...\n A restart may be required for changes to take effect.")
77+
M.load()
78+
end, 1000)
79+
return notify
80+
end
81+
7282
M.load_options(theme)
7383
vim.g.colors_name = "cyberdream"
7484
end
7585

7686
M.clear = function()
77-
os.remove(cache_file)
87+
os.remove(theme_cache_file)
7888
util.notify("Cache file removed")
7989
end
8090

0 commit comments

Comments
 (0)