Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: linrongbin16/colorbox.nvim
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.13.8
Choose a base ref
...
head repository: linrongbin16/colorbox.nvim
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v1.13.9
Choose a head ref
  • 2 commits
  • 7 files changed
  • 2 contributors

Commits on Dec 26, 2023

  1. perf(test): improve unit tests (#145)

    perf(configs): rename 'bufferchanged' to 'filetype' (#145)
    linrongbin16 authored Dec 26, 2023

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    domdfcoding Dominic Davis-Foster
    Copy the full SHA
    eb5393b View commit details
  2. chore(main): release 1.13.9 (#146)

    Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
    github-actions[bot] authored Dec 26, 2023
    Copy the full SHA
    1dff0f3 View commit details
Showing with 103 additions and 44 deletions.
  1. +8 −0 CHANGELOG.md
  2. +3 −3 README.md
  3. +39 −36 lua/colorbox.lua
  4. +11 −3 lua/colorbox/commons/numbers.lua
  5. +1 −1 lua/colorbox/commons/version.txt
  6. +40 −0 test/colorbox_spec.lua
  7. +1 −1 version.txt
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## [1.13.9](https://github.com/linrongbin16/colorbox.nvim/compare/v1.13.8...v1.13.9) (2023-12-26)


### Performance Improvements

* **configs:** rename 'bufferchanged' to 'filetype' ([#145](https://github.com/linrongbin16/colorbox.nvim/issues/145)) ([eb5393b](https://github.com/linrongbin16/colorbox.nvim/commit/eb5393be2b64424ccadbdc297abbb7fa7b4cb679))
* **test:** improve unit tests ([#145](https://github.com/linrongbin16/colorbox.nvim/issues/145)) ([eb5393b](https://github.com/linrongbin16/colorbox.nvim/commit/eb5393be2b64424ccadbdc297abbb7fa7b4cb679))

## [1.13.8](https://github.com/linrongbin16/colorbox.nvim/compare/v1.13.7...v1.13.8) (2023-12-26)


6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -282,7 +282,7 @@ There're 3 types of filter configs:
- `"startup"`: Choose a color on nvim's start.
- `"interval"`: Choose a color after a fixed interval time.
- `"bufferchanged"`: Choose a color when buffer changed.
- `"filetype"`: Choose a color by file type.
- `policy`:
@@ -298,7 +298,7 @@ There're 3 types of filter configs:
- `seconds`: Fixed interval time by seconds.
- `implement`: Internal policy implementation, e.g. `shuffle`, `in_order`, `reverse_order`, `single` builtin policies.
- By filetype policy, works with `timing = "bufferchanged"`.
- By filetype policy, works with `timing = "filetype"`.
- `mapping`: A lua table to map file type to colorscheme.
- `fallback`: Default colorscheme when file type is not mapped.
@@ -351,7 +351,7 @@ require('colorbox').setup({
},
fallback = "solarized8",
},
timing = "bufferchanged",
timing = "filetype",
})
```

75 changes: 39 additions & 36 deletions lua/colorbox.lua
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ local Defaults = {
--- @type colorbox.PolicyConfig
policy = "shuffle",

--- @type "startup"|"interval"|"bufferchanged"
--- @type "startup"|"interval"|"filetype"
timing = "startup",

-- (Optional) filters that disable some colors that you don't want.
@@ -235,7 +235,7 @@ end

local function _policy_shuffle()
if #FilteredColorNamesList > 0 then
local i = numbers.random(#FilteredColorNamesList)
local i = numbers.random(#FilteredColorNamesList) --[[@as integer]]
local color = _get_next_color_name_by_idx(i)
logging.get("colorbox"):debug(
"|_policy_shuffle| color:%s, FilteredColorNamesList:%s (%d), i:%d",
@@ -244,22 +244,16 @@ local function _policy_shuffle()
vim.inspect(#FilteredColorNamesList),
vim.inspect(i)
)
local ok, err = pcall(
vim.cmd --[[@as function]],
string.format([[color %s]], color)
)
assert(ok, err)
vim.cmd(string.format([[color %s]], color))
end
end

local function _policy_in_order()
if #FilteredColorNamesList > 0 then
local previous_track = _load_previous_track() --[[@as colorbox.PreviousTrack]]
local i = previous_track ~= nil and previous_track.color_number or 1
local i = previous_track ~= nil and previous_track.color_number or 0
local color = _get_next_color_name_by_idx(i)
---@diagnostic disable-next-line: param-type-mismatch
local ok, err = pcall(vim.cmd, string.format([[color %s]], color))
assert(ok, err)
vim.cmd(string.format([[color %s]], color))
end
end

@@ -269,25 +263,17 @@ local function _policy_reverse_order()
local i = previous_track ~= nil and previous_track.color_number
or (#FilteredColorNamesList + 1)
local color = _get_prev_color_name_by_idx(i)
---@diagnostic disable-next-line: param-type-mismatch
local ok, err = pcall(vim.cmd, string.format([[color %s]], color))
assert(ok, err)
vim.cmd(string.format([[color %s]], color))
end
end

local function _policy_single()
if #FilteredColorNamesList > 0 then
local previous_track = _load_previous_track() --[[@as colorbox.PreviousTrack]]
local color = nil
if previous_track then
color = previous_track.color_name
else
color = _get_next_color_name_by_idx(0)
end
local color = previous_track ~= nil and previous_track.color_name
or _get_next_color_name_by_idx(0)
if color ~= vim.g.colors_name then
---@diagnostic disable-next-line: param-type-mismatch
local ok, err = pcall(vim.cmd, string.format([[color %s]], color))
assert(ok, err)
vim.cmd(string.format([[color %s]], color))
end
end
end
@@ -340,18 +326,15 @@ local function _policy_by_filetype()

if Configs.policy.mapping[ft] then
local ok, err = pcall(
---@diagnostic disable-next-line: param-type-mismatch
vim.cmd,
vim.cmd --[[@as function]],
string.format([[color %s]], Configs.policy.mapping[ft])
)
assert(ok, err)
else
local ok, err =
---@diagnostic disable-next-line: param-type-mismatch
pcall(
vim.cmd,
string.format([[color %s]], Configs.policy.fallback)
)
local ok, err = pcall(
vim.cmd --[[@as function]],
string.format([[color %s]], Configs.policy.fallback)
)
assert(ok, err)
end
_force_sync_syntax()
@@ -374,7 +357,8 @@ local function _policy()
_policy_fixed_interval()
elseif
Configs.timing == "bufferchanged"
and _is_by_filetype_policy(Configs.policy)
or Configs.timing == "filetype"
and _is_by_filetype_policy(Configs.policy)
then
_policy_by_filetype()
end
@@ -386,7 +370,7 @@ local function _timing_startup()
})
end

local function _timing_buffer_changed()
local function _timing_filetype()
vim.api.nvim_create_autocmd({ "BufNew", "BufReadPre", "BufNewFile" }, {
callback = _policy,
})
@@ -404,15 +388,17 @@ local function _timing()
)
)
_policy_fixed_interval()
elseif Configs.timing == "bufferchanged" then
elseif
Configs.timing == "bufferchanged" or Configs.timing == "filetype"
then
assert(
_is_by_filetype_policy(Configs.policy),
string.format(
"invalid policy %s for 'bufferchanged' timing!",
"invalid policy %s for 'filetype' timing!",
vim.inspect(Configs.policy)
)
)
_timing_buffer_changed()
_timing_filetype()
else
error(string.format("invalid timing %s!", vim.inspect(Configs.timing)))
end
@@ -794,6 +780,14 @@ local function setup(opts)
_timing()
end

local function _get_filtered_color_names_list()
return FilteredColorNamesList
end

local function _get_filtered_color_name_to_index_map()
return FilteredColorNameToIndexMap
end

local M = {
setup = setup,
update = update,
@@ -803,6 +797,15 @@ local M = {
_force_sync_syntax = _force_sync_syntax,
_save_track = _save_track,
_load_previous_track = _load_previous_track,
_get_next_color_name_by_idx = _get_next_color_name_by_idx,
_get_prev_color_name_by_idx = _get_prev_color_name_by_idx,
_get_filtered_color_names_list = _get_filtered_color_names_list,
_get_filtered_color_name_to_index_map = _get_filtered_color_name_to_index_map,
_policy_shuffle = _policy_shuffle,
_policy_in_order = _policy_in_order,
_policy_reverse_order = _policy_reverse_order,
_policy_single = _policy_single,
_policy = _policy,
}

return M
14 changes: 11 additions & 3 deletions lua/colorbox/commons/numbers.lua
Original file line number Diff line number Diff line change
@@ -126,13 +126,21 @@ end

--- @param m integer?
--- @param n integer?
--- @return number?, string?
--- @return number
M.random = function(m, n)
local rand_result, rand_err = require("colorbox.commons.uv").random(4)
if rand_result == nil then
return nil, rand_err
if m == nil and n == nil then
return math.random()
elseif m ~= nil and n == nil then
return math.random(m)
else
return math.random(m --[[@as integer]], n --[[@as integer]])
end
end
local bytes = { string.byte(rand_result, 1, -1) }
local bytes = {
string.byte(rand_result --[[@as string]], 1, -1),
}
local total = 0
for _, b in ipairs(bytes) do
total = M.mod(total * 256 + b, M.INT32_MAX)
2 changes: 1 addition & 1 deletion lua/colorbox/commons/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.4.0
3.4.1
40 changes: 40 additions & 0 deletions test/colorbox_spec.lua
Original file line number Diff line number Diff line change
@@ -74,4 +74,44 @@ describe("colorbox", function()
end
end)
end)
describe(
"[_get_next_color_name_by_idx/_get_prev_color_name_by_idx]",
function()
it("_get_next_color_name_by_idx", function()
local colornames = colorbox._get_filtered_color_names_list()
local colorindexes =
colorbox._get_filtered_color_name_to_index_map()
for i, c in ipairs(colornames) do
local actual = colorbox._get_next_color_name_by_idx(i)
if i == #colornames then
assert_eq(colorindexes[actual], 1)
else
assert_eq(i + 1, colorindexes[actual])
end
end
end)
it("_get_prev_color_name_by_idx", function()
local colornames = colorbox._get_filtered_color_names_list()
local colorindexes =
colorbox._get_filtered_color_name_to_index_map()
for i, c in ipairs(colornames) do
local actual = colorbox._get_prev_color_name_by_idx(i)
if i == 1 then
assert_eq(colorindexes[actual], #colornames)
else
assert_eq(i - 1, colorindexes[actual])
end
end
end)
end
)
describe("[_policy]", function()
it("test", function()
colorbox._policy_shuffle()
colorbox._policy_in_order()
colorbox._policy_reverse_order()
colorbox._policy_single()
colorbox._policy()
end)
end)
end)
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.13.8
1.13.9