summary refs log tree commit diff
path: root/.vim/lua/internal/job.lua
diff options
context:
space:
mode:
Diffstat (limited to '.vim/lua/internal/job.lua')
-rw-r--r--.vim/lua/internal/job.lua106
1 files changed, 64 insertions, 42 deletions
diff --git a/.vim/lua/internal/job.lua b/.vim/lua/internal/job.lua
index 42a548b..2e97f16 100644
--- a/.vim/lua/internal/job.lua
+++ b/.vim/lua/internal/job.lua
@@ -28,11 +28,28 @@ end
 function M.jobstart(opts)
   opts = opts or {}
 
-  local spinner_ok, spinner = pcall(require, 'spinner.core')
+  opts = vim.tbl_extend('keep', opts or {}, {
+    enable_spinner = vim.g.job_enable_spinner,
+    populate_quickfix = vim.g.job_populate_quickfix,
+    attach = vim.g.job_attach,
+  })
+
+  -- vim.notify_once(vim.inspect(opts), vim.log.levels.DEBUG)
 
-  -- default options
-  opts.enable_spinner = (opts.enable_spinner or true) and spinner_ok
-  opts.populate_quickfix = opts.populate_quickfix or 'onerror'
+  if opts.attach then
+    return vim.schedule_wrap(function()
+      vim.cmd.terminal({ args = { opts.command, unpack(opts.args) } })
+    end)()
+  end
+
+  local spinner = {}
+  if opts.enable_spinner then
+    local ok = false
+    ok, spinner = pcall(require, 'spinner.core')
+    if not ok then
+      opts.enable_spinner = false
+    end
+  end
 
   if opts.format_title == nil then
     opts.format_title = table.concat({ opts.command, unpack(opts.args) }, ' ')
@@ -102,58 +119,63 @@ function M.jobstart(opts)
   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)
+function M.sh(command, opts)
+  return M.jobstart(vim.tbl_extend('keep', opts or {}, {
+    command = vim.env.SHELL,
+    args = { '-c', command },
+    -- strip 'sh -c' from the title (used by progress reporting etc.)
+    format_title = function(_, args)
+      return table.concat({ unpack(args, 2, #args) }, ' ')
+    end,
+  }))
+end
 
+function M.make(opts)
+  opts = opts or {}
+
+  local makeprg = vim.fn.expandcmd(vim.opt.makeprg:get())
   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
-
-  for _, v in pairs(extra_args or {}) do
-    table.insert(args, v)
+    opts.cwd = require('lspconfig.util').root_pattern('Makefile')(cwd)
   end
-  -- table.foreach(extra_args or {}, function(_, 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 table.concat({ unpack(args, 2, #args) }, ' ')
-    end,
-  })
+  return M.sh(makeprg, opts)
 end
 
 function M.list()
   require('telescope._extensions').manager['jobs']['jobs']()
 end
 
-function M.setup()
-  require('telescope').load_extension('jobs')
+function M.setup(opts)
+  opts = vim.tbl_extend('keep', opts, { telescope = true, mappings = true })
+
+  -- defaults
+  vim.g.job_enable_spinner = true
+  vim.g.job_populate_quickfix = 'onerror'
+  vim.g.job_attach = false
+
+  if opts.telescope then
+    require('telescope').load_extension('jobs')
+    vim.api.nvim_create_user_command('Jobs', M.list, { desc = 'list jobs managed by internal.job' })
+    if opts.mappings then
+      vim.keymap.set('n', '<leader>jl', M.list, { desc = 'list jobs managed by internal.job' })
+    end
+  end
+
+  vim.api.nvim_create_user_command('Make', function(a)
+    require('internal.job').make(a.fargs)
+  end, { nargs = '*', desc = 'run make target or &makeprg with internal.job' })
 
-  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' })
+  vim.api.nvim_create_user_command('Sh', function(a)
+    require('internal.job').sh(table.concat(a.fargs, ' '))
+  end, { nargs = '*', desc = 'run shell command with internal.job' })
+
+  if opts.mappings then
+    vim.keymap.set('n', '<leader>jm', M.make, { desc = 'run &makeprg with internal.job' })
+    vim.keymap.set('n', '<leader>js', ':Sh ', { desc = 'run shell commands with internal.job' })
+  end
 end
 
 return M