summary refs log tree commit diff
path: root/.vim/lua/telescope/_extensions/jobs.lua
blob: 8caea2fa0713155bb51cd0d07845b9feccec91f3 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
local job = require('job')

local pickers = require('telescope.pickers')
local finders = require('telescope.finders')
local conf = require('telescope.config').values
local actions = require('telescope.actions')
local action_state = require('telescope.actions.state')
local entry_display = require('telescope.pickers.entry_display')

local function job_entry_maker(_)
  return function(j)
    local displayer = entry_display.create({
      separator = " ",
      items = {
        { width = 8 },
        { remaining = true },
      },
    })
    local make_display = function(e)
      return displayer({
        { e.value.pid, "TelescopeResultsNumber" },
        (e.value.command .. ' ' .. table.concat(e.value.args, ' ')),
      })
    end
    return {
      value = j,
      display = make_display,
      ordinal = tostring(j.pid),
    }
  end
end

local function list_jobs(opts)
  local jobs = {}
  for _, j in pairs(job.current_jobs) do
    table.insert(jobs, j)
  end
  if #jobs == 0 then return end

  opts = vim.tbl_extend("force", { prompt_title = 'Jobs' }, opts or {})
  pickers.new(opts, {
    finder = finders.new_table {
      results = jobs,
      entry_maker = job_entry_maker(opts),
    },
    sorter = conf.generic_sorter(opts),
    attach_mappings = function(prompt_bufnr, _)
      actions.select_default:replace(function()
        local entry = action_state.get_selected_entry()
        if not entry then return end
        entry.value:shutdown(1, 'SIGKILL')
        actions.close(prompt_bufnr)
      end)
      return true
    end,
  }):find()
end

return require('telescope').register_extension({
  exports = {
    jobs = list_jobs,
  }
})