feat: center help box, auto-dismiss after 2s, bold help hint
This commit is contained in:
1 file changed
+29
-9
@@ -4,6 +4,7 @@ local M = {}
|
|||||||
|
|
||||||
local win_id = nil
|
local win_id = nil
|
||||||
local buf_id = nil
|
local buf_id = nil
|
||||||
|
local timer = nil
|
||||||
|
|
||||||
local modal_info = {
|
local modal_info = {
|
||||||
{ "continue", "Continue" },
|
{ "continue", "Continue" },
|
||||||
@@ -16,7 +17,6 @@ local modal_info = {
|
|||||||
{ "eval", "Eval in REPL" },
|
{ "eval", "Eval in REPL" },
|
||||||
{ "terminate", "Quit debugging" },
|
{ "terminate", "Quit debugging" },
|
||||||
{ "restart", "Restart session" },
|
{ "restart", "Restart session" },
|
||||||
{ "help", "Show this help" },
|
|
||||||
}
|
}
|
||||||
|
|
||||||
local global_info = {
|
local global_info = {
|
||||||
@@ -49,11 +49,21 @@ local function build_content()
|
|||||||
if key then add(key, pair[2]) end
|
if key then add(key, pair[2]) end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- highlight line at the bottom
|
||||||
|
local help_key = config.opts.keys.help or "?"
|
||||||
lines[#lines + 1] = ""
|
lines[#lines + 1] = ""
|
||||||
|
lines[#lines + 1] = " Press " .. help_key .. " for help"
|
||||||
|
lines[#lines + 1] = ""
|
||||||
|
|
||||||
return lines, width + 2
|
return lines, width + 2
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.close()
|
function M.close()
|
||||||
|
if timer then
|
||||||
|
timer:stop()
|
||||||
|
timer:close()
|
||||||
|
timer = nil
|
||||||
|
end
|
||||||
if win_id and vim.api.nvim_win_is_valid(win_id) then
|
if win_id and vim.api.nvim_win_is_valid(win_id) then
|
||||||
vim.api.nvim_win_close(win_id, true)
|
vim.api.nvim_win_close(win_id, true)
|
||||||
end
|
end
|
||||||
@@ -68,29 +78,39 @@ function M.open()
|
|||||||
M.close()
|
M.close()
|
||||||
|
|
||||||
local lines, width = build_content()
|
local lines, width = build_content()
|
||||||
|
local height = #lines
|
||||||
|
|
||||||
buf_id = vim.api.nvim_create_buf(false, true)
|
buf_id = vim.api.nvim_create_buf(false, true)
|
||||||
vim.api.nvim_buf_set_lines(buf_id, 0, -1, false, lines)
|
vim.api.nvim_buf_set_lines(buf_id, 0, -1, false, lines)
|
||||||
|
|
||||||
|
-- highlight the "Press ? for help" line
|
||||||
|
local hl_line = height - 2
|
||||||
|
vim.api.nvim_buf_add_highlight(buf_id, -1, "Bold", hl_line, 0, -1)
|
||||||
|
|
||||||
vim.bo[buf_id].modifiable = false
|
vim.bo[buf_id].modifiable = false
|
||||||
vim.bo[buf_id].bufhidden = "wipe"
|
vim.bo[buf_id].bufhidden = "wipe"
|
||||||
|
|
||||||
|
-- center the float
|
||||||
|
local ed_w = vim.o.columns
|
||||||
|
local ed_h = vim.o.lines
|
||||||
|
local row = math.floor((ed_h - height) / 2)
|
||||||
|
local col = math.floor((ed_w - width) / 2)
|
||||||
|
|
||||||
win_id = vim.api.nvim_open_win(buf_id, false, {
|
win_id = vim.api.nvim_open_win(buf_id, false, {
|
||||||
relative = "editor",
|
relative = "editor",
|
||||||
anchor = "SE",
|
row = row,
|
||||||
row = vim.o.lines - 2,
|
col = col,
|
||||||
col = vim.o.columns,
|
|
||||||
width = width,
|
width = width,
|
||||||
height = #lines,
|
height = height,
|
||||||
style = "minimal",
|
style = "minimal",
|
||||||
border = "rounded",
|
border = "rounded",
|
||||||
focusable = false,
|
focusable = false,
|
||||||
noautocmd = true,
|
noautocmd = true,
|
||||||
})
|
})
|
||||||
|
|
||||||
vim.api.nvim_create_autocmd({ "CursorMoved", "InsertEnter", "CmdlineEnter" }, {
|
-- auto-close after 2 seconds
|
||||||
once = true,
|
timer = vim.uv.new_timer()
|
||||||
callback = function() M.close() end,
|
timer:start(2000, 0, vim.schedule_wrap(function() M.close() end))
|
||||||
})
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.is_open()
|
function M.is_open()
|
||||||
|
|||||||
Reference in new issue
Block a user