From 5e2dd8ee9227ff8dcecbed037229a80ecfd3a003 Mon Sep 17 00:00:00 2001 From: Thomas Ingham Date: Wed, 31 Aug 2022 13:18:38 -0400 Subject: [PATCH 1/2] Attempting to add per-instance options --- lua/specs/init.lua | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/lua/specs/init.lua b/lua/specs/init.lua index 4857275..d3dad3b 100644 --- a/lua/specs/init.lua +++ b/lua/specs/init.lua @@ -40,33 +40,56 @@ function M.should_show_specs(start_win_id) return true end -function M.show_specs() +function M.show_specs(popup) local start_win_id = vim.api.nvim_get_current_win() if not M.should_show_specs(start_win_id) then return end + local function dcopy (t) + local orig_type = type(orig) + local copy + if orig_type == 'table' then + copy = {} + for orig_key, orig_value in next, orig, nil do + copy[deepcopy(orig_key)] = deepcopy(orig_value) + end + setmetatable(copy, deepcopy(getmetatable(orig))) + else -- number, string, boolean, etc + copy = orig + end + return copy + end + + if popup == nil then + popup = opts.popup + end + + local originalOptions = deep_copy(opts) + local instanceOptions = deep_copy(opts) + instanceOptions.popup = popup + local cursor_col = vim.fn.wincol()-1 local cursor_row = vim.fn.winline()-1 local bufh = vim.api.nvim_create_buf(false, true) local win_id = vim.api.nvim_open_win(bufh, false, { - relative='win', + relative= 'win', width = 1, height = 1, col = cursor_col, row = cursor_row, style = 'minimal' }) - vim.api.nvim_win_set_option(win_id, 'winhl', 'Normal:'.. opts.popup.winhl) - vim.api.nvim_win_set_option(win_id, "winblend", opts.popup.blend) + vim.api.nvim_win_set_option(win_id, 'winhl', 'Normal:'.. instanceOptions.popup.winhl) + vim.api.nvim_win_set_option(win_id, "winblend", instanceOptions.popup.blend) local cnt = 0 local config = vim.api.nvim_win_get_config(win_id) local timer = vim.loop.new_timer() local closed = false - vim.loop.timer_start(timer, opts.popup.delay_ms, opts.popup.inc_ms, vim.schedule_wrap(function() + vim.loop.timer_start(timer, instanceOptions.popup.delay_ms, instanceOptions.popup.inc_ms, vim.schedule_wrap(function() if closed or vim.api.nvim_get_current_win() ~= start_win_id then if not closed then pcall(vim.loop.close, timer) @@ -75,14 +98,15 @@ function M.show_specs() -- Callbacks might stack up before the timer actually gets closed, track that state -- internally here instead closed = true + opts = originalOptions end return end if vim.api.nvim_win_is_valid(win_id) then - local bl = opts.popup.fader(opts.popup.blend, cnt) - local dm = opts.popup.resizer(opts.popup.width, cursor_col, cnt) + local bl = instanceOptions.popup.fader(instanceOptions.popup.blend, cnt) + local dm = instanceOptions.popup.resizer(instanceOptions.popup.width, cursor_col, cnt) if bl ~= nil then vim.api.nvim_win_set_option(win_id, "winblend", bl) From c61eac8eaf8262833ce9824538fa3e3732a3bf1c Mon Sep 17 00:00:00 2001 From: Thomas Ingham Date: Wed, 31 Aug 2022 14:16:46 -0400 Subject: [PATCH 2/2] Actually made it work.. nice. --- README.md | 4 ++++ lua/specs/init.lua | 41 ++++++++++++++++------------------------- 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 8576fcb..2ca91ae 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ require('specs').setup{ - Toggle Specs on/off Faders: +- sinus_fader `⌣/⌢\⌣/⌢\⌣/⌢\⌣/⌢` - linear_fader `▁▂▂▃▃▄▄▅▅▆▆▇▇██` - exp_fader `▁▁▁▁▂▂▂▃▃▃▄▄▅▆▇` @@ -93,6 +94,9 @@ vim.api.nvim_set_keymap('n', '', ':lua require("specs").show_specs()', { no -- You can even bind it to search jumping and more, example: vim.api.nvim_set_keymap('n', 'n', 'n:lua require("specs").show_specs()', { noremap = true, silent = true }) vim.api.nvim_set_keymap('n', 'N', 'N:lua require("specs").show_specs()', { noremap = true, silent = true }) + +-- Or maybe you do a lot of screen-casts and want to call attention to a specific line of code: +vim.api.nvim_set_keymap('n', 'v', ':lua require("specs").show_specs({width = 97, winhl = "Search", delay_ms = 610, inc_ms = 21})', { noremap = true, silent = true }) ``` ## Planned Features diff --git a/lua/specs/init.lua b/lua/specs/init.lua index d3dad3b..e72ee08 100644 --- a/lua/specs/init.lua +++ b/lua/specs/init.lua @@ -47,28 +47,11 @@ function M.show_specs(popup) return end - local function dcopy (t) - local orig_type = type(orig) - local copy - if orig_type == 'table' then - copy = {} - for orig_key, orig_value in next, orig, nil do - copy[deepcopy(orig_key)] = deepcopy(orig_value) - end - setmetatable(copy, deepcopy(getmetatable(orig))) - else -- number, string, boolean, etc - copy = orig - end - return copy - end - if popup == nil then - popup = opts.popup + popup = {} end - local originalOptions = deep_copy(opts) - local instanceOptions = deep_copy(opts) - instanceOptions.popup = popup + local _opts = vim.tbl_deep_extend("force", opts, {popup = popup}) local cursor_col = vim.fn.wincol()-1 local cursor_row = vim.fn.winline()-1 @@ -81,15 +64,15 @@ function M.show_specs(popup) row = cursor_row, style = 'minimal' }) - vim.api.nvim_win_set_option(win_id, 'winhl', 'Normal:'.. instanceOptions.popup.winhl) - vim.api.nvim_win_set_option(win_id, "winblend", instanceOptions.popup.blend) + vim.api.nvim_win_set_option(win_id, 'winhl', 'Normal:'.. _opts.popup.winhl) + vim.api.nvim_win_set_option(win_id, "winblend", _opts.popup.blend) local cnt = 0 local config = vim.api.nvim_win_get_config(win_id) local timer = vim.loop.new_timer() local closed = false - vim.loop.timer_start(timer, instanceOptions.popup.delay_ms, instanceOptions.popup.inc_ms, vim.schedule_wrap(function() + vim.loop.timer_start(timer, _opts.popup.delay_ms, _opts.popup.inc_ms, vim.schedule_wrap(function() if closed or vim.api.nvim_get_current_win() ~= start_win_id then if not closed then pcall(vim.loop.close, timer) @@ -98,15 +81,14 @@ function M.show_specs(popup) -- Callbacks might stack up before the timer actually gets closed, track that state -- internally here instead closed = true - opts = originalOptions end return end if vim.api.nvim_win_is_valid(win_id) then - local bl = instanceOptions.popup.fader(instanceOptions.popup.blend, cnt) - local dm = instanceOptions.popup.resizer(instanceOptions.popup.width, cursor_col, cnt) + local bl = _opts.popup.fader(_opts.popup.blend, cnt) + local dm = _opts.popup.resizer(_opts.popup.width, cursor_col, cnt) if bl ~= nil then vim.api.nvim_win_set_option(win_id, "winblend", bl) @@ -123,6 +105,7 @@ function M.show_specs(popup) cnt = cnt+1 end end)) + end --[[ ▁▁▂▂▃▃▄▄▅▅▆▆▇▇██ ]]-- @@ -133,6 +116,14 @@ function M.linear_fader(blend, cnt) else return nil end end +--[[ ⌣/⌢\⌣/⌢\⌣/⌢\⌣/⌢\ ]]-- + +function M.sinus_fader(blend, cnt) + if cnt <= 100 then + return math.ceil((math.sin(cnt * (1 / blend)) * 0.5 + 0.5) * 100) + else return nil end +end + --[[ ▁▁▁▁▂▂▂▃▃▃▄▄▅▆▇ ]]--