summary refs log tree commit diff
path: root/.vim/lua/internal/job.lua
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2022-11-04 16:40:29 +0100
committerRobert Günzler <r@gnzler.io>2022-11-18 18:37:24 +0100
commite06d4c6b0065bc92f59bb7f4e09093f51c9659ef (patch)
treebcd7c527cba648ca086c2394a644ae537aa8dd00 /.vim/lua/internal/job.lua
parent2c754ebab57b44021b837da51c5ee43d369e397c (diff)
vim/internal/job: improve
Diffstat (limited to '.vim/lua/internal/job.lua')
-rw-r--r--.vim/lua/internal/job.lua60
1 files changed, 34 insertions, 26 deletions
diff --git a/.vim/lua/internal/job.lua b/.vim/lua/internal/job.lua
index f9f7bf3..c85fd67 100644
--- a/.vim/lua/internal/job.lua
+++ b/.vim/lua/internal/job.lua
@@ -6,7 +6,8 @@ local M = {
 }
 
 local strip_ansi = function(line)
-  return line:gsub(string.char(27) .. '[[0-9;]*m', '')
+  line, _ = line:gsub(string.char(27) .. '[[0-9;]*m', '')
+  return line
 end
 
 --create a new job and register
@@ -24,70 +25,71 @@ function M.jobstart(opts)
     title = opts.format_title(opts.command, opts.args)
     opts.format_title = nil
   else
-    title = opts.command
+    title = table.concat({opts.command, unpack(opts.args)}, ' ')
   end
 
   opts.on_start = vim.schedule_wrap(function(j)
     table.insert(M.current_jobs, j.pid, j)
 
     if opts.populate_quickfix then
-      -- clear quickfix
-      vim.fn.setqflist({}, 'r')
+      vim.fn.setqflist({}, 'r') -- clear quickfix
     end
-
     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, _)
+  opts.on_exit = vim.schedule_wrap(function(j, code)
     table.remove(M.current_jobs, j.pid)
 
+    if not (code == 0) then
+      vim.notify(string.format('[%d] exit %d', j.pid, code), vim.log.levels.ERROR)
+    end
     if opts.populate_quickfix then
-      vim.cmd([[doautocmd QuickFixCmdPost]])
-      -- vim.fn.setqflist({}, 'r', {
-      --   lines = {},
-      --   id = j.pid,
-      -- })
+      vim.cmd.doautocmd [[QuickFixCmdPost]]
     end
-
     if opts.enable_spinner then
       spinner.on_progress('end', j.pid, j.pid)
       spinner.on_exit(nil, nil, j.pid)
     end
   end)
 
-  opts.on_stdout = vim.schedule_wrap(function(error, data, j)
-    if error then
-      vim.notify('error: ' .. error)
-      return
+  opts.on_stdout = vim.schedule_wrap(function(err, data, j)
+    if err ~= nil then
+      vim.notify(string.format('[%d] error: %s', j.pid, err), vim.log.levels.ERROR)
     end -- handle error?
 
-    if opts.populate_quickfix and data then
+    if opts.populate_quickfix and data ~= nil then
       vim.fn.setqflist({}, 'a', {
         title = title,
         lines = {strip_ansi(data)},
         efm = '%m',
-        id = j.pid,
+        context = {
+          cmd = {opts.command, unpack(opts.args)},
+          pid = j.pid,
+          source = 'jobstart',
+        }
       })
     end
   end)
 
-  opts.on_stderr = vim.schedule_wrap(function(error, data, j)
-    if error then
-      vim.notify('error: ' .. error)
-      return
+  opts.on_stderr = vim.schedule_wrap(function(err, data, j)
+    if err ~= nil then
+      vim.notify(string.format('[%d] error: %s', j.pid, err), vim.log.levels.ERROR)
     end -- handle error?
 
-    if opts.populate_quickfix and data then
+    if opts.populate_quickfix and data ~= nil then
       vim.fn.setqflist({}, 'a', {
         title = title,
         lines = {strip_ansi(data)},
         efm = '%m',
-        id = j.pid,
+        context = {
+          cmd = {opts.command, unpack(opts.args)},
+          pid = j.pid,
+          source = 'jobstart',
+        }
       })
-      vim.cmd([[doautocmd QuickFixCmdPost]])
     end
   end)
 
@@ -132,9 +134,15 @@ function M.sh(command_string)
 
     -- strip 'sh -c' from the title (used by progress reporting etc.)
     format_title = function(_, args)
-      return require('plenary.collections.py_list')(args):slice(2,#args):join(' ')
+      return table.concat({unpack(args, 2, #args)}, ' ')
     end
   }
 end
 
+
+function M.list()
+  require('telescope').load_extension('jobs')
+  require('telescope._extensions').manager['jobs']['jobs']()
+end
+
 return M