Signed-off-by: Hadi <[email protected]>
This commit is contained in:
Hadi
2026-05-26 20:32:48 +02:00
commit c5100cb2cd
3 changed files with 150 additions and 0 deletions
+65
View File
@@ -0,0 +1,65 @@
# jwt-tui.nvim
A Neovim plugin for [jwt-tui](https://github.com/anotherhadi/jwt-tui): open a floating JWT inspector from Neovim, optionally pre-filled with a token from your visual selection.
## Requirements
- Neovim 0.10+
- [jwt-tui](https://github.com/anotherhadi/jwt-tui) installed and available in `$PATH`
## Installation
**lazy.nvim**
```lua
{
'anotherhadi/jwt-tui.nvim',
config = function()
require('jwt-tui').setup()
end,
}
```
## Setup
```lua
require('jwt-tui').setup()
```
All options and their defaults:
```lua
require('jwt-tui').setup({
width = 0.9, -- fraction of editor width
height = 0.9, -- fraction of editor height
border = "rounded", -- any value accepted by nvim_open_win
})
```
No keymaps are set by default. Add your own:
```lua
-- open empty
vim.keymap.set('n', '<leader>jj', '<cmd>JwtTui<cr>', { desc = 'jwt-tui: open' })
-- open with visual selection as token
vim.keymap.set('v', '<leader>jj', ':<C-u>JwtTuiOpen<CR>', { silent = true, desc = 'jwt-tui: open with selection' })
```
## Commands
| Command | Description |
| ------------- | --------------------------------------------------- |
| `:JwtTui` | Open the JWT inspector (empty) |
| `:JwtTuiOpen` | Open with the current visual selection as the token |
## Lua API
You can call the functions directly in your keymaps or other plugins:
```lua
require('jwt-tui').open() -- open empty
require('jwt-tui').open({ token = '...' }) -- pre-fill token
require('jwt-tui').open({ token = '...', secret = 'mysecret' }) -- pre-fill token and secret
require('jwt-tui').open_selection() -- open with visual selection as token
```
+24
View File
@@ -0,0 +1,24 @@
local M = {}
function M.setup(opts)
local term = require("jwt-tui.term")
term.config = vim.tbl_deep_extend("force", term.defaults, opts or {})
vim.api.nvim_create_user_command("JwtTui", function()
term.open({})
end, { desc = "jwt-tui: open JWT inspector" })
vim.api.nvim_create_user_command("JwtTuiOpen", function()
term.open_selection()
end, { range = true, desc = "jwt-tui: open with visual selection as token" })
end
function M.open(opts)
require("jwt-tui.term").open(opts or {})
end
function M.open_selection()
require("jwt-tui.term").open_selection()
end
return M
+61
View File
@@ -0,0 +1,61 @@
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