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-10-14 20:57:03 +0200
committerRobert Günzler <r@gnzler.io>2022-10-14 21:01:15 +0200
commit2d2009413f8be624a7cca95cf702e32d9315cbef (patch)
treef024c1b5b27b886530e86221593e3cfdc3672d7e /.vim/lua/internal/job.lua
parent6d7e524d3d7a71da2cea61b335a59e9544e2a6f7 (diff)
vim: move custom modules under internal/
Diffstat (limited to '.vim/lua/internal/job.lua')
-rw-r--r--.vim/lua/internal/job.lua140
1 files changed, 140 insertions, 0 deletions
diff --git a/.vim/lua/internal/job.lua b/.vim/lua/internal/job.lua
new file mode 100644
index 0000000..f9f7bf3
--- /dev/null
+++ b/.vim/lua/internal/job.lua
@@ -0,0 +1,140 @@
+local Job = require ('plenary.job')
+local spinner_ok, spinner = pcall(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 {}
+
+  -- 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
+  else
+    title = opts.command
+  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')
+    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, _)
+    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)
+    end
+  end)
+
+  opts.on_stdout = vim.schedule_wrap(function(error, data, j)
+    if error then
+      vim.notify('error: ' .. error)
+      return
+    end -- handle error?
+
+    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?
+
+    if opts.populate_quickfix and data then
+      vim.fn.setqflist({}, 'a', {
+        title = title,
+        lines = {strip_ansi(data)},
+        efm = '%m',
+        id = j.pid,
+      })
+      vim.cmd([[doautocmd QuickFixCmdPost]])
+    end
+  end)
+
+  -- run vim.fn.expand() on all args
+  for i=1, #opts.args do
+    opts.args[i] = vim.fn.expandcmd(opts.args[i])
+  end
+
+  local j = Job:new(opts)
+  j:start()
+
+  return j
+end
+
+function M.make(extra_args)
+  local makeprg = vim.fn.expandcmd(vim.opt.makeprg:get())
+  -- split makeprg into command + args
+  local args = vim.split(makeprg, ' ')
+  local command = args[1]
+  table.remove(args, 1)
+
+  local cwd = vim.fn.expand('%:p:h')
+
+  if not (string.match(makeprg, 'make') == nil) then
+    -- detect makefile
+    cwd = require('lspconfig.util').root_pattern('Makefile')(cwd)
+  end
+
+  table.foreach(extra_args or {}, function(_, v) table.insert(args, v) end)
+
+  return M.jobstart {
+    command = command,
+    args = args,
+    cwd = cwd,
+  }
+end
+
+function M.sh(command_string)
+  return M.jobstart {
+    command = vim.env.SHELL,
+    args = {'-c', 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(' ')
+    end
+  }
+end
+
+return M