summary refs log tree commit diff
path: root/.vim/lua/job.lua
blob: a3591e5272cc0f4fa7cfbbf686602315e8d6184e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
local M = {}

function M.jobstart(cmd)
  local output = {}

  local function on_event(job_id, data, event)
    if event == 'stdout' or event == 'stderr' then
      if data then
        vim.list_extend(output, data)
      end
    end
    if event == 'exit' then
      vim.fn.setqflist({}, ' ', {
        title = cmd,
        lines = output,
      })
      vim.api.nvim_command('doautocmd QuickFixCmdPost')
    end
  end

  local job_id = require('spinner.job').jobstart(cmd, {
    on_stdout = on_event,
    on_stderr = on_event,
    on_exit = on_event,
    stdout_buffered = true,
    stderr_buffered = true,
  })
  return job_id
end

function M.make(args)
  local args = args or {}
  local makeprg = vim.opt.makeprg:get()
  local cmd = vim.split(vim.fn.expandcmd(makeprg), ' ')
  table.foreach(args, function(_, v) table.insert(cmd, v) end)
  return M.jobstart(cmd)
end

function M.sh(cmd)
  local cmd = {vim.env.SHELL, '-c', cmd}
  return M.jobstart(cmd)
end

return M