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 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, } })