@@ -15,6 +15,23 @@ local function is_executable(x)
15
15
return false
16
16
end
17
17
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
+
18
35
-- get_or_create_buf checks if there is already a buffer with the rest run results
19
36
-- and if the buffer does not exists, then create a new one
20
37
M .get_or_create_buf = function ()
@@ -51,7 +68,7 @@ M.get_or_create_buf = function()
51
68
return new_bufnr
52
69
end
53
70
54
- local function create_callback (method , url , script_str )
71
+ local function create_callback (curl_cmd , method , url , script_str )
55
72
return function (res )
56
73
if res .exit ~= 0 then
57
74
log .error (" [rest.nvim] " .. utils .curl_error (res .exit ))
@@ -83,6 +100,11 @@ local function create_callback(method, url, script_str)
83
100
end
84
101
end
85
102
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
+
86
108
if config .get (" result" ).show_url then
87
109
--- Add metadata into the created buffer (status code, date, etc)
88
110
-- Request statement (METHOD URL)
@@ -200,40 +222,27 @@ local function create_callback(method, url, script_str)
200
222
end
201
223
end
202
224
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
-
219
225
-- curl_cmd runs curl with the passed options, gets or creates a new buffer
220
226
-- and then the results are printed to the recently obtained/created buffer
221
227
-- @param opts (table) curl arguments:
222
228
-- - yank_dry_run (boolean): displays the command
223
229
-- - arguments are forwarded to plenary
224
230
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 )
228
236
237
+ if opts .dry_run then
229
238
if config .get (" yank_dry_run" ) then
230
239
vim .cmd (" let @+=" .. string.format (" %q" , curl_cmd ))
231
240
end
232
241
233
242
vim .api .nvim_echo ({ { " [rest.nvim] Request preview:\n " , " Comment" }, { curl_cmd } }, false , {})
234
243
return
235
244
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 ))
237
246
curl [opts .method ](opts )
238
247
end
239
248
end
0 commit comments