Skip to content

Commit

Permalink
refactor: use split instead of quickfix
Browse files Browse the repository at this point in the history
  • Loading branch information
jellydn committed Oct 22, 2023
1 parent 0067e29 commit ad1aecd
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 51 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
## Features

- 🚀 Execute HTTP requests directly from `.hurl` files.
- 👁‍🗨 Multiple display modes for API response: popup or quickfix.
- 👁‍🗨 Multiple display modes for API response: popup or split.
- 🌈 Highly customizable through Neovim settings.

## Usage
Expand All @@ -38,7 +38,7 @@ Add the following configuration to your Neovim setup:
-- Show debugging info
debug = true,
-- Show response in popup or in quick list
-- popup | quickfix
-- popup | split
mode = "popup", --
},
keys = {
Expand Down
25 changes: 13 additions & 12 deletions hurl.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
nvim
quickfix
autocmd
systemlist
bufnr
winid
vlog
vararg
getpos
Expand All @@ -17,18 +22,14 @@ setqflist
copen
fargs
nargs
Neovim
jellydn
Munif
Tanjim
Huynh
kofi
buymeacoffee
docstrings
sweepai
pygithub
sandboxed
jellydn
quickfix'
systemlist
bufnr
Tanjim
quickfix
Munif
autocmd
keymap
noremap
winid
sandboxed
7 changes: 1 addition & 6 deletions lua/hurl/init.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
--- Global configuration for hurl.nvim
_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.
-- - mode: ('popup' | 'split') default: popup.
function M.setup(options)
_HURL_CFG = vim.tbl_extend('force', _HURL_CFG, options or {})

Expand Down
30 changes: 5 additions & 25 deletions lua/hurl/popup.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,6 @@ local layout = Layout(
}, { dir = 'col' })
)

--- 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 data table
--- - body string
Expand Down Expand Up @@ -85,23 +71,17 @@ M.show = function(data, type)
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

local headers_table = utils.render_header_table(data.headers)
-- Hide header block if empty headers
if line == 0 then
if headers_table.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)
if headers_table.line > 0 then
vim.api.nvim_buf_set_lines(popups.top.bufnr, 0, 1, false, headers_table.headers)
end
end

local content = format(data.body, type)
local content = utils.format(data.body, type)
if not content then
return
end
Expand Down
51 changes: 51 additions & 0 deletions lua/hurl/split.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
local Split = require('nui.split')
local event = require('nui.utils.autocmd').event

local split = Split({
relative = 'editor',
position = 'bottom',
size = '30%',
})

local utils = require('hurl.utils')

local M = {}

-- Show content in a popup
---@param data table
--- - body string
--- - headers table
---@param type 'json' | 'html'
M.show = function(data, type)
-- mount/open the component
split:mount()

-- unmount component when cursor leaves buffer
split:on(event.BufLeave, function()
split:unmount()
end)

-- Add headers to the top
local headers_table = utils.render_header_table(data.headers)
-- Hide header block if empty headers
if headers_table.line == 0 then
utils.log('no headers')
else
if headers_table.line > 0 then
vim.api.nvim_buf_set_lines(split.bufnr, 0, 1, false, headers_table.headers)
end
end

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

-- Set content to highlight
vim.api.nvim_buf_set_option(split.bufnr, 'filetype', type)

-- Add content to the bottom
vim.api.nvim_buf_set_lines(split.bufnr, headers_table.line, -1, false, content)
end

return M
44 changes: 44 additions & 0 deletions lua/hurl/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,48 @@ util.create_cmd = function(cmd, func, opt)
vim.api.nvim_create_user_command(cmd, func, opt)
end

--- Format the body of the request
---@param body string
---@param type 'json' | 'html'
---@return string[] | nil
util.format = function(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
util.log('formatter failed' .. tostring(vim.v.shell_error))
return nil
end
return stdout
end

--- Render header table
---@param headers table
util.render_header_table = function(headers)
local result = {}
local maxKeyLength = 0
for k, _ in pairs(headers) do
maxKeyLength = math.max(maxKeyLength, #k)
end

local line = 0
for k, v in pairs(headers) do
line = line + 1
if line == 1 then
-- Add header for the table view
table.insert(
result,
string.format('%-' .. maxKeyLength .. 's | %s', 'Header Key', 'Header Value')
)

line = line + 1
end
table.insert(result, string.format('%-' .. maxKeyLength .. 's | %s', k, v))
end

return {
line = line,
headers = result,
}
end

return util
14 changes: 8 additions & 6 deletions lua/hurl/wrapper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,14 @@ local function request(opts, callback)
else
popup.show(response, 'html')
end
elseif _HURL_CFG.mode == 'quickfix' then
vim.fn.setqflist({}, ' ', {
title = 'hurl finished',
lines = lines,
})
vim.cmd('copen')
elseif _HURL_CFG.mode == 'split' then
local split = require('hurl.split')
--show body if it is json
if response.headers['content-type'] == 'application/json' then
split.show(response, 'json')
else
split.show(response, 'html')
end
end
end
end,
Expand Down

0 comments on commit ad1aecd

Please sign in to comment.