Skip to content

Commit

Permalink
feat: send tmp file to quicklist on hurl request (#11)
Browse files Browse the repository at this point in the history
* feat: send tmp file to quicklist on hurl request

feat: introduce new config for hurl command

refactor: drop hurl options on init

* fix(test): default config to split
  • Loading branch information
jellydn committed Oct 30, 2023
1 parent 61c23d6 commit 464f28e
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 16 deletions.
22 changes: 12 additions & 10 deletions hurl.txt
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
nvim
jobstart
quickfix
setqflist
copen
fargs
nargs
autocmd
systemlist
bufnr
winid
foldmethod
vlog
vararg
getpos
tempname
fnamemodify
bufname
getpid
stdpath
systemlist
tjdevries
neovim
echohl
outfile
stdpath
nameupper
lineinfo
currentline
echom
jobstart
setqflist
copen
fargs
nargs
Neovim
jellydn
Munif
Expand All @@ -32,5 +35,4 @@ buymeacoffee
docstrings
sweepai
pygithub
sandboxed
foldmethod
sandboxed
12 changes: 10 additions & 2 deletions lua/hurl/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@ local function request(opts, callback)
util.log_info('exit at ' .. i .. ' , code ' .. code)
if code ~= 0 then
-- Send error code and response to quickfix and open it
vim.fn.setqflist({ { filename = '', text = vim.inspect(response.body) } })
vim.fn.setqflist({ { filename = vim.inspect(cmd), text = vim.inspect(response.body) } })
vim.cmd('copen')
end

vim.notify('hurl: request finished', vim.log.levels.INFO)

if callback then
return callback(response)
else
Expand Down Expand Up @@ -118,13 +120,19 @@ local function run_selection(opts)
local fname = util.create_tmp_file(lines)

if not fname then
vim.notify('hurl: create tmp file failed. Please try again!', vim.log.levels.WARN)
return
end

table.insert(opts, fname)
request(opts)
vim.defer_fn(function()
os.remove(fname)
local success = os.remove(fname)
if not success then
vim.notify('hurl: remove file failed', vim.log.levels.WARN)
else
util.log_info('hurl: remove file success ' .. fname)
end
end, 1000)
end

Expand Down
1 change: 1 addition & 0 deletions lua/hurl/popup.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ M.show = function(data, type)
return
end
end
-- TODO: clear buffer on unmount
layout:unmount()
end)
end)
Expand Down
1 change: 1 addition & 0 deletions lua/hurl/split.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ M.show = function(data, type)

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

Expand Down
16 changes: 15 additions & 1 deletion lua/hurl/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,15 @@ end
---@param content any
---@return string|nil
util.create_tmp_file = function(content)
local tmp_file = vim.fn.tempname()
-- create temp file base on pid and datetime
local tmp_file = string.format(
'%s/%s.hurl',
vim.fn.stdpath('cache'),
vim.fn.getpid() .. '-' .. vim.fn.localtime()
)

if not tmp_file then
vim.notify('hurl: failed to create tmp file', vim.log.levels.ERROR)
return
end

Expand All @@ -60,6 +67,13 @@ util.create_tmp_file = function(content)
f:write(content)
end
f:close()

-- Send to quicklist to open the temp file in debug mode
if _HURL_CFG.debug then
vim.fn.setqflist({ { filename = tmp_file, text = 'hurl.nvim' } })
vim.cmd('copen')
end

return tmp_file
end

Expand Down
6 changes: 3 additions & 3 deletions test/plugin_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ describe('Hurl.nvim plugin', function()
local hurl = require('hurl')
assert.truthy(hurl)

assert.are.same('popup', _HURL_CFG.mode)
assert.are.same('split', _HURL_CFG.mode)
assert.are.same(false, _HURL_CFG.debug)
end)

it('should be able parse the configuration file', function()
require('hurl').setup({
debug = true,
mode = 'split',
mode = 'popup',
})

assert.are.same('split', _HURL_CFG.mode)
assert.are.same('popup', _HURL_CFG.mode)
assert.are.same(true, _HURL_CFG.debug)
end)
end)

0 comments on commit 464f28e

Please sign in to comment.