Skip to content

Commit 4c51455

Browse files
authored
feat(filter): add 'primary' filter (#21)
feat: MPV done
1 parent 0e7a564 commit 4c51455

File tree

1 file changed

+72
-33
lines changed

1 file changed

+72
-33
lines changed

lua/colorbox.lua

Lines changed: 72 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@ local logger = require("colorbox.logger")
22
local LogLevels = require("colorbox.logger").LogLevels
33
local json = require("colorbox.json")
44

5+
--- @param l any[]
6+
--- @param f fun(v:any):number
7+
--- @return number
8+
local function minimal_integer(l, f)
9+
local result = 2 ^ 31 - 1
10+
for _, i in ipairs(l) do
11+
result = math.min(result, f(i))
12+
end
13+
return result
14+
end
15+
516
local function randint()
617
local s1 = vim.loop.getpid()
718
local s2, s3 = vim.loop.gettimeofday()
@@ -52,28 +63,19 @@ local function string_find(s, t, start)
5263
return nil
5364
end
5465

55-
--- @enum colorbox.PolicyConfigEnum
56-
local PolicyConfigEnum = {
57-
SHUFFLE = "shuffle",
58-
INORDER = "inorder",
59-
SINGLE = "single",
60-
}
61-
62-
--- @enum colorbox.TimingConfigEnum
63-
local TimingConfigEnum = {
64-
STARTUP = "startup",
65-
INTERVAL = "interval",
66-
FILETYPE = "filetype",
67-
}
66+
--- @enum
6867

6968
--- @alias colorbox.Options table<any, any>
7069
--- @type colorbox.Options
7170
local Defaults = {
72-
policy = PolicyConfigEnum.SHUFFLE,
71+
--- @type "shuffle"|"inorder"|"single"
72+
policy = "shuffle",
7373

74-
timing = TimingConfigEnum.STARTUP,
74+
--- @type "startup"|"interval"|"filetype"
75+
timing = "startup",
7576

76-
filter = function() end,
77+
--- @type "primary"|fun(color:string):boolean|nil
78+
filter = nil,
7779

7880
setup = {
7981
["projekt0n/github-nvim-theme"] = function()
@@ -175,6 +177,36 @@ local function _load_repo_meta_urls_map(cwd)
175177
return repos
176178
end
177179

180+
--- @param color string
181+
--- @param spec colorbox.ColorSpec
182+
--- @return boolean
183+
local function _should_filter(color, spec)
184+
if Configs.filter == nil then
185+
return false
186+
end
187+
if Configs.filter == "primary" then
188+
local unique = #spec.colors <= 1
189+
local variants = spec.colors
190+
local shortest = string.len(color)
191+
== minimal_integer(variants, string.len)
192+
local url_splits =
193+
vim.split(spec.url, "/", { plain = true, trimempty = true })
194+
local matched = url_splits[1]:lower() == color:lower()
195+
or url_splits[2]:lower() == color:lower()
196+
logger.debug(
197+
"|colorbox._should_filter| color:%s, spec:%s, unique:%s, shortest: %s, matched:%s",
198+
vim.inspect(color),
199+
vim.inspect(spec),
200+
vim.inspect(unique),
201+
vim.inspect(shortest),
202+
vim.inspect(matched)
203+
)
204+
return not unique and not shortest and not matched
205+
elseif type(Configs.filter) == "function" then
206+
return Configs.filter(color)
207+
end
208+
end
209+
178210
local function _init()
179211
local cwd = vim.fn["colorbox#base_dir"]()
180212
local packstart = string.format("%s/pack/colorbox/start", cwd)
@@ -217,6 +249,7 @@ local function _init()
217249
vim.inspect(err)
218250
)
219251
end
252+
local color_candidates = {}
220253
while true do
221254
local color_tmp = color_dir:readdir()
222255
if type(color_tmp) == "table" and #color_tmp > 0 then
@@ -239,24 +272,32 @@ local function _init()
239272
local color = color_file:sub(1, #color_file - 4)
240273
table.insert(spec.colors, color)
241274
ColorNamesMap[color] = spec
242-
table.insert(ColorNames, color)
275+
table.insert(color_candidates, color)
243276
end
244277
end
245278
else
246279
break
247280
end
248281
end
282+
for _, c in ipairs(color_candidates) do
283+
if not _should_filter(c, spec) then
284+
table.insert(ColorNames, c)
285+
end
286+
end
249287
end
250288
end
289+
logger.debug("|colorbox._init| ColorNames:%s", vim.inspect(ColorNames))
251290
end
252291

253292
local function _policy_shuffle()
254-
local n = 0
255-
for _, spec in pairs(ColorSpecs) do
256-
n = n + #spec.colors
257-
end
258-
local r = math.floor(math.fmod(randint(), n))
293+
local r = math.floor(math.fmod(randint(), #ColorNames))
259294
local color = ColorNames[r + 1]
295+
-- logger.debug(
296+
-- "|colorbox._policy_shuffle| color:%s, ColorNames:%s (%d), r:%d",
297+
-- vim.inspect(color),
298+
-- vim.inspect(ColorNames),
299+
-- vim.inspect()
300+
-- )
260301
vim.cmd(string.format([[color %s]], color))
261302
end
262303

@@ -293,13 +334,12 @@ local function setup(opts)
293334

294335
vim.api.nvim_create_autocmd("ColorSchemePre", {
295336
callback = function(event)
296-
logger.debug("|colorbox.init| event:%s", vim.inspect(event))
337+
logger.debug("|colorbox.setup| event:%s", vim.inspect(event))
297338
if type(event) ~= "table" or ColorNamesMap[event.match] == nil then
298339
return
299340
end
300341
local spec = ColorNamesMap[event.match]
301342
vim.cmd(string.format([[packadd %s]], spec.name))
302-
303343
if
304344
type(Configs.setup) == "table"
305345
and type(Configs.setup[spec.url]) == "function"
@@ -342,16 +382,16 @@ local function update()
342382

343383
local function _on_output(chanid, data, name)
344384
if type(data) == "table" then
385+
logger.debug("%s: %s", vim.inspect(name), vim.inspect(data))
386+
local lines = {}
345387
for _, line in ipairs(data) do
346-
if string.len(line) > 0 then
347-
logger.debug(
348-
"(%s) %s: %s",
349-
vim.inspect(name),
350-
vim.inspect(url),
351-
vim.inspect(line)
352-
)
388+
if string.len(vim.trim(line)) > 0 then
389+
table.insert(lines, line)
353390
end
354391
end
392+
if #lines > 0 then
393+
logger.info(table.concat(lines, ""))
394+
end
355395
end
356396
end
357397

@@ -367,7 +407,6 @@ local function update()
367407
on_stdout = _on_output,
368408
on_stderr = _on_output,
369409
})
370-
logger.info("updating %s", vim.inspect(url))
371410
table.insert(jobs, jobid)
372411
else
373412
local cmd = string.format(
@@ -383,7 +422,7 @@ local function update()
383422
on_stdout = _on_output,
384423
on_stderr = _on_output,
385424
})
386-
logger.info("installing %s", vim.inspect(url))
425+
logger.debug("installing %s", vim.inspect(url))
387426
table.insert(jobs, jobid)
388427
end
389428
end

0 commit comments

Comments
 (0)