Skip to content

Commit bca6544

Browse files
committed
perf(cache): use JSON encoding for cached theme
1 parent 0669430 commit bca6544

File tree

1 file changed

+42
-53
lines changed

1 file changed

+42
-53
lines changed

lua/cyberdream/cache.lua

+42-53
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,7 @@ 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.lua"
6-
7-
local function gen_hl_opts_string(opts)
8-
local opts_string = ""
9-
for opt, value in pairs(opts) do
10-
local value_str = ((type(value) == "string") and "'" .. value .. "'") or tostring(value)
11-
opts_string = opts_string .. opt .. " = " .. value_str .. ", "
12-
end
13-
return opts_string
14-
end
15-
16-
local function gen_highlights_string(highlights)
17-
local highlights_string = ""
18-
for group, opts in pairs(highlights) do
19-
local opts_string = gen_hl_opts_string(opts)
20-
highlights_string = highlights_string .. "vim.api.nvim_set_hl(0, '" .. group .. "', {" .. opts_string .. "})\n"
21-
end
22-
return highlights_string
23-
end
24-
25-
local function gen_fillchars_string()
26-
local fillchars_string = "vim.opt.fillchars:append({\n"
27-
for key, value in pairs(vim.opt.fillchars:get()) do
28-
fillchars_string = fillchars_string .. " " .. key .. " = '" .. value .. "',\n"
29-
end
30-
fillchars_string = fillchars_string .. "})\n"
31-
return fillchars_string
32-
end
33-
34-
local function gen_terminal_colors_string()
35-
local terminal_colors_string = ""
36-
for i = 0, 15 do
37-
terminal_colors_string = terminal_colors_string
38-
.. "vim.g.terminal_color_"
39-
.. i
40-
.. " = '"
41-
.. vim.g["terminal_color_" .. i]
42-
.. "'\n"
43-
end
44-
return terminal_colors_string
45-
end
5+
local cache_file = vim.fn.stdpath("cache") .. "/cyberdream_cache.json"
466

477
--- build a cache file for a configured cyberdream theme
488
--- @param theme table
@@ -53,35 +13,64 @@ M.build = function(theme)
5313
return
5414
end
5515

56-
cache:write(gen_highlights_string(theme.highlights))
57-
58-
if config.options.hide_fillchars then
59-
cache:write(gen_fillchars_string())
60-
end
61-
16+
local terminal_colors = {}
6217
if config.options.terminal_colors then
63-
cache:write(gen_terminal_colors_string())
18+
for i = 0, 15 do
19+
terminal_colors[i] = vim.g["terminal_color_" .. i]
20+
end
6421
end
6522

66-
cache:write("vim.g.colors_name = 'cyberdream'\n")
67-
cache:close()
23+
theme.fillchars = config.options.hide_fillchars
24+
theme.terminal_colors = #terminal_colors > 0 and terminal_colors or nil
6825

26+
cache:write(vim.json.encode(theme))
6927
util.notify("Cache file written to " .. cache_file)
7028
end
7129

30+
M.load_options = function(theme)
31+
if theme.fillchars then
32+
vim.opt.fillchars:append({
33+
horiz = " ",
34+
horizup = " ",
35+
horizdown = " ",
36+
vert = " ",
37+
vertleft = " ",
38+
vertright = " ",
39+
verthoriz = " ",
40+
eob = " ",
41+
})
42+
else
43+
vim.opt.fillchars:append({
44+
eob = " ",
45+
})
46+
end
47+
48+
if theme.terminal_colors then
49+
for i = 0, 15 do
50+
vim.g["terminal_color_" .. i] = theme.terminal_colors[i]
51+
end
52+
end
53+
end
54+
7255
--- load a cache file for a configured cyberdream theme
7356
M.load = function()
74-
local cache = loadfile(cache_file)
57+
local cache = io.open(cache_file, "r")
7558
if not cache then
7659
local notify = vim.defer_fn(function()
7760
util.notify("Cache file not found, run :CyberdreamBuildCache to generate", "warn")
7861
end, 1000)
7962
return notify
8063
end
8164

82-
if cache then
83-
cache()
65+
local theme = vim.json.decode(cache:read("*a"))
66+
67+
for group, opts in pairs(theme.highlights) do
68+
vim.api.nvim_set_hl(0, group, opts)
8469
end
70+
71+
M.load_options(theme)
72+
73+
vim.g.colors_name = "cyberdream"
8574
end
8675

8776
M.clear = function()

0 commit comments

Comments
 (0)