Skip to content

Commit 86ccc6e

Browse files
authored
perf(startup): improve rendering on nvim start (#202)
1 parent 8698413 commit 86ccc6e

File tree

8 files changed

+104
-54
lines changed

8 files changed

+104
-54
lines changed

lua/colorbox.lua

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
local logging = require("colorbox.commons.logging")
22
local LogLevels = require("colorbox.commons.logging").LogLevels
33
local strings = require("colorbox.commons.strings")
4+
local tables = require("colorbox.commons.tables")
45

56
local configs = require("colorbox.configs")
67
local timing = require("colorbox.timing")
78
local track = require("colorbox.track")
89
local runtime = require("colorbox.runtime")
910
local controller = require("colorbox.controller")
11+
local loader = require("colorbox.loader")
1012

1113
--- @param opts colorbox.Options?
1214
local function setup(opts)
@@ -78,30 +80,7 @@ local function setup(opts)
7880

7981
vim.api.nvim_create_autocmd("ColorSchemePre", {
8082
callback = function(event)
81-
local logger = logging.get("colorbox") --[[@as commons.logging.Logger]]
82-
-- logger:debug("|colorbox.setup| ColorSchemePre event:%s", vim.inspect(event))
83-
local ColorNameToColorSpecsMap = require("colorbox.db").get_color_name_to_color_specs_map()
84-
if type(event) ~= "table" or ColorNameToColorSpecsMap[event.match] == nil then
85-
return
86-
end
87-
local spec = ColorNameToColorSpecsMap[event.match]
88-
local autoload = string.format("%s/autoload", spec.full_pack_path)
89-
vim.opt.runtimepath:append(autoload)
90-
vim.cmd(string.format([[packadd %s]], spec.git_path))
91-
if type(confs.setup) == "table" and type(confs.setup[spec.handle]) == "function" then
92-
local home_dir = vim.fn["colorbox#base_dir"]()
93-
local ok, setup_err = pcall(confs.setup[spec.handle], home_dir, spec)
94-
if not ok then
95-
logger:err(
96-
"failed to setup colorscheme:%s, error:%s",
97-
vim.inspect(spec.handle),
98-
vim.inspect(setup_err)
99-
)
100-
end
101-
end
102-
if confs.background == "dark" or confs.background == "light" then
103-
vim.opt.background = confs.background
104-
end
83+
loader.load(tables.tbl_get(event, "match"))
10584
end,
10685
})
10786

lua/colorbox/loader.lua

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
local logging = require("colorbox.commons.logging")
2+
local strings = require("colorbox.commons.strings")
3+
local tables = require("colorbox.commons.tables")
4+
5+
local configs = require("colorbox.configs")
6+
7+
local M = {}
8+
9+
--- @param colorname string?
10+
M.load = function(colorname)
11+
local ColorNameToColorSpecsMap = require("colorbox.db").get_color_name_to_color_specs_map()
12+
13+
if strings.empty(colorname) then
14+
return
15+
end
16+
local spec = ColorNameToColorSpecsMap[colorname]
17+
if tables.tbl_empty(spec) then
18+
return
19+
end
20+
21+
local autoload_path = string.format("%s/autoload", spec.full_pack_path)
22+
vim.opt.runtimepath:append(autoload_path)
23+
vim.cmd(string.format([[packadd %s]], spec.git_path))
24+
25+
local confs = configs.get()
26+
local logger = logging.get("colorbox") --[[@as commons.logging.Logger]]
27+
if type(confs.setup) == "table" and vim.is_callable(confs.setup[spec.handle]) then
28+
local home_dir = vim.fn["colorbox#base_dir"]()
29+
local ok, setup_err = pcall(confs.setup[spec.handle], home_dir, spec)
30+
logger:ensure(
31+
ok,
32+
"failed to setup colorscheme:%s, error:%s",
33+
vim.inspect(spec.handle),
34+
vim.inspect(setup_err)
35+
)
36+
end
37+
if confs.background == "dark" or confs.background == "light" then
38+
vim.opt.background = confs.background
39+
end
40+
41+
vim.cmd(string.format("colorscheme %s", colorname))
42+
end
43+
44+
return M

lua/colorbox/policy.lua

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
local logging = require("colorbox.commons.logging")
2-
local numbers = require("colorbox.commons.numbers")
3-
41
local configs = require("colorbox.configs")
52

63
local builtin_policy = require("colorbox.policy.builtin")

lua/colorbox/policy/builtin.lua

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local numbers = require("colorbox.commons.numbers")
33

44
local runtime = require("colorbox.runtime")
55
local track = require("colorbox.track")
6+
local loader = require("colorbox.loader")
67

78
local M = {}
89

@@ -18,7 +19,8 @@ M.shuffle = function()
1819
vim.inspect(#ColorNamesList),
1920
vim.inspect(i)
2021
)
21-
vim.cmd(string.format([[color %s]], color))
22+
23+
loader.load(color)
2224
end
2325
end
2426

@@ -28,7 +30,8 @@ M.in_order = function()
2830
local previous_track = track.previous_track() --[[@as colorbox.PreviousTrack]]
2931
local i = previous_track ~= nil and previous_track.color_number or 0
3032
local color = track.get_next_color_name_by_idx(i)
31-
vim.cmd(string.format([[color %s]], color))
33+
34+
loader.load(color)
3235
end
3336
end
3437

@@ -38,7 +41,8 @@ M.reverse_order = function()
3841
local previous_track = track.previous_track() --[[@as colorbox.PreviousTrack]]
3942
local i = previous_track ~= nil and previous_track.color_number or (#ColorNamesList + 1)
4043
local color = track.get_prev_color_name_by_idx(i)
41-
vim.cmd(string.format([[color %s]], color))
44+
45+
loader.load(color)
4246
end
4347
end
4448

@@ -49,7 +53,7 @@ M.single = function()
4953
local color = previous_track ~= nil and previous_track.color_name
5054
or track.get_next_color_name_by_idx(0)
5155
if color ~= vim.g.colors_name then
52-
vim.cmd(string.format([[color %s]], color))
56+
loader.load(color)
5357
end
5458
end
5559
end

lua/colorbox/policy/filetype.lua

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local strings = require("colorbox.commons.strings")
22

33
local configs = require("colorbox.configs")
44
local track = require("colorbox.track")
5+
local loader = require("colorbox.loader")
56

67
local M = {}
78

@@ -25,20 +26,14 @@ M.run = function()
2526
local confs = configs.get()
2627
local ft = vim.bo.filetype or ""
2728

28-
if confs.policy.mapping[ft] then
29-
local ok, err =
30-
pcall(vim.cmd --[[@as function]], string.format([[color %s]], confs.policy.mapping[ft]))
31-
assert(ok, err)
29+
if strings.not_empty(confs.policy.mapping[ft]) then
30+
loader.load(confs.policy.mapping[ft])
3231
track.sync_syntax()
3332
elseif strings.empty(ft) and strings.not_empty(confs.policy.empty) then
34-
local ok, err =
35-
pcall(vim.cmd --[[@as function]], string.format([[color %s]], confs.policy.empty))
36-
assert(ok, err)
33+
loader.load(confs.policy.empty)
3734
track.sync_syntax()
3835
elseif strings.not_empty(confs.policy.fallback) then
39-
local ok, err =
40-
pcall(vim.cmd --[[@as function]], string.format([[color %s]], confs.policy.fallback))
41-
assert(ok, err)
36+
loader.load(confs.policy.fallback)
4237
track.sync_syntax()
4338
end
4439
end, 10)

lua/colorbox/timing.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local M = {}
55

66
M.startup = function()
77
vim.api.nvim_create_autocmd({ "VimEnter" }, {
8-
callback = vim.schedule_wrap(policy.run),
8+
callback = policy.run,
99
})
1010
end
1111

@@ -16,12 +16,12 @@ M.filetype = function()
1616
"WinEnter",
1717
"TermEnter",
1818
}, {
19-
callback = vim.schedule_wrap(policy.run),
19+
callback = policy.run,
2020
})
2121
end
2222

