From 6a940447273ac3cce4ef669d8afdc1ce3a9a6c47 Mon Sep 17 00:00:00 2001 From: "Radu Macocian (admac)" Date: Thu, 19 Mar 2026 17:01:27 +0100 Subject: [PATCH] Added input parsing, and curl response parsing --- lua/nvim_http/config.lua | 4 ---- lua/nvim_http/init.lua | 44 ++++++++++++++++++++++++++++++++++++++-- nvim-http-scm-1.rockspec | 18 ---------------- plugin/nvim-http.lua | 12 +++++------ tests/urls.md | 1 + 5 files changed, 49 insertions(+), 30 deletions(-) delete mode 100644 nvim-http-scm-1.rockspec create mode 100644 tests/urls.md diff --git a/lua/nvim_http/config.lua b/lua/nvim_http/config.lua index 74be6dc..70e6785 100644 --- a/lua/nvim_http/config.lua +++ b/lua/nvim_http/config.lua @@ -17,8 +17,4 @@ M.defaults = { }, } -function M.merge(opts) - return vim.tbl_deep_extend("force", {}, M.defaults, opts or {}) -end - return M diff --git a/lua/nvim_http/init.lua b/lua/nvim_http/init.lua index 864af09..3b77182 100644 --- a/lua/nvim_http/init.lua +++ b/lua/nvim_http/init.lua @@ -1,12 +1,52 @@ local M = {} +local output_fields = {"http_code", "time_total"} + function M.get_current_line() return vim.api.nvim_get_current_line() end function M.parse_line(line) - local a, b = line:match"^(%S+)%s+(.+)" - return a, b + local method, url = line:match"^%s*(%S+)%s+(.+)" + return method, url +end + +function M.execute(method, url, fields) + local outFormat = "\\n--metadata--\\n" + for _, field in pairs(fields) do + outFormat = outFormat .. field .. ":%{" .. field .. "}\\n" + end + local result = vim.system({"curl", url, "-X", method, "-w", outFormat}, { + text = true, + }):wait() + return result +end + +function M.parse_output(response, fields) + local body, metadata = response:match"(.-)%s*\n%-%-metadata%-%-\n(.*)" + local result = { + body = body or "" + } + + for _, field in pairs(fields) do + local value = metadata:match(field .. ":([^\n]*)") + if value then + result[field] = value + end + end + + return result +end + +function M.run_http_under_cursor() + local line = M.get_current_line() + vim.notify("Current line: " .. line, vim.log.levels.TRACE) + local method, url = M.parse_line(line) + vim.notify("Parsed - Method/Command: " .. tostring(method) .. ", URL/Args: " .. tostring(url), vim.log.levels.TRACE) + local response = M.execute(method, url, output_fields) + vim.notify("Response: " .. response.stdout, vim.log.levels.TRACE) + local result = M.parse_output(response.stdout, output_fields) + return result end return M diff --git a/nvim-http-scm-1.rockspec b/nvim-http-scm-1.rockspec deleted file mode 100644 index 783a9f7..0000000 --- a/nvim-http-scm-1.rockspec +++ /dev/null @@ -1,18 +0,0 @@ -package = "nvim-http" -version = "scm-1" -rockspec_format = "3.0" - -source = { - url = "git+https://github.com/macocianradu/nvim-http", -} - -description = { - summary = "Neovim HTTP runner plugin", - license = "MIT", -} - -dependencies = { - "lua >= 5.1", - "lyaml", -} - diff --git a/plugin/nvim-http.lua b/plugin/nvim-http.lua index d78d3e8..f3be0e0 100644 --- a/plugin/nvim-http.lua +++ b/plugin/nvim-http.lua @@ -1,7 +1,9 @@ local ok, nvim_http = pcall(require, "nvim_http") if not ok then + vim.notify("Failed to load nvim_http: " .. tostring(nvim_http), vim.log.levels.ERROR) return end +vim.notify("nvim_http loaded successfully", vim.log.levels.INFO) vim.api.nvim_create_user_command("NvimHttpRun", function() nvim_http.run_http_under_cursor() @@ -9,9 +11,7 @@ end, { desc = "Run HTTP command under cursor and open Telescope response tree", }) -vim.api.nvim_create_user_command("NvimHttpClear", function() - nvim_http.clear_http_result() -end, { - desc = "Close and clear HTTP response tree", -}) - +-- Set up keybinding +vim.keymap.set('n', 'hr', function() + nvim_http.run_http_under_cursor() +end, { desc = "Run HTTP under cursor" }) diff --git a/tests/urls.md b/tests/urls.md new file mode 100644 index 0000000..400b29a --- /dev/null +++ b/tests/urls.md @@ -0,0 +1 @@ +GET https://jsonplaceholder.typicode.com/todos/1