From 464f28e60665897f3d320166e5fe025183f83b32 Mon Sep 17 00:00:00 2001 From: "Dung Duc Huynh (Kaka)" <870029+jellydn@users.noreply.github.com> Date: Mon, 30 Oct 2023 19:34:10 +0800 Subject: [PATCH] feat: send tmp file to quicklist on hurl request (#11) * 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 --- hurl.txt | 22 ++++++++++++---------- lua/hurl/main.lua | 12 ++++++++++-- lua/hurl/popup.lua | 1 + lua/hurl/split.lua | 1 + lua/hurl/utils.lua | 16 +++++++++++++++- test/plugin_spec.lua | 6 +++--- 6 files changed, 42 insertions(+), 16 deletions(-) diff --git a/hurl.txt b/hurl.txt index 03ba533..d3b6634 100644 --- a/hurl.txt +++ b/hurl.txt @@ -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 @@ -32,5 +35,4 @@ buymeacoffee docstrings sweepai pygithub -sandboxed -foldmethod +sandboxed \ No newline at end of file diff --git a/lua/hurl/main.lua b/lua/hurl/main.lua index 426c940..ecbc083 100644 --- a/lua/hurl/main.lua +++ b/lua/hurl/main.lua @@ -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 @@ -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 diff --git a/lua/hurl/popup.lua b/lua/hurl/popup.lua index d8c97c8..91e5635 100644 --- a/lua/hurl/popup.lua +++ b/lua/hurl/popup.lua @@ -46,6 +46,7 @@ M.show = function(data, type) return end end + -- TODO: clear buffer on unmount layout:unmount() end) end) diff --git a/lua/hurl/split.lua b/lua/hurl/split.lua index 3548f2b..5cf2e50 100644 --- a/lua/hurl/split.lua +++ b/lua/hurl/split.lua @@ -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) diff --git a/lua/hurl/utils.lua b/lua/hurl/utils.lua index 0ac0291..e482d27 100644 --- a/lua/hurl/utils.lua +++ b/lua/hurl/utils.lua @@ -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 @@ -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 diff --git a/test/plugin_spec.lua b/test/plugin_spec.lua index c629863..f466a8f 100644 --- a/test/plugin_spec.lua +++ b/test/plugin_spec.lua @@ -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)