summary refs log tree commit diff
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2022-04-20 14:13:26 +0200
committerRobert Günzler <r@gnzler.io>2022-04-20 14:13:26 +0200
commit97d8e0329d1c08b0b2ac0f0e518d20241cea1bda (patch)
tree69df3d3c407430ddcf0cb9a8635d0bcaeffd1508
parent73bb00e4afea78f4f4b14c621300ec0912e6210e (diff)
vim: rework job.lua
-rw-r--r--.vim/lua/job.lua111
1 files changed, 51 insertions, 60 deletions
diff --git a/.vim/lua/job.lua b/.vim/lua/job.lua
index 657a72d..0969861 100644
--- a/.vim/lua/job.lua
+++ b/.vim/lua/job.lua
@@ -1,24 +1,23 @@
 local Job = require ('plenary.job')
-local List = require('plenary.collections.py_list')
 local spinner = require('spinner.core')
 
 local M = {
   current_jobs = {}
 }
 
+local strip_ansi = function(line)
+  return line:gsub(string.char(27) .. '[[0-9;]*m', '')
+end
+
 --create a new job and register
 --@return ~/.local/share/nvim/site/pack/packer/start/plenary.nvim/lua/plenary/job.lua
 function M.jobstart(opts)
   opts = opts or {}
 
-  -- required options
-  vim.tbl_extend('force', opts, {
-    enable_recording = true,  -- enable job:result()
-  })
-
   -- default options
-  opts.populate_quickfix = opts.populate_quickfix or true
+  -- opts.enable_recording = true
   opts.enable_spinner = opts.enable_spinner or true
+  opts.populate_quickfix = opts.populate_quickfix or true
 
   local title
   if opts.format_title then
@@ -28,31 +27,60 @@ function M.jobstart(opts)
     title = opts.command
   end
 
-  opts.on_exit = vim.schedule_wrap(function(j, code)
+  opts.on_start = vim.schedule_wrap(function(j)
+    table.insert(M.current_jobs, j.pid, j)
+
+    if opts.enable_spinner then
+      spinner.on_attach(j.pid, title, vim.fn.bufnr())
+      spinner.on_progress('begin', j.pid, j.pid)
+    end
+  end)
+
+  opts.on_exit = vim.schedule_wrap(function(j, _)
+    table.remove(M.current_jobs, j.pid)
+
+    if opts.populate_quickfix then
+      vim.cmd([[doautocmd QuickFixCmdPost]])
+      -- vim.fn.setqflist({}, 'r', {
+      --   lines = {},
+      --   id = j.pid,
+      -- })
+    end
+
     if opts.enable_spinner then
       spinner.on_progress('end', j.pid, j.pid)
       spinner.on_exit(nil, nil, j.pid)
-      table.remove(M.current_jobs, j.pid)
     end
+  end)
 
-    local lines
-    if code == 0 then
-      lines = j:result()
-    else
-      lines = j:stderr_result()
-    end
+  opts.on_stdout = vim.schedule_wrap(function(error, data, j)
+    if error then
+      vim.notify('error: ' .. error)
+      return
+    end -- handle error?
 
-    -- strip ansi terminal color codes
-    for i = 1, #lines do
-      lines[i] = (lines[i]):gsub(string.char(27) .. '[[0-9;]*m', '')
+    if opts.populate_quickfix and data then
+      vim.fn.setqflist({}, 'a', {
+        title = title,
+        lines = {strip_ansi(data)},
+        efm = '%m',
+        id = j.pid,
+      })
     end
+  end)
+
+  opts.on_stderr = vim.schedule_wrap(function(error, data, j)
+    if error then
+      vim.notify('error: ' .. error)
+      return
+    end -- handle error?
 
-    -- only show quickfix if there is output
-    if opts.populate_quickfix and #lines > 0 then
-      vim.fn.setqflist({}, 'r', {
+    if opts.populate_quickfix and data then
+      vim.fn.setqflist({}, 'a', {
         title = title,
-        lines = lines,
+        lines = {strip_ansi(data)},
         efm = '%m',
+        id = j.pid,
       })
       vim.cmd([[doautocmd QuickFixCmdPost]])
     end
@@ -60,12 +88,6 @@ function M.jobstart(opts)
 
   local j = Job:new(opts)
   j:start()
-  M.current_jobs[j.pid] = j
-
-  if opts.enable_spinner then
-    spinner.on_attach(j.pid, title, vim.fn.bufnr())
-    spinner.on_progress('begin', j.pid, j.pid)
-  end
 
   return j
 end
@@ -93,40 +115,9 @@ function M.sh(command_string)
 
     -- strip 'sh -c' from the title (used by progress reporting etc.)
     format_title = function(_, args)
-      return List(args):slice(2,#args):join(' ')
+      return require('plenary.collections.py_list')(args):slice(2,#args):join(' ')
     end
   }
 end
 
-
-function M.gen_entry_from_job()
-  return function(entry)
-    return {
-      value = entry,
-      ordinal = entry.pid,
-      display = entry.command .. ' ' .. table.concat(entry.args, ' '),
-    }
-  end
-end
-
-function M.cancel_job()
-  if #M.current_jobs < 1 then return end
-  require('telescope.pickers').new({}, {
-    prompt_title = 'Jobs',
-    finder = require('telescope.finders').new_table {
-      results = M.current_jobs,
-      entry_maker = M.gen_entry_from_job(),
-    },
-    sorter = require('telescope.sorters').fuzzy_with_index_bias(),
-    attach_mappings = function(bufnr)
-      require('telescope.actions.set').select:replace(function(bufnr, type)
-        local entry = require('telescope.actions.state').get_selected_entry()
-        require('telescope.actions').close(bufnr)
-        entry.value:shutdown(1, 'SIGKILL')
-      end)
-      return true
-    end
-  }):find()
-end
-
 return M