Skip to content

feat: add config.show_curl_command #202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ use {
result = {
-- toggle showing URL, HTTP info, headers at top the of result window
show_url = true,
-- show the generated curl command in case you want to launch
-- the same request via the terminal (can be verbose)
show_curl_command = false,
show_http_info = true,
show_headers = true,
-- executables or functions for formatting response body [optional]
Expand Down
1 change: 1 addition & 0 deletions lua/rest-nvim/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ local config = {
timeout = 150,
},
result = {
show_curl_command = true,
show_url = true,
show_http_info = true,
show_headers = true,
Expand Down
51 changes: 30 additions & 21 deletions lua/rest-nvim/curl/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ local function is_executable(x)
return false
end

local function format_curl_cmd(res)
local cmd = "curl"

for _, value in pairs(res) do
if string.sub(value, 1, 1) == "-" then
cmd = cmd .. " " .. value
else
cmd = cmd .. " '" .. value .. "'"
end
end

-- remote -D option
cmd = string.gsub(cmd, "-D '%S+' ", "")
return cmd
end


-- get_or_create_buf checks if there is already a buffer with the rest run results
-- and if the buffer does not exists, then create a new one
M.get_or_create_buf = function()
Expand Down Expand Up @@ -51,7 +68,7 @@ M.get_or_create_buf = function()
return new_bufnr
end

local function create_callback(method, url, script_str)
local function create_callback(curl_cmd, method, url, script_str)
return function(res)
if res.exit ~= 0 then
log.error("[rest.nvim] " .. utils.curl_error(res.exit))
Expand Down Expand Up @@ -83,6 +100,11 @@ local function create_callback(method, url, script_str)
end
end

-- This can be quite verbose so let user control it
if config.get("result").show_curl_command then
vim.api.nvim_buf_set_lines(res_bufnr, 0, 0, false, { "Command :" .. curl_cmd })
end

if config.get("result").show_url then
--- Add metadata into the created buffer (status code, date, etc)
-- Request statement (METHOD URL)
Expand Down Expand Up @@ -200,40 +222,27 @@ local function create_callback(method, url, script_str)
end
end

local function format_curl_cmd(res)
local cmd = "curl"

for _, value in pairs(res) do
if string.sub(value, 1, 1) == "-" then
cmd = cmd .. " " .. value
else
cmd = cmd .. " '" .. value .. "'"
end
end

-- remote -D option
cmd = string.gsub(cmd, "-D '%S+' ", "")
return cmd
end

-- curl_cmd runs curl with the passed options, gets or creates a new buffer
-- and then the results are printed to the recently obtained/created buffer
-- @param opts (table) curl arguments:
-- - yank_dry_run (boolean): displays the command
-- - arguments are forwarded to plenary
M.curl_cmd = function(opts)
if opts.dry_run then
local res = curl[opts.method](opts)
local curl_cmd = format_curl_cmd(res)
-- plenary's curl module is strange in the sense that with "dry_run" it returns the command
-- otherwise it starts the request :/
local dry_run_opts = vim.tbl_extend("force", opts, { dry_run = true } )
local res = curl[opts.method](dry_run_opts)
local curl_cmd = format_curl_cmd(res)

if opts.dry_run then
if config.get("yank_dry_run") then
vim.cmd("let @+=" .. string.format("%q", curl_cmd))
end

vim.api.nvim_echo({ { "[rest.nvim] Request preview:\n", "Comment" }, { curl_cmd } }, false, {})
return
else
opts.callback = vim.schedule_wrap(create_callback(opts.method, opts.url, opts.script_str))
opts.callback = vim.schedule_wrap(create_callback(curl_cmd, opts.method, opts.url, opts.script_str))
curl[opts.method](opts)
end
end
Expand Down