Skip to content

Commit aea7c64

Browse files
Matthieu Coudronteto
Matthieu Coudron
authored andcommitted
feat: add config.show_curl_command
This is gated behind a flag at false by default because it can be quite verbose. Sometimes we may want to rerun the command run by rest.nvim with additional parameters, for instance to save the output in a specific file.
1 parent 5714b71 commit aea7c64

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ use {
7777
result = {
7878
-- toggle showing URL, HTTP info, headers at top the of result window
7979
show_url = true,
80+
-- show the generated curl command in case you want to launch
81+
-- the same request via the terminal (can be verbose)
82+
show_curl_command = false,
8083
show_http_info = true,
8184
show_headers = true,
8285
-- executables or functions for formatting response body [optional]

lua/rest-nvim/config/init.lua

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ local config = {
1010
timeout = 150,
1111
},
1212
result = {
13+
show_curl_command = true,
1314
show_url = true,
1415
show_http_info = true,
1516
show_headers = true,

lua/rest-nvim/curl/init.lua

+30-21
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,23 @@ local function is_executable(x)
1515
return false
1616
end
1717

18+
local function format_curl_cmd(res)
19+
local cmd = "curl"
20+
21+
for _, value in pairs(res) do
22+
if string.sub(value, 1, 1) == "-" then
23+
cmd = cmd .. " " .. value
24+
else
25+
cmd = cmd .. " '" .. value .. "'"
26+
end
27+
end
28+
29+
-- remote -D option
30+
cmd = string.gsub(cmd, "-D '%S+' ", "")
31+
return cmd
32+
end
33+
34+
1835
-- get_or_create_buf checks if there is already a buffer with the rest run results
1936
-- and if the buffer does not exists, then create a new one
2037
M.get_or_create_buf = function()
@@ -51,7 +68,7 @@ M.get_or_create_buf = function()
5168
return new_bufnr
5269
end
5370

54-
local function create_callback(method, url, script_str)
71+
local function create_callback(curl_cmd, method, url, script_str)
5572
return function(res)
5673
if res.exit ~= 0 then
5774
log.error("[rest.nvim] " .. utils.curl_error(res.exit))
@@ -83,6 +100,11 @@ local function create_callback(method, url, script_str)
83100
end
84101
end
85102

103+
-- This can be quite verbose so let user control it
104+
if config.get("result").show_curl_command then
105+
vim.api.nvim_buf_set_lines(res_bufnr, 0, 0, false, { "Command :" .. curl_cmd })
106+
end
107+
86108
if config.get("result").show_url then
87109
--- Add metadata into the created buffer (status code, date, etc)
88110
-- Request statement (METHOD URL)
@@ -200,40 +222,27 @@ local function create_callback(method, url, script_str)
200222
end
201223
end
202224

203-
local function format_curl_cmd(res)
204-
local cmd = "curl"
205-
206-
for _, value in pairs(res) do
207-
if string.sub(value, 1, 1) == "-" then
208-
cmd = cmd .. " " .. value
209-
else
210-
cmd = cmd .. " '" .. value .. "'"
211-
end
212-
end
213-
214-
-- remote -D option
215-
cmd = string.gsub(cmd, "-D '%S+' ", "")
216-
return cmd
217-
end
218-
219225
-- curl_cmd runs curl with the passed options, gets or creates a new buffer
220226
-- and then the results are printed to the recently obtained/created buffer
221227
-- @param opts (table) curl arguments:
222228
-- - yank_dry_run (boolean): displays the command
223229
-- - arguments are forwarded to plenary
224230
M.curl_cmd = function(opts)
225-
if opts.dry_run then
226-
local res = curl[opts.method](opts)
227-
local curl_cmd = format_curl_cmd(res)
231+
-- plenary's curl module is strange in the sense that with "dry_run" it returns the command
232+
-- otherwise it starts the request :/
233+
local dry_run_opts = vim.tbl_extend("force", opts, { dry_run = true } )
234+
local res = curl[opts.method](dry_run_opts)
235+
local curl_cmd = format_curl_cmd(res)
228236

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

233242
vim.api.nvim_echo({ { "[rest.nvim] Request preview:\n", "Comment" }, { curl_cmd } }, false, {})
234243
return
235244
else
236-
opts.callback = vim.schedule_wrap(create_callback(opts.method, opts.url, opts.script_str))
245+
opts.callback = vim.schedule_wrap(create_callback(curl_cmd, opts.method, opts.url, opts.script_str))
237246
curl[opts.method](opts)
238247
end
239248
end

0 commit comments

Comments
 (0)