Skip to content

Commit

Permalink
refactor: add log error to util
Browse files Browse the repository at this point in the history
  • Loading branch information
jellydn committed Oct 23, 2023
1 parent 2a9bf8f commit fe60493
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lua/hurl/split.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ M.show = function(data, type)
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')
utils.log_info('no headers')
else
if headers_table.line > 0 then
vim.api.nvim_buf_set_lines(split.bufnr, 0, 1, false, headers_table.headers)
Expand Down
20 changes: 15 additions & 5 deletions lua/hurl/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,28 @@ local log = require('hurl.vlog')

local util = {}

--- Log function
--- Print the variable to stdout
--- Log info
---@vararg any
util.log = function(...)
-- Only print when debug is on
util.log_info = function(...)
-- Only save log when debug is on
if not _HURL_CFG.debug then
return
end

log.info(...)
end

--- Log error
---@vararg any
util.log_error = function(...)
-- Only save log when debug is on
if not _HURL_CFG.debug then
return
end

log.error(...)
end

--- Get visual selection
---@return string[]
util.get_visual_selection = function()
Expand Down Expand Up @@ -70,7 +80,7 @@ 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))
util.log_error('formatter failed' .. tostring(vim.v.shell_error))
return nil
end
return stdout
Expand Down
19 changes: 7 additions & 12 deletions lua/hurl/wrapper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ local on_output = function(code, data, event)
table.remove(data, 1)
end
if not data[1] then
util.log('no data')
return
end

if event == 'stderr' and #data > 1 then
util.log('stderr', data)
util.log_error('hurl: stderr', data)
response.body = data
response.raw = data
response.headers = {}
Expand All @@ -35,7 +34,6 @@ local on_output = function(code, data, event)
for i = 2, #data do
local line = data[i]
if line == '' or line == nil then
util.log(i, 'change to body')
head_state = 'body'
elseif head_state == 'start' then
local key, value = string.match(line, '([%w-]+):%s*(.+)')
Expand All @@ -50,9 +48,9 @@ local on_output = function(code, data, event)
end
response.raw = data

util.log('hurl: response status ' .. response.status)
util.log('hurl: response headers ' .. vim.inspect(response.headers))
util.log('hurl: response body ' .. response.body)
util.log_info('hurl: response status ' .. response.status)
util.log_info('hurl: response headers ' .. vim.inspect(response.headers))
util.log_info('hurl: response body ' .. response.body)
end

--- Call hurl command
Expand All @@ -66,7 +64,7 @@ local function request(opts, callback)
on_stdout = on_output,
on_stderr = on_output,
on_exit = function(i, code)
util.log('exit', i, code)
util.log_info('exit code ' .. code)
if code ~= 0 then
vim.notify(
string.format(
Expand All @@ -78,14 +76,12 @@ local function request(opts, callback)
)
end

util.log(response)
if callback then
return callback(response)
else
-- show messages
local lines = response.raw or response.body
if #lines == 0 then
vim.notify('hurl: no response')
return
end

Expand Down Expand Up @@ -122,8 +118,7 @@ end

--- Run selection
---@param opts table The options
---@param range number The range
local function run_selection(opts, range)
local function run_selection(opts)
opts = opts or {}
local lines = util.get_visual_selection()
if not lines then
Expand All @@ -145,7 +140,7 @@ end
function M.setup()
util.create_cmd('HurlRun', function(opts)
if opts.range ~= 0 then
run_selection(opts.fargs, opts.range)
run_selection(opts.fargs)
else
run_current_file(opts.fargs)
end
Expand Down

0 comments on commit fe60493

Please sign in to comment.