2323
M.fixed_interval = function()
24-
vim.schedule_wrap(policy.run)()
24+
policy.run()
2525
end
2626

2727
M.setup = function()

spec/colorbox/controller_spec.lua

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,22 @@ describe("colorbox.controller", function()
4747
controller.update()
4848
end
4949
end)
50-
it("clean", function()
51-
if not github_actions then
52-
controller.clean()
53-
end
54-
end)
50+
-- it("clean", function()
51+
-- if not github_actions then
52+
-- controller.clean()
53+
-- end
54+
-- end)
5555
it("info", function()
5656
if not github_actions then
5757
controller.info("scale=0.7")
5858
controller.info()
5959
controller.info("")
6060
end
6161
end)
62-
it("reinstall", function()
63-
if not github_actions then
64-
controller.reinstall()
65-
end
66-
end)
62+
-- it("reinstall", function()
63+
-- if not github_actions then
64+
-- controller.reinstall()
65+
-- end
66+
-- end)
6767
end)
6868
end)

spec/colorbox/loader_spec.lua

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
local cwd = vim.fn.getcwd()
2+
3+
describe("colorbox.loader", function()
4+
local assert_eq = assert.is_equal
5+
local assert_true = assert.is_true
6+
local assert_false = assert.is_false
7+
8+
before_each(function()
9+
vim.api.nvim_command("cd " .. cwd)
10+
end)
11+
12+
local github_actions = os.getenv("GITHUB_ACTIONS") == "true"
13+
local runtime = require("colorbox.runtime")
14+
local loader = require("colorbox.loader")
15+
require("colorbox").setup({
16+
debug = true,
17+
file_log = true,
18+
background = "dark",
19+
})
20+
21+
describe("loader", function()
22+
it("load", function()
23+
-- if not github_actions then
24+
local colors = runtime.colornames()
25+
for i, c in ipairs(colors) do
26+
loader.load(c)
27+
end
28+
-- end
29+
end)
30+
end)
31+
end)

0 commit comments

Comments
 (0)