Skip to content

Commit 08b9d4b

Browse files
authored
perf(filter)!: enable color when filter returns true (#158)
1 parent 5586be5 commit 08b9d4b

File tree

16 files changed

+1050
-60
lines changed

16 files changed

+1050
-60
lines changed

.github/workflows/ci.yml

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ concurrency:
1010
group: ${{ github.ref }}-ci
1111
cancel-in-progress: true
1212
jobs:
13-
pr_conventional_commit:
14-
name: PR Conventional Commit
13+
conventional_commit:
14+
name: Conventional Commits
1515
if: ${{ github.ref != 'refs/heads/main' }}
1616
runs-on: ubuntu-latest
1717
steps:
@@ -24,25 +24,28 @@ jobs:
2424
runs-on: ubuntu-latest
2525
steps:
2626
- uses: actions/checkout@v4
27-
- uses: lunarmodules/luacheck@v1
28-
with:
29-
args: lua --config .luacheckrc
3027
- name: Install commons.nvim
31-
shell: bash
3228
if: ${{ github.ref != 'refs/heads/main' }}
3329
run: |
3430
git clone --depth=1 https://github.com/linrongbin16/commons.nvim.git ~/.commons.nvim
3531
rm -rf ./lua/colorbox/commons
36-
mkdir -p ./lua/colorbox/commons
37-
cp -rf ~/.commons.nvim/lua/commons/*.lua ./lua/colorbox/commons
32+
cp -rf ~/.commons.nvim/lua/commons ./lua/colorbox/
3833
cp ~/.commons.nvim/version.txt ./lua/colorbox/commons/version.txt
39-
cd ./lua/colorbox/commons
40-
find . -type f -name '*.lua' -exec sed -i 's/require("commons/require("colorbox.commons/g' {} \;
34+
find ./lua/colorbox/commons -type f -name '*.lua' -exec sed -i 's/"commons./"colorbox.commons./g' {} \;
35+
- uses: stevearc/nvim-typecheck-action@v1
36+
with:
37+
path: lua
38+
level: Information
39+
configpath: ".luarc.json"
40+
neodev-version: stable
41+
- uses: lunarmodules/luacheck@v1
42+
with:
43+
args: lua --config .luacheckrc
4144
- uses: JohnnyMorganz/stylua-action@v3
4245
with:
4346
token: ${{ secrets.GITHUB_TOKEN }}
4447
version: latest
45-
args: --config-path .stylua.toml ./lua ./test
48+
args: --config-path .stylua.toml ./lua ./spec
4649
- uses: stefanzweifel/git-auto-commit-action@v4
4750
if: ${{ github.ref != 'refs/heads/main' }}
4851
with:
@@ -66,15 +69,36 @@ jobs:
6669
with:
6770
luaVersion: "luajit-2.1.0-beta3"
6871
- uses: leafo/gh-actions-luarocks@v4
69-
- name: Run test cases
72+
- name: Run Tests
73+
shell: bash
74+
run: |
75+
luarocks install luacheck
76+
luarocks install vusted
77+
vusted --shuffle ./spec
78+
code_coverage:
79+
name: Code Coverage
80+
needs:
81+
- luacheck
82+
runs-on: ubuntu-latest
83+
steps:
84+
- uses: actions/checkout@v4
85+
- uses: rhysd/action-setup-vim@v1
86+
id: vim
87+
with:
88+
neovim: true
89+
version: stable
90+
- uses: leafo/gh-actions-lua@v10
91+
with:
92+
luaVersion: "luajit-2.1.0-beta3"
93+
- uses: leafo/gh-actions-luarocks@v4
94+
- name: Run Tests
7095
shell: bash
7196
run: |
7297
luarocks install luacheck
7398
luarocks install luacov
74-
luarocks install cluacov
7599
luarocks install vusted
76-
vusted --coverage --shuffle ./test
77-
- name: Generate coverage reports
100+
vusted --coverage ./spec
101+
- name: Generate Coverage Report
78102
shell: bash
79103
run: |
80104
echo "ls ."

.luacheckrc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
globals = { "vim", "describe", "before_each", "it", "assert" }
2-
max_line_length = 200
2+
max_line_length = 500
33
unused = false
44
unused_args = false
5-
exclude_files = { "lua/colorbox/commons/*.lua" }
5+
exclude_files = {
6+
"lua/colorbox/commons/*.lua",
7+
}

.luarc.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
{
2-
"diagnostics.globals": ["vim", "describe", "before_each", "it", "jit"],
2+
"diagnostics.globals": [
3+
"vim",
4+
"describe",
5+
"before_each",
6+
"it",
7+
"jit",
8+
"utf8"
9+
],
10+
"diagnostics.disable": [
11+
"undefined-field",
12+
"inject-field",
13+
"deprecated",
14+
"luadoc-miss-module-name",
15+
"undefined-doc-name"
16+
],
17+
"runtime.version": "LuaJIT",
318
"workspace.checkThirdParty": "Disable"
419
}

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ It use offline github actions to weekly collect and update the most popular Vim/
3939
>
4040
> with below conditions:
4141
>
42-
> 1. Github stars ≥ 500 (default config only enables stars ≥ 800, please modify the `filter` option to choose any colors (see [Configuration](#-configuration))).
42+
> 1. Github stars ≥ 500 (default config only enable ≥ 800, please modify the `filter` option to choose any colors, see [Configuration](#-configuration)).
4343
> 2. Last git commit in 3 years.
4444
> 3. For multiple plugins that contain the same color name, choose the one in following rules:
4545
> 1. **Awesome-neovim** wins **vimcolorsheme**, since they usually has modern Neovim features (lua, lsp, treesitter) and support more third-party plugins.
@@ -197,12 +197,13 @@ You can use command `Colorbox` to control the player with below subcommands:
197197

198198
```lua
199199
require('colorbox').setup({
200-
-- Disable those colors you don't want from the candidates list.
200+
-- Only enable those colors you want from the candidates list.
201+
-- Enable color when the (all of those) filter returns `true`.
201202
-- By default only enable primary colorscheme and GitHub stars >= 800.
202203
filter = {
203204
"primary",
204205
function(color, spec)
205-
return spec.github_stars < 800
206+
return spec.github_stars >= 800
206207
end,
207208
},
208209

@@ -247,7 +248,7 @@ require('colorbox').setup({
247248

248249
When choosing a colorscheme, this plugin will run following steps:
249250

250-
- Run the filter, disable those colors you don't want from candidates list. See [Filter](#filter).
251+
- Run the filter, only enable those colors you want from candidates list. See [Filter](#filter).
251252
- Run the policy at a proper timing, and choose a colorscheme. See [Timing & Policy](#timing--policy).
252253
- Refresh the `background` option. See [Background](#background).
253254
- Run the `colorscheme` command to actually change to the colorscheme.
@@ -277,8 +278,8 @@ There're 3 types of filter configs:
277278
>
278279
> Returns:
279280
>
280-
> - To disable a color, returns `true`.
281-
> - To enable a color, returns `false`.
281+
> - To enable a color, returns `true`.
282+
> - To disable a color, returns `false`.
282283
>
283284
> ```lua
284285
> --- @class colorbox.ColorSpec
@@ -295,7 +296,7 @@ There're 3 types of filter configs:
295296
> --- @field full_pack_path string "Users/linrongbin16/github/linrongbin16/colorbox.nvim/pack/colorbox/start/folke-tokyonight.nvim"
296297
> ```
297298
298-
- List filters: A lua list that contains multiple other filters. A color will be disabled if any of those filters returns true.
299+
- List filters: A lua list that contains multiple other filters. A color will only be enabled if **_all_** of those filters returns true.
299300
300301
### Timing & Policy
301302

lua/colorbox.lua

Lines changed: 71 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,18 @@ local Defaults = {
3232
-- builtin filter
3333
--- @alias colorbox.BuiltinFilterConfig "primary"
3434
---
35-
-- function-based filter, disabled if function return true.
35+
-- function-based filter, enabled if function returns true.
3636
--- @alias colorbox.FunctionFilterConfig fun(color:string, spec:colorbox.ColorSpec):boolean
3737
---
38-
---list-based filter, disabled if any of filter hit the conditions.
39-
--- @alias colorbox.AnyFilterConfig (colorbox.BuiltinFilterConfig|colorbox.FunctionFilterConfig)[]
38+
-- list-based all of filter, a color is enabled if all of inside filters returns true.
39+
--- @alias colorbox.AllFilterConfig (colorbox.BuiltinFilterConfig|colorbox.FunctionFilterConfig)[]
4040
---
41-
--- @alias colorbox.FilterConfig colorbox.BuiltinFilterConfig|colorbox.FunctionFilterConfig|colorbox.AnyFilterConfig
41+
--- @alias colorbox.FilterConfig colorbox.BuiltinFilterConfig|colorbox.FunctionFilterConfig|colorbox.AllFilterConfig
4242
--- @type colorbox.FilterConfig?
4343
filter = {
4444
"primary",
4545
function(color, spec)
46-
return spec.github_stars < 800
46+
return spec.github_stars >= 800
4747
end,
4848
},
4949

@@ -99,25 +99,69 @@ end
9999

100100
--- @param color_name string
101101
--- @param spec colorbox.ColorSpec
102-
--- @return boolean
103-
local function _builtin_filter_primary(color_name, spec)
102+
--- @return integer
103+
local function _primary_score(color_name, spec)
104104
local logger = logging.get("colorbox") --[[@as commons.logging.Logger]]
105+
106+
-- unique
105107
local unique = #spec.color_names <= 1
108+
109+
-- shortest
106110
local current_name_len = string.len(color_name)
107111
local minimal_name_len = _minimal_color_name_len(spec)
108112
local shortest = current_name_len == minimal_name_len
113+
114+
-- match
109115
local handle_splits = strings.split(spec.handle, "/")
110-
local matched = handle_splits[1]:lower() == color_name:lower()
111-
or handle_splits[2]:lower() == color_name:lower()
116+
local handle1 = handle_splits[1]:gsub("%-", "_")
117+
local handle2 = handle_splits[2]:gsub("%-", "_")
118+
local normalized_color = color_name:gsub("%-", "_")
119+
local matched = strings.startswith(
120+
handle1,
121+
normalized_color,
122+
{ ignorecase = true }
123+
) or strings.startswith(
124+
handle2,
125+
normalized_color,
126+
{ ignorecase = true }
127+
) or strings.endswith(handle1, normalized_color, { ignorecase = true }) or strings.endswith(
128+
handle2,
129+
normalized_color,
130+
{ ignorecase = true }
131+
)
112132
logger:debug(
113-
"|_builtin_filter_primary| unique:%s, shortest:%s (current:%s, minimal:%s), matched:%s",
133+
"|_primary_score| unique:%s, shortest:%s (current:%s, minimal:%s), matched:%s",
114134
vim.inspect(unique),
115135
vim.inspect(shortest),
116136
vim.inspect(current_name_len),
117137
vim.inspect(minimal_name_len),
118138
vim.inspect(matched)
119139
)
120-
return not unique and not shortest and not matched
140+
local n = 0
141+
if unique then
142+
n = n + 3
143+
end
144+
if matched then
145+
n = n + 2
146+
end
147+
if shortest then
148+
n = n + 1
149+
end
150+
return n
151+
end
152+
153+
--- @param color_name string
154+
--- @param spec colorbox.ColorSpec
155+
--- @return boolean
156+
local function _builtin_filter_primary(color_name, spec)
157+
local color_score = _primary_score(color_name, spec)
158+
for _, other_color in ipairs(spec.color_names) do
159+
local other_score = _primary_score(other_color, spec)
160+
if color_score < other_score then
161+
return false
162+
end
163+
end
164+
return true
121165
end
122166

123167
--- @param f colorbox.BuiltinFilterConfig
@@ -143,35 +187,35 @@ local function _function_filter(f, color_name, spec)
143187
else
144188
logging
145189
.get("colorbox")
146-
:warn(
190+
:err(
147191
"failed to invoke function filter, please check your config!"
148192
)
149193
end
150194
end
151195
return false
152196
end
153197

154-
--- @param f colorbox.AnyFilterConfig
198+
--- @param f colorbox.AllFilterConfig
155199
--- @param color_name string
156200
--- @param spec colorbox.ColorSpec
157201
--- @return boolean
158-
local function _any_filter(f, color_name, spec)
202+
local function _all_filter(f, color_name, spec)
159203
if type(f) == "table" then
160204
for _, f1 in ipairs(f) do
161205
if type(f1) == "string" then
162206
local result = _builtin_filter(f1, color_name, spec)
163-
if result then
207+
if not result then
164208
return result
165209
end
166210
elseif type(f1) == "function" then
167211
local result = _function_filter(f1, color_name, spec)
168-
if result then
212+
if not result then
169213
return result
170214
end
171215
end
172216
end
173217
end
174-
return false
218+
return true
175219
end
176220

177221
--- @param color_name string
@@ -183,7 +227,7 @@ local function _filter(color_name, spec)
183227
elseif type(Configs.filter) == "function" then
184228
return _function_filter(Configs.filter, color_name, spec)
185229
elseif type(Configs.filter) == "table" then
186-
return _any_filter(Configs.filter, color_name, spec)
230+
return _all_filter(Configs.filter, color_name, spec)
187231
end
188232
return false
189233
end
@@ -211,7 +255,7 @@ local function _init()
211255
for _, color_name in pairs(ColorNamesList) do
212256
local spec = ColorNameToColorSpecsMap[color_name]
213257
local pack_exist = uv.fs_stat(spec.full_pack_path) ~= nil
214-
if not _filter(color_name, spec) and pack_exist then
258+
if _filter(color_name, spec) and pack_exist then
215259
table.insert(FilteredColorNamesList, color_name)
216260
end
217261
end
@@ -669,6 +713,12 @@ local function _info(args)
669713
apis.set_buf_option(bufnr, "bufhidden", "wipe")
670714
apis.set_buf_option(bufnr, "buflisted", false)
671715
apis.set_buf_option(bufnr, "filetype", "markdown")
716+
vim.keymap.set(
717+
{ "n" },
718+
"q",
719+
":\\<C-U>quit<CR>",
720+
{ silent = true, buffer = bufnr }
721+
)
672722
local winnr = vim.api.nvim_open_win(bufnr, true, win_config)
673723

674724
local HandleToColorSpecsMap =
@@ -702,7 +752,7 @@ local function _info(args)
702752

703753
vim.api.nvim_buf_set_lines(bufnr, 0, 0, true, {
704754
string.format(
705-
"# ColorSchemes List (total(colors/plugins): %d/%d, enabled(colors/plugins): %d/%d)",
755+
"# ColorSchemes List, total: %d(colors)/%d(plugins), enabled: %d(colors)/%d(plugins)",
706756
total_colors,
707757
total_plugins,
708758
enabled_colors,
@@ -872,7 +922,7 @@ local M = {
872922
_builtin_filter_primary = _builtin_filter_primary,
873923
_builtin_filter = _builtin_filter,
874924
_function_filter = _function_filter,
875-
_any_filter = _any_filter,
925+
_all_filter = _all_filter,
876926
_filter = _filter,
877927

878928
-- misc

0 commit comments

Comments
 (0)