Skip to content

Commit

Permalink
feat: show response header on popup
Browse files Browse the repository at this point in the history
  • Loading branch information
jellydn committed Oct 21, 2023
1 parent c325d11 commit ba04d85
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 21 deletions.
4 changes: 4 additions & 0 deletions hurl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ bufnr
Tanjim
quickfix
Munif
autocmd
keymap
noremap
winid
101 changes: 83 additions & 18 deletions lua/hurl/popup.lua
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
local Popup = require('nui.popup')
local event = require('nui.utils.autocmd').event
local Layout = require('nui.layout')

local utils = require('hurl.utils')

local M = {}

local popup = Popup({
enter = true,
focusable = true,
border = {
local popups = {
bottom = Popup({ border = 'single', enter = true }),
top = Popup({ border = {
style = 'rounded',
} }),
}

local layout = Layout(
{
position = '50%',
size = {
width = 80,
height = 40,
},
},
position = '50%',
size = {
width = '80%',
height = '60%',
},
})
Layout.Box({
Layout.Box(popups.top, { size = {
height = '20%',
} }),
Layout.Box(popups.bottom, { grow = 1 }),
}, { dir = 'col' })
)

--- Format the body of the request
---@param body string
Expand All @@ -33,20 +43,75 @@ local function format(body, type)
end

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

-- Close popup when buffer is closed
for _, popup in pairs(popups) do
popup:on(event.BufLeave, function()
vim.schedule(function()
local current_buffer = vim.api.nvim_get_current_buf()
for _, p in pairs(popups) do
if p.bufnr == current_buffer then
return
end
end
layout:unmount()
end)
end)
end

-- Map q to quit
popups.top:map('n', 'q', '<cmd>q<cr>')
popups.bottom:map('n', 'q', '<cmd>q<cr>')

-- Map <Ctr-n> to next popup
popups.top:map('n', '<C-n>', function()
vim.api.nvim_set_current_win(popups.bottom.winid)
end)
popups.bottom:map('n', '<C-n>', function()
vim.api.nvim_set_current_win(popups.top.winid)
end)
-- Map <Ctr-p> to previous popup
popups.top:map('n', '<C-p>', function()
vim.api.nvim_set_current_win(popups.bottom.winid)
end)
popups.bottom:map('n', '<C-p>', function()
vim.api.nvim_set_current_win(popups.top.winid)
end)

-- Add headers to the top
local headers = {}
local line = 0
for k, v in pairs(data.headers) do
line = line + 1
table.insert(headers, k .. ': ' .. v)
end

-- Hide header block if empty headers
if line == 0 then
vim.api.nvim_win_close(popups.top.winid, true)
else
if line > 0 then
vim.api.nvim_buf_set_lines(popups.top.bufnr, 0, 1, false, headers)
end
end

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

vim.api.nvim_buf_set_lines(popup.bufnr, 0, -1, false, content)
-- Set content to highlight
vim.api.nvim_buf_set_option(popups.top.bufnr, 'filetype', 'bash')
vim.api.nvim_buf_set_option(popups.bottom.bufnr, 'filetype', type)

-- Add content to the bottom
vim.api.nvim_buf_set_lines(popups.bottom.bufnr, 0, -1, false, content)
end

return M
7 changes: 4 additions & 3 deletions lua/hurl/wrapper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ local on_output = function(code, data, event)
response.headers = {}
return
end

local status = tonumber(string.match(data[1], '([%w+]%d+)'))
head_state = 'start'
if status then
response.status = status
response.headers = { status = data[1] }
response.headers_str = data[1] .. '\r\n'
end

for i = 2, #data do
local line = data[i]
if line == '' or line == nil then
Expand Down Expand Up @@ -91,9 +93,9 @@ local function request(opts, callback)
local popup = require('hurl.popup')
--show body if it is json
if response.headers['content-type'] == 'application/json' then
popup.show(response.body, 'json')
popup.show(response, 'json')
else
popup.show(response.body, 'html')
popup.show(response, 'html')
end
elseif _HURL_CFG.mode == 'quickfix' then
vim.fn.setqflist({}, ' ', {
Expand All @@ -113,7 +115,6 @@ end
local function run_current_file(opts)
opts = opts or {}
table.insert(opts, vim.fn.expand('%:p'))
util.log('Run current file ' .. vim.fn.expand('%:p'))
request(opts)
end

Expand Down

0 comments on commit ba04d85

Please sign in to comment.