Merge pull request #2 from levouh/main

Cleanup old windows on focus change, add ability to ignore based on buffer/file type
This commit is contained in:
edluffy 2021-04-03 17:01:36 +01:00 committed by GitHub
commit a03c03ed53
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 10 deletions

View file

@ -35,7 +35,11 @@ require('specs').setup{
winhl = "PMenu", winhl = "PMenu",
fader = require('specs').linear_fader, fader = require('specs').linear_fader,
resizer = require('specs').shrink_resizer resizer = require('specs').shrink_resizer
} },
ignore_filetypes = {},
ignore_buftypes = {
nofile = true,
},
} }
``` ```

View file

@ -6,7 +6,7 @@ local old_cur
function M.on_cursor_moved() function M.on_cursor_moved()
local cur = vim.api.nvim_win_get_cursor(0) local cur = vim.api.nvim_win_get_cursor(0)
if old_cur then if old_cur then
jump = math.abs(cur[1]-old_cur[1]) local jump = math.abs(cur[1]-old_cur[1])
if jump >= opts.min_jump then if jump >= opts.min_jump then
M.show_specs() M.show_specs()
end end
@ -14,9 +14,37 @@ function M.on_cursor_moved()
old_cur = cur old_cur = cur
end end
function M.should_show_specs(start_win_id)
if not vim.api.nvim_win_is_valid(start_win_id) then
return false
end
if type(opts.ignore_filetypes) ~= 'table' or type(opts.ignore_buftypes) ~= 'table' then
return true
end
local buftype, filetype, ok
ok, buftype = pcall(vim.api.nvim_buf_get_option, 0, 'buftype')
if ok and opts.ignore_buftypes[buftype] then
return false
end
ok, filetype = pcall(vim.api.nvim_buf_get_option, 0, 'filetype')
if ok and opts.ignore_filetypes[filetype] then
return false
end
return true
end
function M.show_specs() function M.show_specs()
local buftype = vim.api.nvim_buf_get_option(0, 'buftype') local start_win_id = vim.api.nvim_get_current_win()
if buftype == 'nofile' then return end
if not M.should_show_specs(start_win_id) then
return
end
local cursor_col = vim.fn.wincol()-1 local cursor_col = vim.fn.wincol()-1
local cursor_row = vim.fn.winline()-1 local cursor_row = vim.fn.winline()-1
@ -24,7 +52,7 @@ function M.show_specs()
local win_id = vim.api.nvim_open_win(bufh, false, { local win_id = vim.api.nvim_open_win(bufh, false, {
relative='win', relative='win',
width = 1, width = 1,
height = 1, height = 1,
col = cursor_col, col = cursor_col,
row = cursor_row, row = cursor_row,
style = 'minimal' style = 'minimal'
@ -35,8 +63,22 @@ function M.show_specs()
local cnt = 0 local cnt = 0
local config = vim.api.nvim_win_get_config(win_id) local config = vim.api.nvim_win_get_config(win_id)
local timer = vim.loop.new_timer() 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, 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)
pcall(vim.api.nvim_win_close, win_id, true)
-- Callbacks might stack up before the timer actually gets closed, track that state
-- internally here instead
closed = true
end
return
end
if vim.api.nvim_win_is_valid(win_id) then if vim.api.nvim_win_is_valid(win_id) then
local bl = opts.popup.fader(opts.popup.blend, cnt) local bl = opts.popup.fader(opts.popup.blend, cnt)
local dm = opts.popup.resizer(opts.popup.width, cursor_col, cnt) local dm = opts.popup.resizer(opts.popup.width, cursor_col, cnt)
@ -77,7 +119,7 @@ end
--[[ ▁▂▃▄▅▆▇█▇▆▅▄▃▂▁ ]]-- --[[ ▁▂▃▄▅▆▇█▇▆▅▄▃▂▁ ]]--
function M.pulse_fader(blend, cnt) function M.pulse_fader(blend, cnt)
if cnt < (100-blend)/2 then if cnt < (100-blend)/2 then
return cnt return cnt
@ -88,7 +130,7 @@ end
--[[ ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ ]]-- --[[ ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ ]]--
function M.empty_fader(blend, cnt) function M.empty_fader(_, _)
return nil return nil
end end
@ -123,14 +165,18 @@ local DEFAULT_OPTS = {
show_jumps = true, show_jumps = true,
min_jump = 30, min_jump = 30,
popup = { popup = {
delay_ms = 10, delay_ms = 10,
inc_ms = 5, inc_ms = 5,
blend = 10, blend = 10,
width = 20, width = 20,
winhl = "PMenu", winhl = "PMenu",
fader = M.exp_fader, fader = M.exp_fader,
resizer = M.shrink_resizer resizer = M.shrink_resizer,
} },
ignore_filetypes = {},
ignore_buftypes = {
nofile = true,
},
} }
function M.setup(user_opts) function M.setup(user_opts)