summary refs log tree commit diff
path: root/.vim/lua/internal/job.lua
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2023-01-19 18:33:43 +0100
committerRobert Günzler <r@gnzler.io>2023-01-19 18:33:43 +0100
commitfc67eecc8a40bf28d3d2b190702e58c580bfba9f (patch)
tree0b8d0455046ae484e036d2f9dfa69306bbea811d /.vim/lua/internal/job.lua
parentc03b74ce367d6e9e2988dbb4a18985bc372dd8a6 (diff)
vim: format config with stylua
Signed-off-by: Robert Günzler <r@gnzler.io>
Diffstat (limited to '.vim/lua/internal/job.lua')
-rw-r--r--.vim/lua/internal/job.lua59
1 files changed, 36 insertions, 23 deletions
diff --git a/.vim/lua/internal/job.lua b/.vim/lua/internal/job.lua
index 64d9837..42a548b 100644
--- a/.vim/lua/internal/job.lua
+++ b/.vim/lua/internal/job.lua
@@ -1,7 +1,5 @@
-local spinner_ok, spinner = pcall(require, 'spinner.core')
-
 local M = {
-  current_jobs = {}
+  current_jobs = {},
 }
 
 local strip_ansi = function(line)
@@ -14,14 +12,15 @@ local setqf = function(opts, pid, data)
   end
   vim.fn.setqflist({}, 'a', {
     title = opts.format_title,
-    lines = {strip_ansi(data)},
+    lines = { strip_ansi(data) },
     efm = '%m',
     context = {
-      cmd = {opts.command, unpack(opts.args)},
+      cmd = { opts.command, unpack(opts.args) },
       pid = pid,
       source = 'jobstart',
-    }
+    },
   })
+  vim.cmd.doautocmd('QuickFixCmdPost')
 end
 
 --create a new job and register
@@ -29,13 +28,14 @@ end
 function M.jobstart(opts)
   opts = opts or {}
 
+  local spinner_ok, spinner = pcall(require, 'spinner.core')
+
   -- default options
-  -- opts.enable_recording = true
   opts.enable_spinner = (opts.enable_spinner or true) and spinner_ok
   opts.populate_quickfix = opts.populate_quickfix or 'onerror'
 
   if opts.format_title == nil then
-    opts.format_title = table.concat({opts.command, unpack(opts.args)}, ' ')
+    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
@@ -45,7 +45,7 @@ function M.jobstart(opts)
   end
 
   opts.on_start = vim.schedule_wrap(function(j)
-    table.insert(M.current_jobs, j.pid, j)
+    M.current_jobs[j.pid] = j
 
     if opts.populate_quickfix then
       vim.fn.setqflist({}, 'r') -- clear quickfix
@@ -57,7 +57,7 @@ function M.jobstart(opts)
   end)
 
   opts.on_exit = vim.schedule_wrap(function(j, code)
-    table.remove(M.current_jobs, j.pid)
+    M.current_jobs[j.pid] = nil
 
     if not (code == 0) then
       vim.notify(string.format('[%d] exit %d', j.pid, code), vim.log.levels.ERROR)
@@ -67,7 +67,7 @@ function M.jobstart(opts)
       spinner.on_exit(nil, nil, j.pid)
     end
     if opts.populate_quickfix then
-      vim.cmd.doautocmd [[QuickFixCmdPost]]
+      vim.cmd.doautocmd('QuickFixCmdPost')
     end
   end)
 
@@ -92,11 +92,11 @@ function M.jobstart(opts)
   end)
 
   -- run vim.fn.expand() on all args
-  for i=1, #opts.args do
+  for i = 1, #opts.args do
     opts.args[i] = vim.fn.expandcmd(opts.args[i])
   end
 
-  local j = require ('plenary.job'):new(opts)
+  local j = require('plenary.job'):new(opts)
   j:start()
 
   return j
@@ -116,31 +116,44 @@ function M.make(extra_args)
     cwd = require('lspconfig.util').root_pattern('Makefile')(cwd)
   end
 
-  table.foreach(extra_args or {}, function(_, v) table.insert(args, v) end)
+  for _, v in pairs(extra_args or {}) do
+    table.insert(args, v)
+  end
+  -- table.foreach(extra_args or {}, function(_, v) end)
 
-  return M.jobstart {
+  return M.jobstart({
     command = command,
     args = args,
     cwd = cwd,
-  }
+  })
 end
 
 function M.sh(command_string)
-  return M.jobstart {
+  return M.jobstart({
     command = vim.env.SHELL,
-    args = {'-c', command_string},
+    args = { '-c', command_string },
 
     -- strip 'sh -c' from the title (used by progress reporting etc.)
     format_title = function(_, args)
-      return table.concat({unpack(args, 2, #args)}, ' ')
-    end
-  }
+      return table.concat({ unpack(args, 2, #args) }, ' ')
+    end,
+  })
 end
 
-
 function M.list()
-  require('telescope').load_extension('jobs')
   require('telescope._extensions').manager['jobs']['jobs']()
 end
 
+function M.setup()
+  require('telescope').load_extension('jobs')
+
+  vim.keymap.set(
+    'n',
+    '<leader>J',
+    require('internal.job').list,
+    { desc = 'list jobs managed by internal.job' }
+  )
+  vim.api.nvim_create_user_command('Jobs', M.list, { desc = 'list jobs managed by internal.job' })
+end
+
 return M