Skip to content

Commit

Permalink
feat: show content on popup
Browse files Browse the repository at this point in the history
  • Loading branch information
jellydn committed Oct 21, 2023
1 parent 488d4e6 commit 0ae2711
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 9 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@
{
"jellydn/hurl.nvim",
ft = "hurl",
dependencies = { "MunifTanjim/nui.nvim" },
cmd = { "HurlRun" },
opts = {
debug = true, -- default is false
-- Show debugging info
debug = true,
-- Show response in popup or in quick list
-- popup | quickfix
mode = "popup", --
},
keys = {
-- Run API request
Expand Down
5 changes: 5 additions & 0 deletions hurl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ sweepai
pygithub
sandboxed
jellydn
quickfix'
systemlist
bufnr
Tanjim
quickfix
10 changes: 8 additions & 2 deletions lua/hurl/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ _HURL_CFG = {
-- Debug mode
-- Default: false
debug = false,

-- Display in a floating window or in a quick fix list
-- Default is popup
mode = 'popup',
}
local M = {}

--- Setup hurl.nvim
---@param options (table | nil)
-- - debug: (boolean | nil) default: false.
-- - mode: ('popup' | 'quickfix') default: popup.
function M.setup(options)
_HURL_CFG = vim.tbl_extend('force', _HURL_CFG, options or {})

require('hurl.utils').log('hurl.nvim loaded')

require('hurl.wrapper').setup()
end

Expand Down
52 changes: 52 additions & 0 deletions lua/hurl/popup.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
local Popup = require('nui.popup')
local event = require('nui.utils.autocmd').event

local utils = require('hurl.utils')

local M = {}

local popup = Popup({
enter = true,
focusable = true,
border = {
style = 'rounded',
},
position = '50%',
size = {
width = '80%',
height = '60%',
},
})

--- Format the body of the request
---@param body string
---@param type 'json' | 'html'
---@return string[] | nil
local function format(body, type)
local formatters = { json = 'jq', html = { 'prettier', '--parser', 'html' } }
local stdout = vim.fn.systemlist(formatters[type], body)
if vim.v.shell_error ~= 0 then
utils.log('formatter failed' .. tostring(vim.v.shell_error))
return nil
end
return stdout
end

-- Show content in a popup
---@param body string
---@param type 'json' | 'html'
M.show = function(body, type)
popup:mount()
popup:on(event.BufLeave, function()
popup:unmount()
end)

local content = format(body, type)
if not content then
return
end

vim.api.nvim_buf_set_lines(popup.bufnr, 0, -1, false, content)
end

return M
23 changes: 17 additions & 6 deletions lua/hurl/wrapper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,25 @@ local function request(opts, callback)
-- show messages
local lines = response.raw or response.body
if #lines == 0 then
vim.notify('hurl: no response')
return
end
-- Send to quick fix and open it
vim.fn.setqflist({}, ' ', {
title = 'hurl finished',
lines = lines,
})
vim.cmd('copen')

if _HURL_CFG.mode == 'popup' then
local popup = require('hurl.popup')
--show body if it is json
if response.headers['content-type'] == 'application/json' then
popup.show(response.body, 'json')
else
popup.show(response.body, 'html')
end
elseif _HURL_CFG.mode == 'quickfix' then
vim.fn.setqflist({}, ' ', {
title = 'hurl finished',
lines = lines,
})
vim.cmd('copen')
end
end
end,
})
Expand Down
6 changes: 6 additions & 0 deletions test/example.hurl
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
GET https://api.mangadex.org/statistics/manga/8b34f37a-0181-4f0b-8ce3-01217e9a602c

GET https://example.org

HTTP 200
[Captures]
csrf_token: xpath "string(//meta[@name='_csrf_token']/@content)"

0 comments on commit 0ae2711

Please sign in to comment.