Skip to content

Commit aabcbf1

Browse files
committed
feat(recipes): add recipe for caching colorscheme settings
1 parent f4a19c4 commit aabcbf1

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Cache Colorscheme
2+
3+
**Website:** <https://docs.astronvim.com/recipes/colorscheme/#cache-colorscheme>
4+
5+
This plugin specification caches the last chosen colorscheme by the user to persist colorscheme changes without modifying user configuration.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
-- pick a location to cache colorscheme
2+
local colorscheme_cache = vim.fs.joinpath(vim.fn.stdpath "state" --[[@as string]], "last_colorscheme")
3+
4+
--- Execute function with open file
5+
---@param file string path to file to interact with
6+
---@param mode openmode the mode in which to open the file
7+
---@param callback fun(fd:file*) the callback to execute with the opened file
8+
---@param on_error? fun(err:string) the callback to execute if unable to open the file
9+
local function with_file(file, mode, callback, on_error)
10+
local fd, errmsg = io.open(file, mode)
11+
if fd then
12+
callback(fd)
13+
fd:close()
14+
elseif errmsg and on_error then
15+
on_error(errmsg)
16+
end
17+
end
18+
19+
return {
20+
{
21+
"AstroNvim/astroui",
22+
--@param opts AstroUIOpts
23+
opts = function(_, opts)
24+
-- read colorscheme cache on open
25+
with_file(colorscheme_cache, "r", function(file) opts.colorscheme = file:read "*a" end)
26+
end,
27+
},
28+
{
29+
"AstroNvim/astrocore",
30+
---@type AstroCoreOpts
31+
opts = {
32+
autocmds = {
33+
-- save colorscheme to cache on change
34+
cache_colorscheme = {
35+
{
36+
event = "ColorScheme",
37+
callback = function(args)
38+
if args.match then with_file(colorscheme_cache, "w+", function(file) file:write(args.match) end) end
39+
end,
40+
},
41+
},
42+
},
43+
},
44+
},
45+
}

0 commit comments

Comments
 (0)