specs.nvim/lua/specs/init.lua

227 lines
5.5 KiB
Lua
Raw Normal View History

local M = {}
2021-03-17 01:57:41 +00:00
local opts = {}
local old_cur
2021-05-05 14:46:06 +00:00
local au_toggle
function M.on_cursor_moved()
local cur = vim.api.nvim_win_get_cursor(0)
if old_cur then
local jump = math.abs(cur[1]-old_cur[1])
if jump >= opts.min_jump then
M.show_specs()
end
end
old_cur = cur
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
2022-08-31 17:18:38 +00:00
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
2022-08-31 17:18:38 +00:00
if popup == nil then
2022-08-31 18:16:46 +00:00
popup = {}
2022-08-31 17:18:38 +00:00
end
2022-08-31 18:16:46 +00:00
local _opts = vim.tbl_deep_extend("force", opts, {popup = popup})
2022-08-31 17:18:38 +00:00
2021-03-17 17:29:19 +00:00
local cursor_col = vim.fn.wincol()-1
local cursor_row = vim.fn.winline()-1
2021-03-14 02:13:40 +00:00
local bufh = vim.api.nvim_create_buf(false, true)
local win_id = vim.api.nvim_open_win(bufh, false, {
2022-08-31 17:18:38 +00:00
relative= 'win',
width = 1,
height = 1,
2021-03-17 17:29:19 +00:00
col = cursor_col,
row = cursor_row,
2021-03-14 02:13:40 +00:00
style = 'minimal'
})
2022-08-31 18:16:46 +00:00
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)
2021-03-14 02:13:40 +00:00
local cnt = 0
2021-03-17 17:29:19 +00:00
local config = vim.api.nvim_win_get_config(win_id)
2021-03-14 02:13:40 +00:00
local timer = vim.loop.new_timer()
local closed = false
2021-03-17 17:29:19 +00:00
2022-08-31 18:16:46 +00:00
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
2021-03-14 02:13:40 +00:00
if vim.api.nvim_win_is_valid(win_id) then
2022-08-31 18:16:46 +00:00
local bl = _opts.popup.fader(_opts.popup.blend, cnt)
local dm = _opts.popup.resizer(_opts.popup.width, cursor_col, cnt)
2021-03-17 17:29:19 +00:00
if bl ~= nil then
vim.api.nvim_win_set_option(win_id, "winblend", bl)
end
if dm ~= nil then
config["col"][false] = dm[2]
vim.api.nvim_win_set_config(win_id, config)
vim.api.nvim_win_set_width(win_id, dm[1])
end
if bl == nil and dm == nil then -- Done blending and resizing
2021-03-14 02:13:40 +00:00
vim.loop.close(timer)
vim.api.nvim_win_close(win_id, true)
end
cnt = cnt+1
2021-03-14 02:13:40 +00:00
end
end))
2022-08-31 18:16:46 +00:00
2021-03-14 02:13:40 +00:00
end
--[[ ▁▁▂▂▃▃▄▄▅▅▆▆▇▇██ ]]--
2021-03-17 17:46:39 +00:00
function M.linear_fader(blend, cnt)
if blend + cnt <= 100 then
2021-03-17 17:29:19 +00:00
return cnt
else return nil end
2021-03-14 02:13:40 +00:00
end
2022-08-31 18:16:46 +00:00
--[[ ⌣/⌢\⌣/⌢\⌣/⌢\⌣/⌢\ ]]--
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
2021-03-15 16:35:25 +00:00
--[[ ▁▁▁▁▂▂▂▃▃▃▄▄▅▆▇ ]]--
2021-03-17 17:46:39 +00:00
function M.exp_fader(blend, cnt)
if blend + math.floor(math.exp(cnt/10)) <= 100 then
return blend + math.floor(math.exp(cnt/10))
2021-03-17 17:29:19 +00:00
else return nil end
2021-03-15 16:35:25 +00:00
end
--[[ ▁▂▃▄▅▆▇█▇▆▅▄▃▂▁ ]]--
2021-03-17 17:46:39 +00:00
function M.pulse_fader(blend, cnt)
if cnt < (100-blend)/2 then
2021-03-17 17:29:19 +00:00
return cnt
2021-03-17 17:46:39 +00:00
elseif cnt < 100-blend then
2021-03-17 17:29:19 +00:00
return 100-cnt
else return nil end
end
--[[ ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ ]]--
function M.empty_fader(_, _)
2021-03-17 17:29:19 +00:00
return nil
2021-03-14 02:13:40 +00:00
end
--[[ ░░▒▒▓█████▓▒▒░░ ]]--
2021-03-14 02:13:40 +00:00
2021-03-17 17:46:39 +00:00
function M.shrink_resizer(width, ccol, cnt)
if width-cnt > 0 then
return {width-cnt, ccol-(width-cnt)/2 + 1}
2021-03-17 17:29:19 +00:00
else return nil end
2021-03-14 02:13:40 +00:00
end
--[[ ████▓▓▓▒▒▒▒░░░░ ]]--
2021-03-17 17:46:39 +00:00
function M.slide_resizer(width, ccol, cnt)
if width-cnt > 0 then
return {width-cnt, ccol}
2021-03-17 17:29:19 +00:00
else return nil end
2021-03-14 02:13:40 +00:00
end
--[[ ███████████████ ]]--
2021-03-17 17:46:39 +00:00
function M.empty_resizer(width, ccol, cnt)
2021-03-17 17:29:19 +00:00
if cnt < 100 then
2021-03-17 17:46:39 +00:00
return {width, ccol - width/2}
2021-03-17 17:29:19 +00:00
else return nil end
end
2021-03-17 01:57:41 +00:00
local DEFAULT_OPTS = {
show_jumps = true,
min_jump = 30,
popup = {
delay_ms = 10,
2021-03-17 01:57:41 +00:00
inc_ms = 5,
blend = 10,
width = 20,
2021-03-23 23:31:18 +00:00
winhl = "PMenu",
2021-03-17 01:57:41 +00:00
fader = M.exp_fader,
resizer = M.shrink_resizer,
},
ignore_filetypes = {},
ignore_buftypes = {
nofile = true,
},
2021-03-17 01:57:41 +00:00
}
function M.setup(user_opts)
2021-03-17 01:57:41 +00:00
opts = vim.tbl_deep_extend("force", DEFAULT_OPTS, user_opts)
M.create_autocmds()
end
2021-05-05 14:46:06 +00:00
function M.toggle()
if au_toggle then
M.clear_autocmds()
else
M.create_autocmds()
end
end
2021-03-17 01:57:41 +00:00
function M.create_autocmds()
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")
2021-05-05 14:46:06 +00:00
au_toggle = true
end
2021-05-05 14:46:06 +00:00
function M.clear_autocmds()
vim.cmd("augroup Specs") vim.cmd("autocmd!")
vim.cmd("augroup END")
au_toggle = false
end
2021-03-14 02:13:40 +00:00
return M