Skip to content

Commit

Permalink
Add support for curl --write-out option
Browse files Browse the repository at this point in the history
Related to #50

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/oysandvik94/curl.nvim/issues/50?shareId=XXXX-XXXX-XXXX-XXXX).
  • Loading branch information
oysandvik94 committed Sep 26, 2024
1 parent e3e70d8 commit 3359e63
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 9 deletions.
48 changes: 42 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# curl.nvim

💪 Integrate curl and jq in Neovim. 💪
Integrate curl and jq in Neovim.

</div>

Expand Down Expand Up @@ -129,16 +129,16 @@ Or if you use [Lazy](https://github.com/folke/lazy.nvim), just pass the table in

</details>

## Features
## Features

### 💪 .curl filetype
### .curl filetype

Opening any file with the ".curl" file extension will activate this plugins features.
You will get some syntax highlighting and the ability to execute curl commands from you buffer.
Since any ".curl" file will work, you can manage your own collection instead of using the builtin
system, and even check in files to your repository.

### 💪 Formatting
### Formatting

#### No quotes needed

Expand Down Expand Up @@ -193,7 +193,7 @@ curl -X POST https://jsonplaceholder.typicode.com/posts

</details>

### 💪 Headers
### Headers

Basic auth and bearer tokens work, and can be retrieved from environment variables

Expand All @@ -215,7 +215,7 @@ curl -X GET "https://httpbin.org/bearer" -H "accept: application/json" -H "Autho

</details>

### 💪 Collections
### Collections

There are multiple ways to work with the scratch buffers, so you can tailor it to your own workflow.
By default, running ":CurlOpen" will open a command buffer that is tied to your current working directory.
Expand Down Expand Up @@ -258,6 +258,42 @@ This might be the neovim default picker, or telescope/fzf-lua if configured. See
In the future, I (or someone else) might create a dedicated telescope/fzf picker to get features
like preview enabled.

### 💪 Write-out option

The plugin now supports the `curl --write-out` option with custom formats. You can define a format file (or an inline format) in the config, or the plugin detects the `-w` flag in the curl file, and outputs the formatted result, maybe in the top of the output buffer, just like how headers are shown for the `-i` flag.

<details>
<summary>See example</summary>

```bash
curl -XGET https://jsonplaceholder.typicode.com/todos
-H "Content-Type: application/json"
-w "totalTime: %{time_total}"
```

This will give the results in a single line, with the custom format at the end of it.

You can also use a format file:

```bash
curl -w "@curl-filename.txt" https://example.com
```

Where `curl-filename.txt` contains:

```
time_namelookup: %{time_namelookup}\n
time_connect: %{time_connect}\n
time_appconnect: %{time_appconnect}\n
time_pretransfer: %{time_pretransfer}\n
time_redirect: %{time_redirect}\n
time_starttransfer: %{time_starttransfer}\n
———\n
time_total: %{time_total}\n
```

</details>

## Lua api

This section describes all the methods in the exposed lua api
Expand Down
34 changes: 34 additions & 0 deletions doc/curl.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,40 @@ In the future, I (or someone else) might create a dedicated telescope/fzf
picker to get features like preview enabled.


WRITE-OUT OPTION ~

The plugin now supports the `curl --write-out` option with custom formats. You can define a format file (or an inline format) in the config, or the plugin detects the `-w` flag in the curl file, and outputs the formatted result, maybe in the top of the output buffer, just like how headers are shown for the `-i` flag.

See example ~

>bash
curl -XGET https://jsonplaceholder.typicode.com/todos
-H "Content-Type: application/json"
-w "totalTime: %{time_total}"
<

This will give the results in a single line, with the custom format at the end of it.

You can also use a format file:

>bash
curl -w "@curl-filename.txt" https://example.com
<

Where `curl-filename.txt` contains:

>text
time_namelookup: %{time_namelookup}\n
time_connect: %{time_connect}\n
time_appconnect: %{time_appconnect}\n
time_pretransfer: %{time_pretransfer}\n
time_redirect: %{time_redirect}\n
time_starttransfer: %{time_starttransfer}\n
———\n
time_total: %{time_total}\n
<


LUA API *curl.nvim-lua-api*

This section describes all the methods in the exposed lua api
Expand Down
8 changes: 7 additions & 1 deletion lua/curl/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ M.execute_curl = function()
commands = curl_command
end

local custom_format = nil
local write_out_flag = curl_command:match("%-w%s+([^\n]+)")
if write_out_flag then
custom_format = write_out_flag
end

local _ = vim.fn.jobstart(commands, {
on_exit = function(_, exit_code, _)
if exit_code ~= 0 then
Expand All @@ -126,7 +132,7 @@ M.execute_curl = function()
return
end

local parsed_output = output_parser.parse_curl_output(output)
local parsed_output = output_parser.parse_curl_output(output, custom_format)
buffers.set_output_buffer_content(executed_from_win, parsed_output)
end,
on_stdout = function(_, data, _)
Expand Down
28 changes: 27 additions & 1 deletion lua/curl/output_parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,28 @@ local function extract_json(output_lines)
return header_lines, json_string
end

local function extract_custom_format(output_lines, custom_format)
local formatted_result = {}
local in_custom_format = false

for _, line in ipairs(output_lines) do
if line:match("<custom>") then
in_custom_format = true
elseif line:match("</custom>") then
in_custom_format = false
elseif in_custom_format then
table.insert(formatted_result, line)
end
end

return formatted_result
end

---
---@param curl_standard_out string
---@param custom_format string
---@return table
M.parse_curl_output = function(curl_standard_out)
M.parse_curl_output = function(curl_standard_out, custom_format)
if is_json_start(curl_standard_out) then
return run_jq(curl_standard_out)
end
Expand All @@ -72,6 +90,14 @@ M.parse_curl_output = function(curl_standard_out)

local json_lines = run_jq(json_string)
table.move(json_lines, 1, #json_lines, #header_lines + 1, header_lines)

if custom_format then
local formatted_result = extract_custom_format(output_table, custom_format)
table.insert(header_lines, "")
vim.list_extend(header_lines, formatted_result)
end

return header_lines
end

return M
10 changes: 9 additions & 1 deletion lua/curl/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,15 @@ M.parse_curl_command = function(cursor_pos, lines)
table.insert(selection, flag)
end

return vim.fn.join(selection, " ")
local curl_command = vim.fn.join(selection, " ")

local custom_format = nil
local write_out_flag = curl_command:match("%-w%s+([^\n]+)")
if write_out_flag then
custom_format = write_out_flag
end

return curl_command, custom_format
end

return M

0 comments on commit 3359e63

Please sign in to comment.