Skip to content

Commit

Permalink
feat: add support for custom formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
udayvir-singh authored and NTBBloodbath committed Aug 9, 2022
1 parent 5b21f91 commit b98ee9e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ have to leave Neovim!

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

Expand Down Expand Up @@ -75,6 +74,12 @@ use {
show_url = true,
show_http_info = true,
show_headers = true,
-- executables for formatting response body [optional]
formatters = {
-- set them to nil if you want to disable them
json = "jq",
html = {"tidy", "-i", "-q", "--show-errors", "0", "-"}
},
},
-- Jump to request line on run
jump_to_request = false,
Expand Down
2 changes: 2 additions & 0 deletions doc/rest-nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,5 @@ CONTRIBUTING *rest-nvim-contributing*
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request

vim:tw=78:ts=8:noet:ft=help:norl:
4 changes: 4 additions & 0 deletions lua/rest-nvim/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ local config = {
show_url = true,
show_http_info = true,
show_headers = true,
formatters = {
json = "jq",
html = {"tidy", "-i", "-q", "--show-errors", "0", "-"}
},
},
jump_to_request = false,
env_file = ".env",
Expand Down
8 changes: 5 additions & 3 deletions lua/rest-nvim/curl/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,12 @@ local function create_callback(method, url)
end

--- Add the curl command results into the created buffer
if content_type == "json" then
-- format JSON body
res.body = vim.fn.system("jq", res.body):gsub("\n$", "")
local formatter = config.get("result").formatters[content_type]
-- formate response body
if formatter and vim.fn.executable(type(formatter) == "string" and formatter or formatter[1]) == 1 then
res.body = vim.fn.system(formatter, res.body):gsub("\n$", "")
end

local lines = utils.split(res.body, "\n")
local line_count = vim.api.nvim_buf_line_count(res_bufnr) - 1
vim.api.nvim_buf_set_lines(res_bufnr, line_count, line_count + #lines, false, lines)
Expand Down

0 comments on commit b98ee9e

Please sign in to comment.