summary refs log tree commit diff
path: root/.vim/lua/telescope/_extensions/jobs.lua
blob: be55a479e3cdaec9983d4cfdf53ea66b39829810 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
local job = require('internal.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
    vim.notify('no jobs')
    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
          actions.close(prompt_bufnr)
          vim.ui.select(
            { 'Terminate', 'Kill', 'Show', 'Cancel' },
            { prompt = 'Select an action:' },
            function(choice)
              if choice == 'Cancel' then
                return
              elseif choice == 'Show' then
                vim.fn.getqflist({ id = entry.value.pid })
              elseif choice == 'Kill' then
                entry.value.handle:kill('sigkill')
              elseif choice == 'Terminate' then
                entry.value.handle:kill('sigterm')
              end
            end
          )
        end)
        return true
      end,
    })
    :find()
end

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