Skip to content

Commit debd7c2

Browse files
committed
Add filter option to pick_process
1 parent 1dd02e8 commit debd7c2

File tree

2 files changed

+63
-7
lines changed

2 files changed

+63
-7
lines changed

doc/dap.txt

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,10 +1159,28 @@ UTILS API *dap-utils*
11591159

11601160
Lua module: dap.utils
11611161

1162-
pick_process() *dap.utils.pick_process*
1163-
Show a prompt to pick a PID from a list of processes.
1162+
pick_process({opts}) *dap.utils.pick_process*
1163+
Show a prompt to select a process pid
1164+
Requires `ps ah` on Linux/Mac and `tasklist /nh /fo csv` on windows.
11641165

1165-
This uses `ps ah` to retrieve the process list and won't work if `ps ah`
1166-
is not available. On windows this method uses `tasklist /nh /fo csv` command.
1166+
1167+
Parameters:
1168+
{opts} optional table with the following properties:
1169+
1170+
- filter string|fun:
1171+
A lua pattern or function to filter the processes.
1172+
If a function the parameter is a table with
1173+
{pid: integer, name: string}
1174+
and it must return a boolean.
1175+
Matches are included.
1176+
1177+
>lua
1178+
require("dap.utils").pick_process({ filter = "sway" })
1179+
<
1180+
>lua
1181+
require("dap.utils").pick_process({
1182+
filter = function(proc) return vim.endswith(proc.name, "sway") end
1183+
})
1184+
<
11671185

11681186
vim:ft=help

lua/dap/utils.lua

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ end
5050

5151

5252
--- Return running processes as a list with { pid, name } tables.
53+
---
54+
---@return {pid: integer, name: string}[]
5355
function M.get_processes()
5456
local is_windows = vim.fn.has('win32') == 1
5557
local separator = is_windows and ',' or ' \\+'
@@ -95,20 +97,56 @@ end
9597

9698

9799
--- Show a prompt to select a process pid
98-
function M.pick_process()
100+
--- Requires `ps ah` on Linux/Mac and `tasklist /nh /fo csv` on windows.
101+
--
102+
--- Takes an optional `opts` table with the following options:
103+
---
104+
--- - filter string|fun: A lua pattern or function to filter the processes.
105+
--- If a function the parameter is a table with
106+
--- {pid: integer, name: string}
107+
--- and it must return a boolean.
108+
--- Matches are included.
109+
---
110+
--- <pre>
111+
--- require("dap.utils").pick_process({ filter = "sway" })
112+
--- </pre>
113+
---
114+
--- <pre>
115+
--- require("dap.utils").pick_process({
116+
--- filter = function(proc) return vim.endswith(proc.name, "sway") end
117+
--- })
118+
--- </pre>
119+
---
120+
---@param opts? {filter: string|(fun(proc: {pid: integer, name: string}): boolean)}
121+
function M.pick_process(opts)
122+
opts = opts or {}
99123
local label_fn = function(proc)
100124
return string.format("id=%d name=%s", proc.pid, proc.name)
101125
end
126+
local procs = M.get_processes()
127+
if opts.filter then
128+
local filter
129+
if type(opts.filter) == "string" then
130+
filter = function(proc)
131+
return proc.name:find(opts.filter)
132+
end
133+
elseif type(opts.filter) == "function" then
134+
filter = function(proc)
135+
return opts.filter(proc)
136+
end
137+
else
138+
error("opts.filter must be a string or a function")
139+
end
140+
procs = vim.tbl_filter(filter, procs)
141+
end
102142
local co = coroutine.running()
103143
if co then
104144
return coroutine.create(function()
105-
local procs = M.get_processes()
106145
require('dap.ui').pick_one(procs, "Select process", label_fn, function(choice)
107146
coroutine.resume(co, choice and choice.pid or nil)
108147
end)
109148
end)
110149
else
111-
local procs = M.get_processes()
112150
local result = require('dap.ui').pick_one_sync(procs, "Select process", label_fn)
113151
return result and result.pid or nil
114152
end

0 commit comments

Comments
 (0)