mirror of
https://github.com/macocianradu/nvim-http.git
synced 2026-07-16 10:58:46 +00:00
Added input parsing, and curl response parsing
This commit is contained in:
+42
-2
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user