Skip to content

Commit

Permalink
refactor(hurl): rename functions and variables for clarity and remove…
Browse files Browse the repository at this point in the history
… redundant code
  • Loading branch information
jellydn committed Oct 27, 2024
1 parent d0db829 commit a7cdb3c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 42 deletions.
18 changes: 8 additions & 10 deletions lua/hurl/lib/hurl_runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ local M = {}
M.is_running = false
M.start_time = nil
M.response = {}
M.head_state = ''

--- Log the Hurl command
---@param cmd table
local function log_hurl_command(cmd)
local function save_last_hurl_command(cmd)
local command_str = table.concat(cmd, ' ')
_HURL_GLOBAL_CONFIG.last_hurl_command = command_str
utils.log_info('hurl: running command: ' .. command_str)
Expand Down Expand Up @@ -91,7 +90,7 @@ function M.run_hurl_verbose(filePath, fromEntry, toEntry, isVeryVerbose, additio

-- Log the Hurl command
local hurl_command = 'hurl ' .. table.concat(args, ' ')
log_hurl_command({ 'hurl', unpack(args) })
save_last_hurl_command({ 'hurl', unpack(args) })

-- Always use split mode for verbose commands
local display = require('hurl.split')
Expand Down Expand Up @@ -237,15 +236,14 @@ function M.execute_hurl_cmd(opts, callback)
M.is_running = true
M.start_time = vim.loop.hrtime() -- Capture the start time
spinner.show()
M.head_state = ''
utils.log_info('hurl: running request')
utils.notify('hurl: running request', vim.log.levels.INFO)

local is_json_mode = vim.tbl_contains(opts, '--json')
local is_file_mode = utils.has_file_in_opts(opts)

-- Add verbose mode by default if not in JSON mode
if not is_json_mode then
if not is_json_mode and not vim.tbl_contains(opts, '--verbose') then
table.insert(opts, '--verbose')
end

Expand Down Expand Up @@ -286,7 +284,7 @@ function M.execute_hurl_cmd(opts, callback)
end
M.response = {}

log_hurl_command(cmd)
save_last_hurl_command(cmd)

-- Clear the display and show processing message with Hurl command
local display = require('hurl.' .. _HURL_GLOBAL_CONFIG.mode)
Expand Down Expand Up @@ -317,7 +315,7 @@ function M.execute_hurl_cmd(opts, callback)
utils.notify('Hurl command failed. Check the split view for details.', vim.log.levels.ERROR)

-- Show error in split view
local display = require('hurl.split')
local split = require('hurl.split')
local error_data = {
body = '# Hurl Error\n\n```sh\n' .. stderr_data .. '\n```',
headers = {},
Expand All @@ -327,7 +325,7 @@ function M.execute_hurl_cmd(opts, callback)
response_time = 0,
curl_command = 'N/A',
}
display.show(error_data, 'markdown')
split.show(error_data, 'markdown')
return
end

Expand All @@ -349,7 +347,7 @@ function M.execute_hurl_cmd(opts, callback)
local result = hurl_parser.parse_hurl_output(stderr_data, stdout_data)

-- Display the result using popup or split based on the configuration
local display = require('hurl.' .. _HURL_GLOBAL_CONFIG.mode)
local container = require('hurl.' .. _HURL_GLOBAL_CONFIG.mode)

-- Prepare the data for display
local last_entry = result.entries[#result.entries]
Expand Down Expand Up @@ -394,7 +392,7 @@ function M.execute_hurl_cmd(opts, callback)

display_data.display_type = display_type

display.show(display_data, display_type)
container.show(display_data, display_type)

history.update_history(display_data)

Expand Down
58 changes: 26 additions & 32 deletions lua/hurl/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ local function run_current_file(opts)
hurl_runner.execute_hurl_cmd(opts)
end

-- Create a temporary file with the lines to run
---@param lines string[]
-- Run selection
---@param opts table The options
---@param callback? function The callback function
local function run_lines(lines, opts, callback)
local function run_selection(opts)
opts = opts or {}
local lines = utils.get_visual_selection()
if not lines then
return
end

-- Create a temporary file with the lines to run
local fname = utils.create_tmp_file(lines)
if not fname then
Expand All @@ -30,7 +34,7 @@ local function run_lines(lines, opts, callback)

-- Add the temporary file to the arguments
table.insert(opts, fname)
hurl_runner.execute_hurl_cmd(opts, callback)
hurl_runner.execute_hurl_cmd(opts)

-- Clean up the temporary file after a delay
local timeout = 1000
Expand All @@ -45,34 +49,27 @@ local function run_lines(lines, opts, callback)
end, timeout)
end

-- Run selection
---@param opts table The options
local function run_selection(opts)
opts = opts or {}
local lines = utils.get_visual_selection()
if not lines then
return
end

run_lines(lines, opts)
end

-- Run at current line
---@param start_line number
---@param end_line number
---@param end_line number|nil
---@param opts table
---@param callback? function
local function run_at_lines(start_line, end_line, opts, callback)
opts = opts or {}
-- Get the lines from the buffer
local lines = vim.api.nvim_buf_get_lines(0, start_line - 1, end_line, false)
local file_path = vim.fn.expand('%:p')

if not lines or vim.tbl_isempty(lines) then
utils.notify('hurl: no lines to run', vim.log.levels.WARN)
return
-- Insert the file path first
table.insert(opts, file_path)

-- Then add the --from-entry and --to-entry options
table.insert(opts, '--from-entry')
table.insert(opts, tostring(start_line))
if end_line then
table.insert(opts, '--to-entry')
table.insert(opts, tostring(end_line))
end

run_lines(lines, opts, callback)
hurl_runner.execute_hurl_cmd(opts, callback)
end

-- Helper function to run verbose commands in split mode
Expand Down Expand Up @@ -115,7 +112,7 @@ function M.setup()
utils.log_info(
'hurl: running request at line ' .. result.start_line .. ' to ' .. result.end_line
)
run_at_lines(result.start_line, result.end_line, opts.fargs)
run_at_lines(result.current, result.current, opts.fargs)
else
utils.log_info('hurl: not HTTP method found in the current line' .. result.start_line)
utils.notify('hurl: no HTTP method found in the current line', vim.log.levels.INFO)
Expand All @@ -129,7 +126,7 @@ function M.setup()
or http.find_http_verb_positions_in_buffer()
utils.log_info('hurl: running request to entry #' .. vim.inspect(result))
if result.current > 0 then
run_at_lines(1, result.end_line, opts.fargs)
run_at_lines(1, result.current, opts.fargs)
else
utils.log_info('hurl: not HTTP method found in the current line' .. result.end_line)
utils.notify('hurl: no HTTP method found in the current line', vim.log.levels.INFO)
Expand Down Expand Up @@ -336,13 +333,10 @@ function M.setup()
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
)
if result.current > 0 then
utils.log_info('hurl: running request from entry ' .. result.current .. ' to end')
opts.fargs = opts.fargs or {}
local end_line = vim.api.nvim_buf_line_count(0)
run_at_lines(result.start_line, end_line, opts.fargs)
run_at_lines(result.current, nil, opts.fargs)
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)
Expand Down

0 comments on commit a7cdb3c

Please sign in to comment.