Skip to content

Commit

Permalink
feat: yank executable curl cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
purefun authored and NTBBloodbath committed Dec 16, 2021
1 parent 4b8608e commit b635ff2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ use {
jump_to_request = false,
env_file = '.env',
custom_dynamic_variables = {},
yank_dry_run = true,
})
end
}
Expand Down
3 changes: 2 additions & 1 deletion doc/rest-nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ function, it looks like this by default:
` },`
` -- Jump to request line on run`
` jump_to_request = false,`
` env_file = '.env'`
` env_file = '.env',`
` yank_dry_run = true,`
`})`

In this section we will be using `https://reqres.in/` for requests.
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 @@ -15,6 +15,7 @@ local config = {
jump_to_request = false,
env_file = ".env",
custom_dynamic_variables = {},
yank_dry_run = true,
}

--- Get a configuration value
Expand Down
25 changes: 24 additions & 1 deletion lua/rest-nvim/curl/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,36 @@ local function create_callback(method, url)
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 curl arguments
M.curl_cmd = function(opts)
if opts.dry_run then
local res = curl[opts.method](opts)
log.debug("[rest.nvim] Request preview:\n" .. "curl " .. table.concat(res, " "))
local curl_cmd = format_curl_cmd(res)

if config.get('yank_dry_run') then
vim.cmd('let @+="' .. curl_cmd .. '"')
end

log.debug("[rest.nvim] Request preview:\n" .. curl_cmd)
return
else
opts.callback = vim.schedule_wrap(create_callback(opts.method, opts.url))
Expand Down

0 comments on commit b635ff2

Please sign in to comment.