@@ -6,7 +6,28 @@ local json = require("colorbox.json")
6
6
--- @alias colorbox.Options table<any , any>
7
7
--- @type colorbox.Options
8
8
local Defaults = {
9
- --- @type " shuffle" | " in_order" | " reverse_order" | " single"
9
+ -- shuffle playback
10
+ --- @alias ShufflePolicyConfig " shuffle"
11
+ ---
12
+ --- in order
13
+ --- @alias InOrderPolicyConfig " in_order"
14
+ ---
15
+ --- reverse order
16
+ --- @alias ReverseOrderPolicyConfig " reverse_order"
17
+ ---
18
+ --- single cycle
19
+ --- @alias SinglePolicyConfig " single"
20
+ ---
21
+ -- by filetype policy: filetype => color name
22
+ --- @alias ByFileTypePolicyConfigFileType string
23
+ --- @alias ByFileTypePolicyConfigColorName string
24
+ --- @alias ByFileTypePolicyConfig { name : " filetype" , mapping : table<ByFileTypePolicyConfigFileType , ByFileTypePolicyConfigColorName> }
25
+ ---
26
+ -- fixed interval seconds
27
+ --- @alias FixedIntervalPolicyConfig { name : " interval" , seconds : integer,implement : ShufflePolicyConfig | InOrderPolicyConfig | ReverseOrderPolicyConfig | SinglePolicyConfig }
28
+ ---
29
+ --- @alias PolicyConfig ShufflePolicyConfig | InOrderPolicyConfig | ReverseOrderPolicyConfig | SinglePolicyConfig | ByFileTypePolicyConfig | FixedIntervalPolicyConfig
30
+ --- @type PolicyConfig
10
31
policy = " shuffle" ,
11
32
12
33
--- @type " startup" | " interval" | " filetype"
@@ -126,6 +147,16 @@ local function _init()
126
147
)
127
148
end
128
149
150
+ local function _before_running_colorscheme_hook ()
151
+ if Configs .background == " dark" or Configs .background == " light" then
152
+ vim .opt .background = Configs .background
153
+ end
154
+ end
155
+
156
+ local function _after_running_colorscheme_hook ()
157
+ vim .cmd ([[ syntax sync fromstart]] )
158
+ end
159
+
129
160
--- @alias PreviousTrack { color_name : string,color_number : integer }
130
161
--- @param color_name string
131
162
--- @param color_number integer
@@ -162,6 +193,7 @@ local function _policy_shuffle()
162
193
-- vim.inspect(ColorNames),
163
194
-- vim.inspect()
164
195
-- )
196
+ _before_running_colorscheme_hook ()
165
197
vim .cmd (string.format ([[ color %s]] , color ))
166
198
_save_previous_track (color , i )
167
199
end
@@ -176,6 +208,7 @@ local function _policy_in_order()
176
208
# FilteredColorNamesList
177
209
)
178
210
local color = FilteredColorNamesList [i + 1 ]
211
+ _before_running_colorscheme_hook ()
179
212
vim .cmd (string.format ([[ color %s]] , color ))
180
213
_save_previous_track (color , i )
181
214
end
@@ -191,21 +224,53 @@ local function _policy_reverse_order()
191
224
or previous_track .color_number - 1
192
225
)
193
226
local color = FilteredColorNamesList [i + 1 ]
227
+ _before_running_colorscheme_hook ()
194
228
vim .cmd (string.format ([[ color %s]] , color ))
195
229
_save_previous_track (color , i )
196
230
end
197
231
end
198
232
199
- local function _policy ()
200
- if Configs .background == " dark" or Configs .background == " light" then
201
- vim .opt .background = Configs .background
233
+ --- @param po colorbox.Options ?
234
+ local function _is_fixed_interval_policy (po )
235
+ return type (po ) == " table"
236
+ and po .name == " interval"
237
+ and type (po .seconds ) == " number"
238
+ and po .seconds >= 0
239
+ and type (po .implement ) == " string"
240
+ and string.len (po .implement ) > 0
241
+ end
242
+
243
+ local function _policy_fixed_interval ()
244
+ local later = Configs .policy .seconds > 0 and (Configs .policy .seconds * 1000 )
245
+ or utils .int32_max
246
+
247
+ local function impl ()
248
+ if Configs .policy .implement == " shuffle" then
249
+ _policy_shuffle ()
250
+ _after_running_colorscheme_hook ()
251
+ elseif Configs .policy .implement == " in_order" then
252
+ _policy_in_order ()
253
+ _after_running_colorscheme_hook ()
254
+ elseif Configs .policy .implement == " reverse_order" then
255
+ _policy_reverse_order ()
256
+ _after_running_colorscheme_hook ()
257
+ -- elseif Configs.policy.implement == "single" then
258
+ -- -- TODO: implement single cycle
259
+ end
260
+ vim .defer_fn (impl , later )
202
261
end
262
+ impl ()
263
+ end
264
+
265
+ local function _policy ()
203
266
if Configs .policy == " shuffle" then
204
267
_policy_shuffle ()
205
268
elseif Configs .policy == " in_order" then
206
269
_policy_in_order ()
207
270
elseif Configs .policy == " reverse_order" then
208
271
_policy_reverse_order ()
272
+ elseif _is_fixed_interval_policy (Configs .policy ) then
273
+ _policy_fixed_interval ()
209
274
end
210
275
end
211
276
@@ -215,8 +280,23 @@ local function _timing_startup()
215
280
})
216
281
end
217
282
283
+ local function _timing_interval ()
284
+ -- Configs.timing
285
+ end
286
+
218
287
local function _timing ()
219
- _timing_startup ()
288
+ if Configs .timing == " startup" then
289
+ _timing_startup ()
290
+ elseif Configs .timing == " interval" then
291
+ assert (
292
+ _is_fixed_interval_policy (Configs .policy ),
293
+ string.format (
294
+ " invalid policy %s for 'interval' timing!" ,
295
+ vim .inspect (Configs .policy )
296
+ )
297
+ )
298
+ _policy_fixed_interval ()
299
+ end
220
300
end
221
301
222
302
--- @param opts { concurrency : integer }?
0 commit comments