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 opts = user_opts or {}
local M = {}
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 col = vim.fn.wincol()
local bufh = vim.api.nvim_create_buf(false, true)
win_id = vim.api.nvim_open_win(bufh, false, {
relative='win',
width = opts.width*2 + 1,
width = opts.popup.width*2 + 1,
height = 1,
col = col,
row = row,
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_resize = true
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 can_fade then can_fade = opts.fader(win_id) end
if can_resize then can_resize = opts.resizer(win_id) end
if can_fade then can_fade = opts.popup.fader(win_id) end
if can_resize then can_resize = opts.popup.resizer(win_id) end
if not (can_blend or can_resize) then
vim.loop.close(timer)
vim.api.nvim_win_close(win_id, true)
print("Timer done")
-- print("Timer done")
end
end
end))
end
-- 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")
vim.api.nvim_win_set_option(win_id, "winblend", blend+1)
return (blend+1 < 100)
end
local function pulse_fader(win_id)
function M.pulse_fader(win_id)
end
-- local function exp_fader()
-- function M.exp_fader()
-- end
function specs.shrink_resizer(win_id)
function M.shrink_resizer(win_id)
local config = vim.api.nvim_win_get_config(win_id)
config['width'] = config['width']-1
config['col'][false] = vim.fn.wincol()-config['width']/2
@ -53,14 +77,30 @@ function specs.shrink_resizer(win_id)
return (config['width']-1 > 0)
end
function specs.slide_resizer(win_id)
function M.slide_resizer(win_id)
local config = vim.api.nvim_win_get_config(win_id)
config['width'] = config['width']-1
vim.api.nvim_win_set_config(win_id, config)
return (config['width']-1 > 0)
end
-- local function exp_resizer()
-- function M.exp_resizer()
-- 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()
" remember to remove this line later ;)
lua for k in pairs(package.loaded) do if k:match("^specs") then package.loaded[k] = nil end end
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
if !has('nvim') || exists('g:loaded_specs')
finish
endif
" e.g:
" require('specs').specs_popup({
" delay = ...,
" reps = ...,
" blend = ...,
" width = ...,
" fader = require('specs').linear_fader(),
" resizer = require('specs').shrink_resizer()
" colorizer = ...,
" })
let g:loaded_specs = 1
" 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
" themselves:
" require('specs').specs_popup(require('specs').blink_style({ }))
augroup Specs
autocmd!
augroup END
" augroup Specs
" autocmd!
" augroup END