Skip to content

Commit f0fcd55

Browse files
authored
feat(fixed interval): support fixed interval timing and policy (#59)
1 parent 1576651 commit f0fcd55

File tree

3 files changed

+137
-8
lines changed

3 files changed

+137
-8
lines changed

README.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ I'm greedy that I want all the **most popular** Neovim colorschemes than only on
1414

1515
Let's load all the ultra colorschemes into your Neovim player!
1616

17+
![shuffle-per-seconds-v1](https://github.com/linrongbin16/colorbox.nvim/assets/6496887/74cc205f-fbf6-4edb-abdd-72aaea27b50b)
18+
19+
<details>
20+
<summary><i>Click here to see how to configure</i></summary>
21+
22+
```lua
23+
require('colorbox').setup({
24+
policy = { name = "interval", seconds = 1, implement = "shuffle" },
25+
timing = "interval",
26+
})
27+
```
28+
29+
</details>
30+
1731
It use offline github actions to weekly collect and update the most popular Vim/Neovim colorscheme list.
1832

1933
> The **most popular** colorschemes are picked from below websites:
@@ -57,6 +71,8 @@ And multiple trigger timings (colorschemes don't have end time):
5771
- [lazy.nvim](#lazynvim)
5872
- [pckr.nvim](#pckrnvim)
5973
- [Configuration](#-configuration)
74+
- [On Startup](#on-startup)
75+
- [Fixed Interval](#fixed-interval)
6076
- [Development](#-development)
6177
- [Contribute](#-contribute)
6278

@@ -122,7 +138,16 @@ You can use command `Colorbox` to control the colorschemes player:
122138

123139
```lua
124140
require('colorbox').setup({
125-
--- @type "shuffle"|"in_order"|"reverse_order"|"single"
141+
-- by filetype policy: filetype => color name
142+
--- @alias ByFileTypePolicyConfigFileType string
143+
--- @alias ByFileTypePolicyConfigColorName string
144+
--- @alias ByFileTypePolicyConfig {name:"filetype",mapping:table<ByFileTypePolicyConfigFileType, ByFileTypePolicyConfigColorName>}
145+
---
146+
-- fixed interval seconds
147+
--- @alias FixedIntervalPolicyConfig {name:"interval",seconds:integer,implement:"shuffle"|"in_order"|"reverse_order"|"single"}
148+
---
149+
--- @alias PolicyConfig "shuffle"|"in_order"|"reverse_order"|"single"|ByFileTypePolicyConfig|FixedIntervalPolicyConfig
150+
--- @type PolicyConfig
126151
policy = "shuffle",
127152

128153
--- @type "startup"|"interval"|"filetype"
@@ -172,6 +197,28 @@ require('colorbox').setup({
172197
})
173198
```
174199

200+
### On Startup
201+
202+
To choose a colorscheme on nvim start, please use:
203+
204+
```lua
205+
require('colorbox').setup({
206+
policy = 'shuffle',
207+
timing = 'startup',
208+
})
209+
```
210+
211+
### Fixed Interval
212+
213+
To choose a colorscheme on fixed interval per seconds, please use:
214+
215+
```lua
216+
require('colorbox').setup({
217+
policy = { name = "interval", seconds = 1, implement = "shuffle" },
218+
timing = 'interval',
219+
})
220+
```
221+
175222
## ✏️ Development
176223

177224
To develop the project and make PR, please setup with:

lua/colorbox.lua

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,28 @@ local json = require("colorbox.json")
66
--- @alias colorbox.Options table<any, any>
77
--- @type colorbox.Options
88
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
1031
policy = "shuffle",
1132

1233
--- @type "startup"|"interval"|"filetype"
@@ -126,6 +147,16 @@ local function _init()
126147
)
127148
end
128149

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+
129160
--- @alias PreviousTrack {color_name:string,color_number:integer}
130161
--- @param color_name string
131162
--- @param color_number integer
@@ -162,6 +193,7 @@ local function _policy_shuffle()
162193
-- vim.inspect(ColorNames),
163194
-- vim.inspect()
164195
-- )
196+
_before_running_colorscheme_hook()
165197
vim.cmd(string.format([[color %s]], color))
166198
_save_previous_track(color, i)
167199
end
@@ -176,6 +208,7 @@ local function _policy_in_order()
176208
#FilteredColorNamesList
177209
)
178210
local color = FilteredColorNamesList[i + 1]
211+
_before_running_colorscheme_hook()
179212
vim.cmd(string.format([[color %s]], color))
180213
_save_previous_track(color, i)
181214
end
@@ -191,21 +224,53 @@ local function _policy_reverse_order()
191224
or previous_track.color_number - 1
192225
)
193226
local color = FilteredColorNamesList[i + 1]
227+
_before_running_colorscheme_hook()
194228
vim.cmd(string.format([[color %s]], color))
195229
_save_previous_track(color, i)
196230
end
197231
end
198232

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)
202261
end
262+
impl()
263+
end
264+
265+
local function _policy()
203266
if Configs.policy == "shuffle" then
204267
_policy_shuffle()
205268
elseif Configs.policy == "in_order" then
206269
_policy_in_order()
207270
elseif Configs.policy == "reverse_order" then
208271
_policy_reverse_order()
272+
elseif _is_fixed_interval_policy(Configs.policy) then
273+
_policy_fixed_interval()
209274
end
210275
end
211276

@@ -215,8 +280,23 @@ local function _timing_startup()
215280
})
216281
end
217282

283+
local function _timing_interval()
284+
-- Configs.timing
285+
end
286+
218287
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
220300
end
221301

222302
--- @param opts {concurrency:integer}?

lua/colorbox/utils.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
local int32_max = 2 ^ 31 - 1
2+
13
-- Returns the XOR of two binary numbers
24
--- @param a integer
35
--- @param b integer
@@ -20,7 +22,7 @@ end
2022
--- @param f fun(v:any):number
2123
--- @return number
2224
local function min(l, f)
23-
local result = 2 ^ 31 - 1
25+
local result = int32_max
2426
for _, i in ipairs(l) do
2527
result = math.min(result, f(i))
2628
end
@@ -37,7 +39,6 @@ end
3739
--- @param maximal integer?
3840
--- @return integer
3941
local function randint(maximal)
40-
local int32_max = 2 ^ 31 - 1
4142
maximal = maximal or int32_max
4243
local s1 = vim.loop.getpid()
4344
local s2, s3 = vim.loop.gettimeofday()
@@ -117,6 +118,7 @@ local function readfile(filename, opts)
117118
end
118119

119120
local M = {
121+
int32_max = int32_max,
120122
min = min,
121123
math_mod = math_mod,
122124
randint = randint,

0 commit comments

Comments
 (0)