Add spawn_once function

This commit is contained in:
Seaotatop 2023-06-29 17:40:35 -05:00
parent a07ea96f80
commit d6504b0b82
2 changed files with 28 additions and 15 deletions

View file

@ -41,18 +41,4 @@ require("pinnacle").setup(function(pinnacle)
input.keybind({ "Ctrl" }, keys.KEY_3, function()
process.spawn("nautilus")
end)
input.keybind({ "Ctrl", "Alt" }, keys.w, function()
local win = client.get_window("focus")
win:set_size({ w = 500, h = 500 })
end)
input.keybind({ "Ctrl", "Alt" }, keys.e, function()
local windows = client.get_windows()
for k, v in pairs(windows) do
for k2, v2 in pairs(v) do
print(k2 .. ": " .. tostring(v2))
end
end
end)
end)

View file

@ -8,7 +8,7 @@
local process = {}
---Spawn a process with an optional callback for its stdout and stderr.
---Spawn a process with an optional callback for its stdout, stderr, and exit information.
---
---`callback` has the following parameters:
--- - `stdout`: The process's stdout printed this line.
@ -47,4 +47,31 @@ function process.spawn(command, callback)
})
end
---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:
--- - `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.
function process.spawn_once(command, callback)
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
process.spawn(command, callback)
end
return process