Skip to content

Commit

Permalink
feat(hurl_runner): add support for verbose mode in addition to very v…
Browse files Browse the repository at this point in the history
…erbose mode
  • Loading branch information
jellydn committed Oct 26, 2024
1 parent 5056c34 commit fc33c69
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 64 deletions.
12 changes: 4 additions & 8 deletions lua/hurl/lib/hurl_runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ local function pretty_print_body(body, content_type)
end
end

--- Run the hurl command in very verbose mode
--- Run the hurl command in verbose or very verbose mode
---@param filePath string
---@param fromEntry integer
---@param toEntry integer
function M.run_hurl_in_very_verbose(filePath, fromEntry, toEntry)
---@param isVeryVerbose boolean
function M.run_hurl_verbose(filePath, fromEntry, toEntry, isVeryVerbose)
local args = { filePath }
table.insert(args, '--very-verbose')
table.insert(args, isVeryVerbose and '--very-verbose' or '--verbose')
if fromEntry then
table.insert(args, '--from-entry')
table.insert(args, tostring(fromEntry))
Expand Down Expand Up @@ -82,7 +83,6 @@ function M.run_hurl_in_very_verbose(filePath, fromEntry, toEntry)

-- Show the command being run in the buffer
local command_str = 'hurl ' .. table.concat(args, ' ')
-- Show in shell command in the buffer
append_to_buffer({ '```sh', command_str, '```' })

-- Show the spinner
Expand Down Expand Up @@ -110,12 +110,10 @@ function M.run_hurl_in_very_verbose(filePath, fromEntry, toEntry)

if code ~= 0 then
utils.log_info('Hurl command failed with code ' .. code)
-- Show only the error message
append_to_buffer({ '# Error', stderr_data })
return
end

-- Log the success message
utils.log_info('Hurl command executed successfully')

-- Reset the buffer
Expand Down Expand Up @@ -200,8 +198,6 @@ function M.run_hurl_in_very_verbose(filePath, fromEntry, toEntry)

-- Append the formatted output to the buffer
append_to_buffer(output_lines)

-- TODO: Toggle folding when clicking on a section or use keybindings
end,
})
end
Expand Down
97 changes: 41 additions & 56 deletions lua/hurl/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -414,43 +414,52 @@ function M.setup()
utils.notify('hurl: env file changed to ' .. updated_env, vim.log.levels.INFO)
end, { nargs = '*', range = true })

-- Run Hurl in verbose mode and send output to quickfix
-- Run Hurl in verbose mode
utils.create_cmd('HurlVerbose', function(opts)
-- It should be the same logic with run at current line but with verbose flag
-- The response will be sent to quickfix
local is_support_hurl = utils.is_nightly() or utils.is_hurl_parser_available
local result = is_support_hurl and http.find_hurl_entry_positions_in_buffer()
or http.find_http_verb_positions_in_buffer()
if result.current > 0 and result.start_line and result.end_line then
utils.log_info(
'hurl: running request at line ' .. result.start_line .. ' to ' .. result.end_line
)
opts.fargs = opts.fargs or {}
opts.fargs = vim.list_extend(opts.fargs, { '--verbose' })
local filePath = vim.fn.expand('%:p')
local fromEntry = opts.fargs[1] and tonumber(opts.fargs[1]) or nil
local toEntry = opts.fargs[2] and tonumber(opts.fargs[2]) or nil

-- Clear quickfix list
vim.fn.setqflist({}, 'r', {
title = 'hurl',
lines = {},
})
run_at_lines(1, result.end_line, opts.fargs, function(code, data, event)
utils.log_info('hurl: verbose callback ' .. vim.inspect(code) .. vim.inspect(data))
vim.fn.setqflist({}, 'a', {
title = 'hurl - data',
lines = data,
})
vim.fn.setqflist({}, 'a', {
title = 'hurl - event',
lines = event,
})
vim.cmd('copen')
end)
else
if result then
utils.log_info('hurl: not HTTP method found in the current line' .. result.start_line)
-- Detect the current entry if fromEntry and toEntry are not provided
if not fromEntry or not toEntry then
local is_support_hurl = utils.is_nightly() or utils.is_hurl_parser_available
local result = is_support_hurl and http.find_hurl_entry_positions_in_buffer()
or http.find_http_verb_positions_in_buffer()
if result.current > 0 then
fromEntry = result.current
toEntry = result.current
else
utils.log_info('hurl: no HTTP method found in the current line')
utils.notify('hurl: no HTTP method found in the current line', vim.log.levels.INFO)
return
end
end

hurl_runner.run_hurl_verbose(filePath, fromEntry, toEntry, false)
end, { nargs = '*', range = true })

-- Run Hurl in very verbose mode
utils.create_cmd('HurlVeryVerbose', function(opts)
local filePath = vim.fn.expand('%:p')
local fromEntry = opts.fargs[1] and tonumber(opts.fargs[1]) or nil
local toEntry = opts.fargs[2] and tonumber(opts.fargs[2]) or nil

-- Detect the current entry if fromEntry and toEntry are not provided
if not fromEntry or not toEntry then
local is_support_hurl = utils.is_nightly() or utils.is_hurl_parser_available
local result = is_support_hurl and http.find_hurl_entry_positions_in_buffer()
or http.find_http_verb_positions_in_buffer()
if result.current > 0 then
fromEntry = result.current
toEntry = result.current
else
utils.log_info('hurl: no HTTP method found in the current line')
utils.notify('hurl: no HTTP method found in the current line', vim.log.levels.INFO)
return
end
end

hurl_runner.run_hurl_verbose(filePath, fromEntry, toEntry, true)
end, { nargs = '*', range = true })

-- NOTE: Get output from --json output
Expand Down Expand Up @@ -609,30 +618,6 @@ function M.setup()
utils.notify('hurl: no HTTP method found in the current line', vim.log.levels.INFO)
end
end, { nargs = '*', range = true })

-- Run Hurl in very verbose mode
utils.create_cmd('HurlVeryVerbose', function(opts)
local filePath = vim.fn.expand('%:p') -- Get the current file path
local fromEntry = opts.fargs[1] and tonumber(opts.fargs[1]) or nil
local toEntry = opts.fargs[2] and tonumber(opts.fargs[2]) or nil

-- Detect the current entry if fromEntry and toEntry are not provided
if not fromEntry or not toEntry then
local is_support_hurl = utils.is_nightly() or utils.is_hurl_parser_available
local result = is_support_hurl and http.find_hurl_entry_positions_in_buffer()
or http.find_http_verb_positions_in_buffer()
if result.current > 0 then
fromEntry = result.current
toEntry = result.current
else
utils.log_info('hurl: no HTTP method found in the current line')
utils.notify('hurl: no HTTP method found in the current line', vim.log.levels.INFO)
return
end
end

hurl_runner.run_hurl_in_very_verbose(filePath, fromEntry, toEntry)
end, { nargs = '*', range = true })
end

return M

0 comments on commit fc33c69

Please sign in to comment.