Skip to content

Commit 8782111

Browse files
authored
perf(init): improve speed via build index when initializing (#37)
1 parent abeaccb commit 8782111

File tree

4 files changed

+85
-36
lines changed

4 files changed

+85
-36
lines changed

README.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ It allow you do any switches with:
4848
- [Requirement](#-requirement)
4949
- [Install](#-install)
5050
- [lazy.nvim](#lazynvim)
51+
- [pckr.nvim](#pckrnvim)
5152
- [Configuration](#-configuration)
5253
- [Development](#-development)
5354
- [Contribute](#-contribute)
@@ -59,6 +60,12 @@ It allow you do any switches with:
5960

6061
## 📦 Install
6162

63+
**Warning:** if this plugin provides the main colorscheme (e.g. the `colorscheme` command right after nvim start), then make sure:
64+
65+
1. Don't lazy this plugin, it only takes ~4 ms to load.
66+
2. Load this plugin before all other start plugins.
67+
3. Hold your wifi, it `clone` and `pull` a lot of git repos!
68+
6269
### [lazy.nvim](https://github.com/folke/lazy.nvim)
6370

6471
```lua
@@ -73,12 +80,17 @@ require('lazy').setup({
7380
})
7481
```
7582

76-
> Note:
77-
>
78-
> If this plugin provides the main colorscheme (e.g. the `colorscheme` command right after nvim start), then make sure:
79-
>
80-
> 1. Don't lazy this plugin.
81-
> 2. Load this plugin before all other start plugins.
83+
### [pckr.nvim](https://github.com/lewis6991/pckr.nvim)
84+
85+
```lua
86+
require('pckr').add{
87+
{
88+
'linrongbin16/colorbox.nvim',
89+
run = function() require('colorbox').update() end,
90+
config = function() require('colorbox').setup() end,
91+
}
92+
}
93+
```
8294

8395
## 🔧 Configuration
8496

lua/colorbox.lua

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ local Defaults = {
3636
--- @type colorbox.Options
3737
local Configs = {}
3838

39-
-- color names list
39+
-- filtered color names list
4040
--- @type string[]
41-
local ColorNamesList = {}
41+
local FilteredColorNamesList = {}
4242

4343
--- @param color_name string
4444
--- @param spec colorbox.ColorSpec
@@ -100,41 +100,25 @@ local function _init()
100100
-- vim.opt.packpath:append(cwd .. "/pack/colorbox/opt")
101101
-- vim.cmd([[packadd catppuccin-nvim]])
102102

103-
local HandleToColorSpecsMap =
104-
require("colorbox.db").get_handle_to_color_specs_map()
105-
logger.debug(
106-
"|colorbox._init| HandleToColorSpecsMap:%s",
107-
vim.inspect(HandleToColorSpecsMap)
108-
)
109-
for _, spec in pairs(HandleToColorSpecsMap) do
110-
if
111-
vim.fn.isdirectory(spec.full_pack_path) > 0
112-
and vim.fn.isdirectory(spec.full_pack_path .. "/.git") > 0
113-
then
114-
for _, color_name in ipairs(spec.color_names) do
115-
if not _should_filter(color_name, spec) then
116-
table.insert(ColorNamesList, color_name)
117-
end
118-
end
103+
local ColorNameToColorSpecsMap =
104+
require("colorbox.db").get_color_name_to_color_specs_map()
105+
local ColorNamesList = require("colorbox.db").get_color_names_list()
106+
for _, color_name in pairs(ColorNamesList) do
107+
local spec = ColorNameToColorSpecsMap[color_name]
108+
if not _should_filter(color_name, spec) then
109+
table.insert(FilteredColorNamesList, color_name)
119110
end
120111
end
121-
-- logger.debug(
122-
-- "|colorbox._init| before sort ColorNamesList:%s",
123-
-- vim.inspect(ColorNamesList)
124-
-- )
125-
table.sort(ColorNamesList, function(a, b)
126-
return a:lower() < b:lower()
127-
end)
128112
logger.debug(
129-
"|colorbox._init| ColorNamesList:%s",
130-
vim.inspect(ColorNamesList)
113+
"|colorbox._init| FilteredColorNamesList:%s",
114+
vim.inspect(FilteredColorNamesList)
131115
)
132116
end
133117

134118
local function _policy_shuffle()
135-
if #ColorNamesList > 0 then
136-
local r = utils.randint(#ColorNamesList)
137-
local color = ColorNamesList[r + 1]
119+
if #FilteredColorNamesList > 0 then
120+
local r = utils.randint(#FilteredColorNamesList)
121+
local color = FilteredColorNamesList[r + 1]
138122
-- logger.debug(
139123
-- "|colorbox._policy_shuffle| color:%s, ColorNames:%s (%d), r:%d",
140124
-- vim.inspect(color),

lua/colorbox/db.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ local GitPathToColorSpecsMap = nil
8989
--- @type table<string, colorbox.ColorSpec>
9090
local ColorNameToColorSpecsMap = nil
9191

92+
-- color names list
93+
--- @type string[]
94+
local ColorNamesList = nil
95+
9296
do
9397
if type(HandleToColorSpecsMap) ~= "table" then
9498
local cwd = vim.fn["colorbox#base_dir"]()
@@ -127,6 +131,17 @@ do
127131
GitPathToColorSpecsMap[spec.git_path] = spec
128132
end
129133
end
134+
if type(ColorNamesList) ~= "table" then
135+
ColorNamesList = {}
136+
for _, spec in pairs(HandleToColorSpecsMap) do
137+
for _, color_name in ipairs(spec.color_names) do
138+
table.insert(ColorNamesList, color_name)
139+
end
140+
end
141+
table.sort(ColorNamesList, function(a, b)
142+
return a:lower() < b:lower()
143+
end)
144+
end
130145
end
131146

132147
--- @return table<string, colorbox.ColorSpec>
@@ -144,10 +159,16 @@ local function get_color_name_to_color_specs_map()
144159
return ColorNameToColorSpecsMap
145160
end
146161

162+
--- @return string[]
163+
local function get_color_names_list()
164+
return ColorNamesList
165+
end
166+
147167
local M = {
148168
get_handle_to_color_specs_map = get_handle_to_color_specs_map,
149169
get_git_path_to_color_specs_map = get_git_path_to_color_specs_map,
150170
get_color_name_to_color_specs_map = get_color_name_to_color_specs_map,
171+
get_color_names_list = get_color_names_list,
151172
}
152173

153174
return M

test/db_spec.lua

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
local cwd = vim.fn.getcwd()
2+
3+
describe("db", 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 db = require("colorbox.db")
13+
describe("[get_xxxapi]", function()
14+
it("get_handle_to_color_specs_map", function()
15+
local HandleToColorSpecsMap = db.get_handle_to_color_specs_map()
16+
assert_eq(type(HandleToColorSpecsMap), "table")
17+
end)
18+
it("get_git_path_to_color_specs_map", function()
19+
local GitPathToColorSpecsMap = db.get_git_path_to_color_specs_map()
20+
assert_eq(type(GitPathToColorSpecsMap), "table")
21+
end)
22+
it("get_git_path_to_color_specs_map", function()
23+
local ColorNameToColorSpecsMap =
24+
db.get_color_name_to_color_specs_map()
25+
assert_eq(type(ColorNameToColorSpecsMap), "table")
26+
end)
27+
it("get_color_names_list", function()
28+
local ColorNamesList = db.get_color_names_list()
29+
assert_eq(type(ColorNamesList), "table")
30+
end)
31+
end)
32+
end)

0 commit comments

Comments
 (0)