From 9f9161338f79b827d3f15ad6b915eb4ffe51e6c1 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Tue, 4 Jun 2024 10:26:39 +0800 Subject: [PATCH] feat(hook): add 'post_hook' function hooks (#238) --- README.md | 125 ++++++++++++++++++++---------------- lua/colorbox/configs.lua | 4 ++ lua/colorbox/controller.lua | 18 ++++-- lua/colorbox/filter.lua | 2 +- lua/colorbox/track.lua | 8 +++ 5 files changed, 95 insertions(+), 62 deletions(-) diff --git a/README.md b/README.md index 95761ed2..0b3d7b43 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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", }) ``` @@ -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" }, }) ``` @@ -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", + }, }) ``` @@ -306,20 +307,32 @@ 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", }) ``` @@ -327,8 +340,8 @@ require("colorbox").setup({ ```lua require("colorbox").setup({ - policy = { seconds = 1, implement = "shuffle" }, - timing = "interval", + policy = { seconds = 1, implement = "shuffle" }, + timing = "interval", }) ``` @@ -336,7 +349,7 @@ require("colorbox").setup({ ```lua require("colorbox").setup({ - filter = false, + filter = false, }) ``` @@ -344,12 +357,12 @@ require("colorbox").setup({ ```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 + }, }) ``` @@ -357,28 +370,28 @@ require("colorbox").setup({ ```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 }) ``` @@ -386,23 +399,23 @@ require("colorbox").setup({ ```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 }) ``` diff --git a/lua/colorbox/configs.lua b/lua/colorbox/configs.lua index 051ed83d..950ae7ac 100644 --- a/lua/colorbox/configs.lua +++ b/lua/colorbox/configs.lua @@ -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")), diff --git a/lua/colorbox/controller.lua b/lua/colorbox/controller.lua index d924a3cd..6f9c4402 100644 --- a/lua/colorbox/controller.lua +++ b/lua/colorbox/controller.lua @@ -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) @@ -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 @@ -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 diff --git a/lua/colorbox/filter.lua b/lua/colorbox/filter.lua index 5b431711..ac4fe82b 100644 --- a/lua/colorbox/filter.lua +++ b/lua/colorbox/filter.lua @@ -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 diff --git a/lua/colorbox/track.lua b/lua/colorbox/track.lua index 44624658..2612a075 100644 --- a/lua/colorbox/track.lua +++ b/lua/colorbox/track.lua @@ -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 = {} @@ -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