summary refs log tree commit diff
path: root/.vim
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2022-11-18 17:18:23 +0100
committerRobert Günzler <r@gnzler.io>2022-11-18 18:38:17 +0100
commite2d9f4923d377a723f6aa204c168bdf4ff64500c (patch)
treef3ef4b007a170950b4e40a9fafca01fa2d45d10a /.vim
parente92a1929b122305d1fb80b6eed2fb94f825ed178 (diff)
vim: refactor job module
Diffstat (limited to '.vim')
-rw-r--r--.vim/after/ftplugin/gitrebase.lua7
-rw-r--r--.vim/init.lua2
-rw-r--r--.vim/lua/internal/job.lua65
-rw-r--r--.vim/lua/internal/misc.lua10
4 files changed, 42 insertions, 42 deletions
diff --git a/.vim/after/ftplugin/gitrebase.lua b/.vim/after/ftplugin/gitrebase.lua
index c8a6221..50684f4 100644
--- a/.vim/after/ftplugin/gitrebase.lua
+++ b/.vim/after/ftplugin/gitrebase.lua
@@ -1,13 +1,12 @@
 
 local commit_preview = function()
   local commit = vim.api.nvim_get_current_line():match([[ ([0-9a-f]+) ]])
-  local j = package.loaded.job.jobstart {
+  local result = require('internal.job').jobstart({
     command = 'git',
     args = { 'show', commit },
-    format_title = function(_, _) return 'commit preview' end,
+    format_title = function(_, _) return 'git-show' end,
     populate_quickfix = false,
-  }
-  local result = j:wait():result()
+  }):wait():result()
 
   -- create buffer
   local bufnr = vim.api.nvim_create_buf(false, true)
diff --git a/.vim/init.lua b/.vim/init.lua
index 5f67e74..fbd3352 100644
--- a/.vim/init.lua
+++ b/.vim/init.lua
@@ -201,6 +201,8 @@ vim.keymap.set('n', '<leader>C', function()
   vim.fn.setreg('+', fnln)
   vim.notify(fnln)
 end, {desc='copy file path'})
