@@ -2,47 +2,7 @@ local config = require("cyberdream.config")
2
2
local util = require (" cyberdream.util" )
3
3
4
4
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"
46
6
47
7
--- build a cache file for a configured cyberdream theme
48
8
--- @param theme table
@@ -53,35 +13,64 @@ M.build = function(theme)
53
13
return
54
14
end
55
15
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 = {}
62
17
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
64
21
end
65
22
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
68
25
26
+ cache :write (vim .json .encode (theme ))
69
27
util .notify (" Cache file written to " .. cache_file )
70
28
end
71
29
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
+
72
55
--- load a cache file for a configured cyberdream theme
73
56
M .load = function ()
74
- local cache = loadfile (cache_file )
57
+ local cache = io.open (cache_file , " r " )
75
58
if not cache then
76
59
local notify = vim .defer_fn (function ()
77
60
util .notify (" Cache file not found, run :CyberdreamBuildCache to generate" , " warn" )
78
61
end , 1000 )
79
62
return notify
80
63
end
81
64
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 )
84
69
end
70
+
71
+ M .load_options (theme )
72
+
73
+ vim .g .colors_name = " cyberdream"
85
74
end
86
75
87
76
M .clear = function ()
0 commit comments