2023-08-01 11:06:35 -05:00
-- SPDX-License-Identifier: GPL-3.0-or-later
2023-06-25 17:18:50 -05:00
2023-06-28 16:42:07 -05:00
---@diagnostic disable: redefined-local
2023-09-09 20:52:52 -05:00
---Process management.
---
---This module provides utilities to spawn processes and capture their output.
2023-07-21 21:02:02 -05:00
---@class ProcessModule
local process_module = { }
2023-06-21 14:48:38 -05:00
2023-06-29 17:40:35 -05:00
---Spawn a process with an optional callback for its stdout, stderr, and exit information.
2023-06-28 16:42:07 -05:00
---
---`callback` has the following parameters:
2023-09-07 20:48:41 -05:00
---
2023-07-22 17:43:36 -05:00
--- - `stdout` - The process's stdout printed this line.
--- - `stderr` - The process's stderr printed this line.
--- - `exit_code` - The process exited with this code.
--- - `exit_msg` - The process exited with this message.
2023-06-21 14:48:38 -05:00
---@param command string|string[] The command as one whole string or a table of each of its arguments
2023-06-29 11:59:17 -05:00
---@param callback fun(stdout: string|nil, stderr: string|nil, exit_code: integer|nil, exit_msg: string|nil)? A callback to do something whenever the process's stdout or stderr print a line, or when the process exits.
2023-07-21 21:02:02 -05:00
function process_module . spawn ( command , callback )
2023-06-21 14:48:38 -05:00
---@type integer|nil
local callback_id = nil
if callback ~= nil then
2023-06-26 21:19:02 -05:00
---@param args Args
2023-06-21 14:48:38 -05:00
table.insert ( CallbackTable , function ( args )
2023-06-26 21:19:02 -05:00
local args = args.Spawn or { } -- don't know if the `or {}` is necessary
2023-06-21 14:48:38 -05:00
callback ( args.stdout , args.stderr , args.exit_code , args.exit_msg )
end )
callback_id = # CallbackTable
end
2023-06-26 21:19:02 -05:00
local command_arr = { }
if type ( command ) == " string " then
for i in string.gmatch ( command , " %S+ " ) do
table.insert ( command_arr , i )
2023-06-21 14:48:38 -05:00
end
2023-06-26 21:19:02 -05:00
else
command_arr = command
2023-06-21 14:48:38 -05:00
end
SendMsg ( {
Spawn = {
2023-06-26 21:19:02 -05:00
command = command_arr ,
2023-06-21 14:48:38 -05:00
callback_id = callback_id ,
} ,
} )
end
2023-06-29 17:40:35 -05:00
---Spawn a process only if it isn't already running, with an optional callback for its stdout, stderr, and exit information.
---
---`callback` has the following parameters:
2023-09-07 20:48:41 -05:00
---
2023-06-29 17:40:35 -05:00
--- - `stdout`: The process's stdout printed this line.
--- - `stderr`: The process's stderr printed this line.
--- - `exit_code`: The process exited with this code.
--- - `exit_msg`: The process exited with this message.
---
---`spawn_once` checks for the process using `pgrep`. If your system doesn't have `pgrep`, this won't work properly.
---@param command string|string[] The command as one whole string or a table of each of its arguments
---@param callback fun(stdout: string|nil, stderr: string|nil, exit_code: integer|nil, exit_msg: string|nil)? A callback to do something whenever the process's stdout or stderr print a line, or when the process exits.
2023-07-21 21:02:02 -05:00
function process_module . spawn_once ( command , callback )
2023-06-29 17:40:35 -05:00
local proc = " "
if type ( command ) == " string " then
proc = command : match ( " %S+ " )
else
proc = command [ 1 ]
end
---@type string
local procs = io.popen ( " pgrep -f " .. proc ) : read ( " *a " )
if procs : len ( ) ~= 0 then -- if process exists, return
return
end
2023-07-21 21:02:02 -05:00
process_module.spawn ( command , callback )
2023-06-29 17:40:35 -05:00
end
2023-07-21 21:02:02 -05:00
return process_module