+
+vim.keymap.set('n', '<leader>J', require('internal.job').list, {desc='list jobs started through internal.job'})
 -- }}}
 
 -- AUTO GROUPS -- {{{
diff --git a/.vim/lua/internal/job.lua b/.vim/lua/internal/job.lua
index c85fd67..5e3b6e1 100644
--- a/.vim/lua/internal/job.lua
+++ b/.vim/lua/internal/job.lua
@@ -9,6 +9,21 @@ local strip_ansi = function(line)
   line, _ = line:gsub(string.char(27) .. '[[0-9;]*m', '')
   return line
 end
+local setqf = function(opts, pid, data)
+  if data == nil then
+    return
+  end
+  vim.fn.setqflist({}, 'a', {
+    title = opts.format_title,
+    lines = {strip_ansi(data)},
+    efm = '%m',
+    context = {
+      cmd = {opts.command, unpack(opts.args)},
+      pid = pid,
+      source = 'jobstart',
+    }
+  })
+end
 
 --create a new job and register
 --@return ~/.local/share/nvim/site/pack/packer/start/plenary.nvim/lua/plenary/job.lua
@@ -18,14 +33,16 @@ function M.jobstart(opts)
   -- default options
   -- opts.enable_recording = true
   opts.enable_spinner = (opts.enable_spinner or true) and spinner_ok
-  opts.populate_quickfix = opts.populate_quickfix or true
-
-  local title
-  if opts.format_title then
-    title = opts.format_title(opts.command, opts.args)
-    opts.format_title = nil
+  opts.populate_quickfix = opts.populate_quickfix or 'onerror'
+
+  if opts.format_title == nil then
+    opts.format_title = table.concat({opts.command, unpack(opts.args)}, ' ')
+  elseif type(opts.format_title) == 'function' then
+    opts.format_title = opts.format_title(opts.command, opts.args)
+  elseif type(opts.format_title) == 'string' then
+    -- noop
   else
-    title = table.concat({opts.command, unpack(opts.args)}, ' ')
+    error(string.format('Invalid field "format_title" of type %s', type(opts.format_title)))
   end
 
   opts.on_start = vim.schedule_wrap(function(j)
@@ -35,7 +52,7 @@ function M.jobstart(opts)
       vim.fn.setqflist({}, 'r') -- clear quickfix
     end
     if opts.enable_spinner then
-      spinner.on_attach(j.pid, title, vim.fn.bufnr())
+      spinner.on_attach(j.pid, opts.format_title, vim.fn.bufnr())
       spinner.on_progress('begin', j.pid, j.pid)
     end
   end)
@@ -46,13 +63,13 @@ function M.jobstart(opts)
     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]]
-    end
     if opts.enable_spinner then
       spinner.on_progress('end', j.pid, j.pid)
       spinner.on_exit(nil, nil, j.pid)
     end
+    if opts.populate_quickfix then
+      vim.cmd.doautocmd [[QuickFixCmdPost]]
+    end
   end)
 
   opts.on_stdout = vim.schedule_wrap(function(err, data, j)
@@ -60,17 +77,8 @@ function M.jobstart(opts)
       vim.notify(string.format('[%d] error: %s', j.pid, err), vim.log.levels.ERROR)
     end -- handle error?
 
-    if opts.populate_quickfix and data ~= nil then
-      vim.fn.setqflist({}, 'a', {
-        title = title,
-        lines = {strip_ansi(data)},
-        efm = '%m',
-        context = {
-          cmd = {opts.command, unpack(opts.args)},
-          pid = j.pid,
-          source = 'jobstart',
-        }
-      })
+    if opts.populate_quickfix and opts.populate_quickfix ~= 'onerror' then
+      setqf(opts, j.pid, data)
     end
   end)
 
@@ -79,17 +87,8 @@ function M.jobstart(opts)
       vim.notify(string.format('[%d] error: %s', j.pid, err), vim.log.levels.ERROR)
     end -- handle error?
 
-    if opts.populate_quickfix and data ~= nil then
-      vim.fn.setqflist({}, 'a', {
-        title = title,
-        lines = {strip_ansi(data)},
-        efm = '%m',
-        context = {
-          cmd = {opts.command, unpack(opts.args)},
-          pid = j.pid,
-          source = 'jobstart',
-        }
-      })
+    if opts.populate_quickfix then
+      setqf(opts, j.pid, data)
     end
   end)
 
diff --git a/.vim/lua/internal/misc.lua b/.vim/lua/internal/misc.lua
index 5a2ae21..6f79508 100644
--- a/.vim/lua/internal/misc.lua
+++ b/.vim/lua/internal/misc.lua
@@ -187,15 +187,14 @@ function M.link_preview()
   local buf = vim.api.nvim_get_current_buf()
   local ln = (vim.api.nvim_win_get_cursor(0)[1]) - 1
 
-  local j = require('job').jobstart({
+  require('job').jobstart({
     format_title = function() return "link-preview"  end,
     command = vim.env.SHELL,
     args = {'-c', [[ curl -sSfL ]] .. link .. [[ | grep -iPo '(?<=property="og:title" content=")([^\"]+)(?=")' ]]},
     populate_quickfix = false,
     enable_recording = true,
-  })
-
-  j:after_success(vim.schedule_wrap(function(j, code, _)
+  }):after_success(
+    vim.schedule_wrap(function(j, code, _)
       local results = j:result()
       if not code == 0 or #results == 0 then return end
 
@@ -204,7 +203,8 @@ function M.link_preview()
         virt_text = {{results[1], 'Error'}},
         virt_text_pos = 'eol'
       })
-  end))
+    end)
+  )
 end
 
 return M