Skip to content

Commit b98ee9e

Browse files
udayvir-singhNTBBloodbath
authored andcommitted
feat: add support for custom formatters
1 parent 5b21f91 commit b98ee9e

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ have to leave Neovim!
4747

4848
- System-wide
4949
- curl
50-
- jq +1.6 (to format JSON output so it can be human-readable)
5150
- Other plugins
5251
- [plenary.nvim](https://github.com/nvim-lua/plenary.nvim)
5352

@@ -75,6 +74,12 @@ use {
7574
show_url = true,
7675
show_http_info = true,
7776
show_headers = true,
77+
-- executables for formatting response body [optional]
78+
formatters = {
79+
-- set them to nil if you want to disable them
80+
json = "jq",
81+
html = {"tidy", "-i", "-q", "--show-errors", "0", "-"}
82+
},
7883
},
7984
-- Jump to request line on run
8085
jump_to_request = false,

doc/rest-nvim.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,5 @@ CONTRIBUTING *rest-nvim-contributing*
223223
3. Commit your changes (`git commit -am 'Add some feature'`)
224224
4. Push to the branch (`git push origin my-new-feature`)
225225
5. Create a new Pull Request
226+
227+
vim:tw=78:ts=8:noet:ft=help:norl:

lua/rest-nvim/config/init.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ local config = {
1212
show_url = true,
1313
show_http_info = true,
1414
show_headers = true,
15+
formatters = {
16+
json = "jq",
17+
html = {"tidy", "-i", "-q", "--show-errors", "0", "-"}
18+
},
1519
},
1620
jump_to_request = false,
1721
env_file = ".env",

lua/rest-nvim/curl/init.lua

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,12 @@ local function create_callback(method, url)
8989
end
9090

9191
--- Add the curl command results into the created buffer
92-
if content_type == "json" then
93-
-- format JSON body
94-
res.body = vim.fn.system("jq", res.body):gsub("\n$", "")
92+
local formatter = config.get("result").formatters[content_type]
93+
-- formate response body
94+
if formatter and vim.fn.executable(type(formatter) == "string" and formatter or formatter[1]) == 1 then
95+
res.body = vim.fn.system(formatter, res.body):gsub("\n$", "")
9596
end
97+
9698
local lines = utils.split(res.body, "\n")
9799
local line_count = vim.api.nvim_buf_line_count(res_bufnr) - 1
98100
vim.api.nvim_buf_set_lines(res_bufnr, line_count, line_count + #lines, false, lines)

0 commit comments

Comments
 (0)