Skip to content

Commit

Permalink
feat(hook): add 'post_hook' function hooks (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
linrongbin16 committed Jun 4, 2024
1 parent a93736b commit 9f91613
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 62 deletions.
125 changes: 69 additions & 56 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ And multiple trigger timings:
- [By Fixed Interval Time](#by-fixed-interval-time)
- [By File Type](#by-file-type)
- [Background](#background)
- [Hook](#hook)
- [Receipts](#-receipts)
- [1. Choose fixed color on nvim start](#1-choose-fixed-color-on-nvim-start)
- [2. Change random color per second](#2-change-random-color-per-second)
Expand Down Expand Up @@ -244,8 +245,8 @@ To choose a color on nvim start, please use:

```lua
require("colorbox").setup({
timing = "startup",
policy = "shuffle",
timing = "startup",
policy = "shuffle",
})
```

Expand All @@ -262,8 +263,8 @@ To choose a color on a fixed interval time, please use:

```lua
require("colorbox").setup({
timing = "interval",
policy = { seconds = 60, implement = "in_order" },
timing = "interval",
policy = { seconds = 60, implement = "in_order" },
})
```

Expand All @@ -278,17 +279,17 @@ To choose a color on buffer's file type change, please use:

```lua
require("colorbox").setup({
timing = "filetype",
policy = {
mapping = {
lua = "PaperColor",
yaml = "everforest",
markdown = "kanagawa",
python = "iceberg",
},
empty = "tokyonight",
fallback = "solarized8",
timing = "filetype",
policy = {
mapping = {
lua = "PaperColor",
yaml = "everforest",
markdown = "kanagawa",
python = "iceberg",
},
empty = "tokyonight",
fallback = "solarized8",
},
})
```

Expand All @@ -306,103 +307,115 @@ If you want to bring the dark-able colors back to `dark`, please use:

```lua
require("colorbox").setup({
background = "dark",
background = "dark",
})
```

It automatically `set background=dark` before run a `colorscheme` command.

### Hook

To execute a hook function after policy is triggered and new colorscheme is applied, please use:

```lua
require("colorbox").setup({
post_hook = function(color_name)
vim.notify(string.format("Colorscheme changed to: %s", vim.inspect(color_name)))
end,
})
```

## 📝 Receipts

### 1. Choose fixed color on nvim start

```lua
require("colorbox").setup({
policy = "single",
timing = "startup",
policy = "single",
timing = "startup",
})
```

### 2. Change random color per second

```lua
require("colorbox").setup({
policy = { seconds = 1, implement = "shuffle" },
timing = "interval",
policy = { seconds = 1, implement = "shuffle" },
timing = "interval",
})
```

### 3. Enable all colors

```lua
require("colorbox").setup({
filter = false,
filter = false,
})
```

### 4. Enable only top stars (≥ 1000) & primary colors

```lua
require("colorbox").setup({
filter = {
"primary",
function(color, spec)
return spec.github_stars >= 1000
end
},
filter = {
"primary",
function(color, spec)
return spec.github_stars >= 1000
end
},
})
```

### 5. Disable by name

```lua
local function colorname_disabled(colorname)
for _, c in ipairs({
"iceberg",
"ayu",
"edge",
"nord",
}) do
if string.lower(c) == string.lower(colorname) then
return true
end
for _, c in ipairs({
"iceberg",
"ayu",
"edge",
"nord",
}) do
if string.lower(c) == string.lower(colorname) then
return true
end
return false
end
return false
end

require("colorbox").setup({
filter = function(color, spec)
for _, c in ipairs(spec.color_names) do
if colorname_disabled(c) then
return false
end
end
return true
filter = function(color, spec)
for _, c in ipairs(spec.color_names) do
if colorname_disabled(c) then
return false
end
end
return true
end
})
```

### 6. Disable by plugin

```lua
local function plugin_disabled(spec)
for _, p in ipairs({
"cocopon/iceberg.vim",
"folke/tokyonight.nvim",
"ayu-theme/ayu-vim",
"shaunsingh/nord.nvim",
}) do
if string.lower(p) == string.lower(spec.handle) then
return true
end
for _, p in ipairs({
"cocopon/iceberg.vim",
"folke/tokyonight.nvim",
"ayu-theme/ayu-vim",
"shaunsingh/nord.nvim",
}) do
if string.lower(p) == string.lower(spec.handle) then
return true
end
return false
end
return false
end

require("colorbox").setup({
filter = function(color, spec)
return not plugin_disabled(spec)
end
filter = function(color, spec)
return not plugin_disabled(spec)
end
})
```

Expand Down
4 changes: 4 additions & 0 deletions lua/colorbox/configs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ local Defaults = {
desc = "Colorschemes player controller",
},

-- (Optional) hook lua function after colorscheme is been choose.
--- @type function(color_name: string, color_spec: colorbox.ColorSpec):nil|nil
post_hook = nil,

--- @type string
cache_dir = string.format("%s/colorbox.nvim", vim.fn.stdpath("data")),

Expand Down
18 changes: 13 additions & 5 deletions lua/colorbox/controller.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ M.update = function()
file_log_mode = "w",
})
end
local logger = logging.get("colorbox-update") --[[@as commons.logging.Logger]]
local logger = logging.get("colorbox-update")

local home_dir = vim.fn["colorbox#base_dir"]()
local packstart = string.format("%s/pack/colorbox/start", home_dir)
Expand Down Expand Up @@ -140,6 +140,7 @@ end

--- @param args string?
M.info = function(args)
local logger = logging.get("colorbox")
local opts = M._parse_args(args)
opts = opts or { scale = 0.7 }
opts.scale = type(opts.scale) == "string" and (tonumber(opts.scale) or 0.7) or 0.7
Expand Down Expand Up @@ -230,10 +231,17 @@ M.info = function(args)
})
lineno = lineno + 1
local color_names = vim.deepcopy(spec.color_names)
table.sort(color_names, function(a, b)
local a_enabled = ColorNamesIndex[a] ~= nil
return a_enabled
end)
-- table.sort(color_names, function(a, b)
-- logger:debug(
-- string.format(
-- "|info| sort color_names:%s, ColorNamesIndex:%s",
-- vim.inspect(color_names),
-- vim.inspect(ColorNamesIndex)
-- )
-- )
-- local a_enabled = ColorNamesIndex[a] ~= nil
-- return a_enabled
-- end)

for _, color in ipairs(color_names) do
local enabled = ColorNamesIndex[color] ~= nil
Expand Down
2 changes: 1 addition & 1 deletion lua/colorbox/filter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ M.run = function(color_name, spec)
elseif type(confs.filter) == "table" then
return M._all_filter(confs.filter, color_name, spec)
end
return false
return true
end

return M
8 changes: 8 additions & 0 deletions lua/colorbox/track.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local logging = require("colorbox.commons.logging")

local configs = require("colorbox.configs")
local runtime = require("colorbox.runtime")
local db = require("colorbox.db")

local M = {}

Expand Down Expand Up @@ -42,6 +43,13 @@ M.save_track = function(color_name)
vim.inspect(color_number)
)
)
vim.schedule(function()
if vim.is_callable(confs.post_hook) then
local ColorNameToColorSpecsMap = db.get_color_name_to_color_specs_map()
local color_spec = ColorNameToColorSpecsMap[color_name]
confs.post_hook(color_name, color_spec)
end
end)
end)
end)
end
Expand Down

0 comments on commit 9f91613

Please sign in to comment.