mirror of
https://github.com/anotherhadi/jwt-tui.nvim.git
synced 2026-10-05 11:18:25 +02:00
62 lines
1.5 KiB
Lua
62 lines
1.5 KiB
Lua
local M = {}
|
|
|
|
M.defaults = {
|
|
width = 0.9,
|
|
height = 0.9,
|
|
border = "rounded",
|
|
}
|
|
|
|
M.config = vim.deepcopy(M.defaults)
|
|
|
|
local function get_visual_selection()
|
|
local s = vim.fn.getpos("'<")
|
|
local e = vim.fn.getpos("'>")
|
|
local lines = vim.fn.getregion(s, e, { type = vim.fn.visualmode() })
|
|
return vim.trim(table.concat(lines, "\n"))
|
|
end
|
|
|
|
function M.open(opts)
|
|
if vim.fn.executable("jwt-tui") == 0 then
|
|
vim.notify("[jwt-tui] jwt-tui not found in PATH", vim.log.levels.ERROR)
|
|
return
|
|
end
|
|
|
|
local args = { "jwt-tui" }
|
|
if opts.token and opts.token ~= "" then
|
|
vim.list_extend(args, { "-t", opts.token })
|
|
end
|
|
if opts.secret and opts.secret ~= "" then
|
|
vim.list_extend(args, { "-s", opts.secret })
|
|
end
|
|
|
|
local cfg = M.config
|
|
local width = math.floor(vim.o.columns * cfg.width)
|
|
local height = math.floor(vim.o.lines * cfg.height)
|
|
|
|
local buf = vim.api.nvim_create_buf(false, true)
|
|
vim.api.nvim_open_win(buf, true, {
|
|
relative = "editor",
|
|
width = width,
|
|
height = height,
|
|
col = math.floor((vim.o.columns - width) / 2),
|
|
row = math.floor((vim.o.lines - height) / 2),
|
|
style = "minimal",
|
|
border = cfg.border,
|
|
})
|
|
|
|
vim.fn.termopen(args, {
|
|
on_exit = function()
|
|
if vim.api.nvim_buf_is_valid(buf) then
|
|
vim.api.nvim_buf_delete(buf, { force = true })
|
|
end
|
|
end,
|
|
})
|
|
vim.cmd("startinsert")
|
|
end
|
|
|
|
function M.open_selection()
|
|
M.open({ token = get_visual_selection() })
|
|
end
|
|
|
|
return M
|