-
-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathinit.lua
More file actions
169 lines (129 loc) · 3.24 KB
/
init.lua
File metadata and controls
169 lines (129 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
local uv = vim.loop
local config = require('github-theme.config')
local function read_file(filepath)
local file = io.open(filepath, 'r')
if file then
local content = file:read()
file:close()
return content
end
end
local function write_file(filepath, content)
local file = io.open(filepath, 'wb')
if file then
file:write(content)
file:close()
end
end
local function stat_file(path)
for _ = 1, 3 do
local stat = uv.fs_stat(path)
if stat then
return stat
end
uv.sleep(1)
end
end
local M = {}
function M.reset()
require('github-theme.config').reset()
require('github-theme.override').reset()
end
function M.compile()
require('github-theme.lib.log').clear()
local compiler = require('github-theme.lib.compiler')
local themes = require('github-theme.palette').themes
for _, style in ipairs(themes) do
compiler.compile({ style = style })
end
end
-- Avoid g:colors_name reloading
local lock = false
local did_setup = false
function M.load(opts)
if lock then
return
end
if not did_setup then
M.setup()
end
opts = opts or {}
local _, compiled_file = config.get_compiled_info(opts)
lock = true
local f = loadfile(compiled_file)
if not f then
M.compile()
f = loadfile(compiled_file)
end
---@diagnostic disable-next-line: need-check-nil
f()
require('github-theme.autocmds').set_autocmds()
lock = false
end
M.setup = function(opts)
did_setup = true
opts = opts or {}
local override = require('github-theme.override')
-- New configs
if opts.options then
config.set_options(opts.options)
end
if opts.palettes then
override.palettes = opts.palettes
end
if opts.specs then
override.specs = opts.specs
end
if opts.groups then
override.groups = opts.groups
end
local util = require('github-theme.util')
util.ensure_dir(config.options.compile_path)
local cached_path = util.join_paths(config.options.compile_path, 'cache')
local cached = read_file(cached_path)
local git_mtime
local git_path = debug.getinfo(1).source
if (git_path or ''):find('^@.') then
git_path = git_path:sub(2) .. '/../../../.git'
local st = stat_file(git_path)
if not st then
goto skip
end
-- Handle a .git "file" (e.g. worktree or submodule)
if st.type == 'file' then
local f = io.open(git_path, 'r')
if not f then
goto skip
end
local gitdir_path = (f:read('*l') or ''):match('^gitdir: (.+)')
f:close()
if not gitdir_path then
goto skip
end
git_path = gitdir_path
st = stat_file(git_path)
if not st then
goto skip
end
end
local sec, nsec = st.mtime.sec, st.mtime.nsec or 0
if nsec == 1e9 then
sec = sec + 1
nsec = 0
end
git_mtime = ('%d.%09d'):format(sec, nsec)
end
::skip::
local hash = ('%s %s'):format(
require('github-theme.lib.hash')(opts),
git_mtime or git_path
)
-- Force re-compile if .git dir could not be detected (plugin may have been
-- updated and we don't want to use a stale cache).
if cached ~= hash or not git_mtime then
M.compile()
write_file(cached_path, hash)
end
require('github-theme.util.deprecation').check_deprecation(opts)
end
return M