@@ -2,6 +2,17 @@ local logger = require("colorbox.logger")
2
2
local LogLevels = require (" colorbox.logger" ).LogLevels
3
3
local json = require (" colorbox.json" )
4
4
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
+
5
16
local function randint ()
6
17
local s1 = vim .loop .getpid ()
7
18
local s2 , s3 = vim .loop .gettimeofday ()
@@ -52,28 +63,19 @@ local function string_find(s, t, start)
52
63
return nil
53
64
end
54
65
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
68
67
69
68
--- @alias colorbox.Options table<any , any>
70
69
--- @type colorbox.Options
71
70
local Defaults = {
72
- policy = PolicyConfigEnum .SHUFFLE ,
71
+ --- @type " shuffle" | " inorder" | " single"
72
+ policy = " shuffle" ,
73
73
74
- timing = TimingConfigEnum .STARTUP ,
74
+ --- @type " startup" | " interval" | " filetype"
75
+ timing = " startup" ,
75
76
76
- filter = function () end ,
77
+ --- @type " primary" | fun ( color : string ): boolean | nil
78
+ filter = nil ,
77
79
78
80
setup = {
79
81
[" projekt0n/github-nvim-theme" ] = function ()
@@ -175,6 +177,36 @@ local function _load_repo_meta_urls_map(cwd)
175
177
return repos
176
178
end
177
179
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
+
178
210
local function _init ()
179
211
local cwd = vim .fn [" colorbox#base_dir" ]()
180
212
local packstart = string.format (" %s/pack/colorbox/start" , cwd )
@@ -217,6 +249,7 @@ local function _init()
217
249
vim .inspect (err )
218
250
)
219
251
end
252
+ local color_candidates = {}
220
253
while true do
221
254
local color_tmp = color_dir :readdir ()
222
255
if type (color_tmp ) == " table" and # color_tmp > 0 then
@@ -239,24 +272,32 @@ local function _init()
239
272
local color = color_file :sub (1 , # color_file - 4 )
240
273
table.insert (spec .colors , color )
241
274
ColorNamesMap [color ] = spec
242
- table.insert (ColorNames , color )
275
+ table.insert (color_candidates , color )
243
276
end
244
277
end
245
278
else
246
279
break
247
280
end
248
281
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
249
287
end
250
288
end
289
+ logger .debug (" |colorbox._init| ColorNames:%s" , vim .inspect (ColorNames ))
251
290
end
252
291
253
292
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 ))
259
294
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
+ -- )
260
301
vim .cmd (string.format ([[ color %s]] , color ))
261
302
end
262
303
@@ -293,13 +334,12 @@ local function setup(opts)
293
334
294
335
vim .api .nvim_create_autocmd (" ColorSchemePre" , {
295
336
callback = function (event )
296
- logger .debug (" |colorbox.init | event:%s" , vim .inspect (event ))
337
+ logger .debug (" |colorbox.setup | event:%s" , vim .inspect (event ))
297
338
if type (event ) ~= " table" or ColorNamesMap [event .match ] == nil then
298
339
return
299
340
end
300
341
local spec = ColorNamesMap [event .match ]
301
342
vim .cmd (string.format ([[ packadd %s]] , spec .name ))
302
-
303
343
if
304
344
type (Configs .setup ) == " table"
305
345
and type (Configs .setup [spec .url ]) == " function"
@@ -342,16 +382,16 @@ local function update()
342
382
343
383
local function _on_output (chanid , data , name )
344
384
if type (data ) == " table" then
385
+ logger .debug (" %s: %s" , vim .inspect (name ), vim .inspect (data ))
386
+ local lines = {}
345
387
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 )
353
390
end
354
391
end
392
+ if # lines > 0 then
393
+ logger .info (table.concat (lines , " " ))
394
+ end
355
395
end
356
396
end
357
397
@@ -367,7 +407,6 @@ local function update()
367
407
on_stdout = _on_output ,
368
408
on_stderr = _on_output ,
369
409
})
370
- logger .info (" updating %s" , vim .inspect (url ))
371
410
table.insert (jobs , jobid )
372
411
else
373
412
local cmd = string.format (
@@ -383,7 +422,7 @@ local function update()
383
422
on_stdout = _on_output ,
384
423
on_stderr = _on_output ,
385
424
})
386
- logger .info (" installing %s" , vim .inspect (url ))
425
+ logger .debug (" installing %s" , vim .inspect (url ))
387
426
table.insert (jobs , jobid )
388
427
end
389
428
end
0 commit comments