commit c5100cb2cdfaf333014076a1a5c4c6a778bf3983 Author: Hadi <112569860+anotherhadi@users.noreply.github.com> Date: Tue May 26 20:32:48 2026 +0200 init Signed-off-by: Hadi <112569860+anotherhadi@users.noreply.github.com> diff --git a/README.md b/README.md new file mode 100644 index 0000000..7fe5f8b --- /dev/null +++ b/README.md @@ -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', 'jj', 'JwtTui', { desc = 'jwt-tui: open' }) + +-- open with visual selection as token +vim.keymap.set('v', 'jj', ':JwtTuiOpen', { 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 +``` diff --git a/lua/jwt-tui/init.lua b/lua/jwt-tui/init.lua new file mode 100644 index 0000000..60c08f6 --- /dev/null +++ b/lua/jwt-tui/init.lua @@ -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 diff --git a/lua/jwt-tui/term.lua b/lua/jwt-tui/term.lua new file mode 100644 index 0000000..2ed3148 --- /dev/null +++ b/lua/jwt-tui/term.lua @@ -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