diff --git a/lua/hurl/split.lua b/lua/hurl/split.lua index 691c854..d4a8c01 100644 --- a/lua/hurl/split.lua +++ b/lua/hurl/split.lua @@ -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) diff --git a/lua/hurl/utils.lua b/lua/hurl/utils.lua index f746399..0f22639 100644 --- a/lua/hurl/utils.lua +++ b/lua/hurl/utils.lua @@ -2,11 +2,10 @@ 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 @@ -14,6 +13,17 @@ util.log = function(...) 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() @@ -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 diff --git a/lua/hurl/wrapper.lua b/lua/hurl/wrapper.lua index 7bc0009..ff1a01a 100644 --- a/lua/hurl/wrapper.lua +++ b/lua/hurl/wrapper.lua @@ -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 = {} @@ -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*(.+)') @@ -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 @@ -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( @@ -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 @@ -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 @@ -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