Add user options + popup on cursor jump

This commit is contained in:
edluffy 2021-03-14 19:18:18 +00:00
parent 0450c1008c
commit de54b04bae
2 changed files with 66 additions and 39 deletions

View file

@ -1,51 +1,75 @@
local specs = {} local opts = {}
function specs.show_specs(user_opts) local M = {}
local opts = user_opts or {}
local old_cur
local old_win
function M.on_cursor_moved()
local win = vim.api.nvim_get_current_win()
if old_win then
if(win ~= old_win) then
M.show_specs()
end
end
old_win = win
local cur = vim.api.nvim_win_get_cursor(0)
if old_cur then
jump = math.abs(cur[1]-old_cur[1])
if jump >= opts.min_jump then
M.show_specs()
end
end
-- print(vim.inspect(cur))
old_cur = cur
end
function M.show_specs()
local row = vim.fn.winline()-1 local row = vim.fn.winline()-1
local col = vim.fn.wincol() local col = vim.fn.wincol()
local bufh = vim.api.nvim_create_buf(false, true) local bufh = vim.api.nvim_create_buf(false, true)
win_id = vim.api.nvim_open_win(bufh, false, { win_id = vim.api.nvim_open_win(bufh, false, {
relative='win', relative='win',
width = opts.width*2 + 1, width = opts.popup.width*2 + 1,
height = 1, height = 1,
col = col, col = col,
row = row, row = row,
style = 'minimal' style = 'minimal'
}) })
vim.api.nvim_win_set_option(win_id, "winblend", opts.blend) vim.api.nvim_win_set_option(win_id, "winblend", opts.popup.blend)
local can_fade = true local can_fade = true
local can_resize = true local can_resize = true
local timer = vim.loop.new_timer() local timer = vim.loop.new_timer()
vim.loop.timer_start(timer, opts.delay_ms, opts.inc_ms, vim.schedule_wrap(function() vim.loop.timer_start(timer, opts.popup.delay_ms, opts.popup.inc_ms, vim.schedule_wrap(function()
if vim.api.nvim_win_is_valid(win_id) then if vim.api.nvim_win_is_valid(win_id) then
if can_fade then can_fade = opts.fader(win_id) end if can_fade then can_fade = opts.popup.fader(win_id) end
if can_resize then can_resize = opts.resizer(win_id) end if can_resize then can_resize = opts.popup.resizer(win_id) end
if not (can_blend or can_resize) then if not (can_blend or can_resize) then
vim.loop.close(timer) vim.loop.close(timer)
vim.api.nvim_win_close(win_id, true) vim.api.nvim_win_close(win_id, true)
print("Timer done") -- print("Timer done")
end end
end end
end)) end))
end end
-- Used as the default fader -- Used as the default fader
function specs.linear_fader(win_id) function M.linear_fader(win_id)
local blend = vim.api.nvim_win_get_option(win_id, "winblend") local blend = vim.api.nvim_win_get_option(win_id, "winblend")
vim.api.nvim_win_set_option(win_id, "winblend", blend+1) vim.api.nvim_win_set_option(win_id, "winblend", blend+1)
return (blend+1 < 100) return (blend+1 < 100)
end end
local function pulse_fader(win_id) function M.pulse_fader(win_id)
end end
-- local function exp_fader() -- function M.exp_fader()
-- end -- end
function specs.shrink_resizer(win_id) function M.shrink_resizer(win_id)
local config = vim.api.nvim_win_get_config(win_id) local config = vim.api.nvim_win_get_config(win_id)
config['width'] = config['width']-1 config['width'] = config['width']-1
config['col'][false] = vim.fn.wincol()-config['width']/2 config['col'][false] = vim.fn.wincol()-config['width']/2
@ -53,14 +77,30 @@ function specs.shrink_resizer(win_id)
return (config['width']-1 > 0) return (config['width']-1 > 0)
end end
function specs.slide_resizer(win_id) function M.slide_resizer(win_id)
local config = vim.api.nvim_win_get_config(win_id) local config = vim.api.nvim_win_get_config(win_id)
config['width'] = config['width']-1 config['width'] = config['width']-1
vim.api.nvim_win_set_config(win_id, config) vim.api.nvim_win_set_config(win_id, config)
return (config['width']-1 > 0) return (config['width']-1 > 0)
end end
-- local function exp_resizer() -- function M.exp_resizer()
-- end -- end
--
return specs function M.setup(user_opts)
opts = user_opts or {}
M.create_autocmds(opts)
end
function M.create_autocmds(opts)
vim.cmd("augroup Specs")
vim.cmd("autocmd!")
if opts.show_jumps then
vim.cmd("silent autocmd CursorMoved * :lua require('specs').on_cursor_moved()")
end
vim.cmd("augroup END")
end
return M

View file

@ -1,26 +1,13 @@
fun! Specs() if !has('nvim') || exists('g:loaded_specs')
" remember to remove this line later ;) finish
lua for k in pairs(package.loaded) do if k:match("^specs") then package.loaded[k] = nil end end endif
lua require('specs').show_specs({ delay_ms = 1, inc_ms = 10, blend = 10, width = 20, fader=require('specs').linear_fader, resizer=require('specs').shrink_resizer })
endfun
" e.g: let g:loaded_specs = 1
" require('specs').specs_popup({
" delay = ...,
" reps = ...,
" blend = ...,
" width = ...,
" fader = require('specs').linear_fader(),
" resizer = require('specs').shrink_resizer()
" colorizer = ...,
" })
" fun! Specs()
" lua for k in pairs(package.loaded) do if k:match("^specs") then package.loaded[k] = nil end end
" endfun
" pre-packaged opts could come as 'styles', which also have opts " augroup Specs
" themselves: " autocmd!
" require('specs').specs_popup(require('specs').blink_style({ })) " augroup END
augroup Specs
autocmd!
augroup END