@@ -4,6 +4,7 @@ local jsons = require("colorbox.commons.jsons")
4
4
local uv = require (" colorbox.commons.uv" )
5
5
local numbers = require (" colorbox.commons.numbers" )
6
6
local fileios = require (" colorbox.commons.fileios" )
7
+ local strings = require (" colorbox.commons.strings" )
7
8
8
9
--- @alias colorbox.Options table<any , any>
9
10
--- @type colorbox.Options
@@ -146,6 +147,7 @@ local function _init()
146
147
-- vim.opt.packpath:append(cwd .. "/pack/colorbox/opt")
147
148
-- vim.cmd([[packadd catppuccin-nvim]])
148
149
150
+ local logger = logging .get (" colorbox" ) --[[ @as commons.logging.Logger]]
149
151
local ColorNameToColorSpecsMap =
150
152
require (" colorbox.db" ).get_color_name_to_color_specs_map ()
151
153
local ColorNamesList = require (" colorbox.db" ).get_color_names_list ()
@@ -159,14 +161,14 @@ local function _init()
159
161
for i , color_name in ipairs (FilteredColorNamesList ) do
160
162
FilteredColorNameToIndexMap [color_name ] = i
161
163
end
162
- -- logger. debug(
163
- -- "|colorbox. _init| FilteredColorNamesList:%s",
164
- -- vim.inspect(FilteredColorNamesList)
165
- -- )
166
- -- logger. debug(
167
- -- "|colorbox. _init| FilteredColorNameToIndexMap:%s",
168
- -- vim.inspect(FilteredColorNameToIndexMap)
169
- -- )
164
+ logger : debug (
165
+ " | _init| FilteredColorNamesList:%s" ,
166
+ vim .inspect (FilteredColorNamesList )
167
+ )
168
+ logger : debug (
169
+ " | _init| FilteredColorNameToIndexMap:%s" ,
170
+ vim .inspect (FilteredColorNameToIndexMap )
171
+ )
170
172
end
171
173
172
174
local function _force_sync_syntax ()
@@ -224,18 +226,40 @@ local function _get_prev_color_name_by_idx(idx)
224
226
return FilteredColorNamesList [idx ]
225
227
end
226
228
229
+ local function randint (n )
230
+ local secs , millis = uv .gettimeofday ()
231
+ local pid = uv .os_getpid ()
232
+
233
+ secs = tonumber (secs ) or math.random (n )
234
+ millis = tonumber (millis ) or math.random (n )
235
+ pid = tonumber (pid ) or math.random (n )
236
+
237
+ local total = numbers .mod (
238
+ numbers .mod (secs + millis , numbers .INT32_MAX ) + pid ,
239
+ numbers .INT32_MAX
240
+ )
241
+
242
+ local chars = strings .tochars (tostring (total )) --[[ @as string[] ]]
243
+ chars = numbers .shuffle (chars ) --[[ @as string[] ]]
244
+ return numbers .mod (tonumber (table.concat (chars , " " )) or math.random (n ), n )
245
+ + 1
246
+ end
247
+
227
248
local function _policy_shuffle ()
228
249
if # FilteredColorNamesList > 0 then
229
- local i = math.random (# FilteredColorNamesList )
250
+ local i = randint (# FilteredColorNamesList )
230
251
local color = _get_next_color_name_by_idx (i )
231
- -- logger.debug(
232
- -- "|colorbox._policy_shuffle| color:%s, ColorNames:%s (%d), r:%d",
233
- -- vim.inspect(color),
234
- -- vim.inspect(ColorNames),
235
- -- vim.inspect()
236
- -- )
237
- --- @diagnostic disable-next-line : param-type-mismatch
238
- local ok , err = pcall (vim .cmd , string.format ([[ color %s]] , color ))
252
+ logging .get (" colorbox" ):debug (
253
+ " |_policy_shuffle| color:%s, FilteredColorNamesList:%s (%d), i:%d" ,
254
+ vim .inspect (color ),
255
+ vim .inspect (FilteredColorNamesList ),
256
+ vim .inspect (# FilteredColorNamesList ),
257
+ vim .inspect (i )
258
+ )
259
+ local ok , err = pcall (
260
+ vim .cmd --[[ @as function]] ,
261
+ string.format ([[ color %s]] , color )
262
+ )
239
263
assert (ok , err )
240
264
end
241
265
end
@@ -421,6 +445,7 @@ local function update(opts)
421
445
file_log_name = " colorbox_update.log" ,
422
446
file_log_mode = " w" ,
423
447
})
448
+ local logger = logging .get (" colorbox-update" ) --[[ @as commons.logging.Logger]]
424
449
425
450
local home_dir = vim .fn [" colorbox#base_dir" ]()
426
451
-- local packstart = string.format("%s/pack/colorbox/start", home_dir)
@@ -459,21 +484,21 @@ local function update(opts)
459
484
for handle , spec in pairs (HandleToColorSpecsMap ) do
460
485
local function _on_output (chanid , data , name )
461
486
if type (data ) == " table" then
462
- logging . get ( " colorbox-update " ) :debug (
487
+ logger :debug (
463
488
" (%s) %s: %s" ,
464
489
vim .inspect (name ),
465
490
vim .inspect (handle ),
466
491
vim .inspect (data )
467
492
)
468
493
for _ , d in ipairs (data ) do
469
494
if type (d ) == " string" and string.len (vim .trim (d )) > 0 then
470
- logging . get ( " colorbox-update " ) :info (" %s: %s" , handle , d )
495
+ logger :info (" %s: %s" , handle , d )
471
496
end
472
497
end
473
498
end
474
499
end
475
500
local function _on_exit (jid , exitcode , name )
476
- logging . get ( " colorbox-update " ) :debug (
501
+ logger :debug (
477
502
" (%s-%s) %s: exit with %s" ,
478
503
vim .inspect (name ),
479
504
vim .inspect (jid ),
@@ -490,14 +515,14 @@ local function update(opts)
490
515
end
491
516
end
492
517
if not removed_from_working_queue then
493
- logging . get ( " colorbox-update " ) :err (
518
+ logger :err (
494
519
" failed to remove job id %s from jobs_working_queue: %s" ,
495
520
vim .inspect (jid ),
496
521
vim .inspect (jobs_working_queue )
497
522
)
498
523
end
499
524
if jobid_to_jobs_map [jid ] == nil then
500
- logging . get ( " colorbox-update " ) :err (
525
+ logger :err (
501
526
" failed to remove job id %s from jobid_to_jobs_map: %s" ,
502
527
vim .inspect (jid ),
503
528
vim .inspect (jobid_to_jobs_map )
@@ -606,6 +631,7 @@ local function _clean()
606
631
-- )
607
632
return
608
633
end
634
+ local logger = logging .get (" colorbox" ) --[[ @as commons.logging.Logger]]
609
635
if vim .fn .executable (" rm" ) > 0 then
610
636
local jobid = vim .fn .jobstart ({ " rm" , " -rf" , full_pack_dir }, {
611
637
detach = false ,
@@ -634,9 +660,9 @@ local function _clean()
634
660
end ,
635
661
})
636
662
vim .fn .jobwait ({ jobid })
637
- logging . get ( " colorbox " ) :info (" cleaned directory: %s" , shorten_pack_dir )
663
+ logger :info (" cleaned directory: %s" , shorten_pack_dir )
638
664
else
639
- logging . get ( " colorbox " ) :warn (" no 'rm' command found, skip cleaning..." )
665
+ logger :warn (" no 'rm' command found, skip cleaning..." )
640
666
end
641
667
end
642
668
@@ -699,6 +725,8 @@ local function setup(opts)
699
725
vim .api .nvim_create_user_command (
700
726
Configs .command .name ,
701
727
function (command_opts )
728
+ local logger = logging .get (" colorbox" ) --[[ @as commons.logging.Logger]]
729
+
702
730
-- logger.debug(
703
731
-- "|colorbox.setup| command opts:%s",
704
732
-- vim.inspect(command_opts)
@@ -712,17 +740,15 @@ local function setup(opts)
712
740
-- vim.inspect(args_splits)
713
741
-- )
714
742
if # args_splits == 0 then
715
- logging . get ( " colorbox " ) :warn (" missing parameter." )
743
+ logger :warn (" missing parameter." )
716
744
return
717
745
end
718
746
if type (CONTROLLERS_MAP [args_splits [1 ]]) == " function" then
719
747
local fn = CONTROLLERS_MAP [args_splits [1 ]]
720
748
local sub_args = args :sub (string.len (args_splits [1 ]) + 1 )
721
749
fn (sub_args )
722
750
else
723
- logging
724
- .get (" colorbox" )
725
- :warn (" unknown parameter %s." , args_splits [1 ])
751
+ logger :warn (" unknown parameter %s." , args_splits [1 ])
726
752
end
727
753
end ,
728
754
{
0 commit comments