@@ -15,6 +15,23 @@ local function is_executable(x)
1515 return false
1616end
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
2037M .get_or_create_buf = function ()
@@ -51,7 +68,7 @@ M.get_or_create_buf = function()
5168 return new_bufnr
5269end
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
201223end
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
224230M .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
239248end
0 commit comments