diff options
author | Aylur <[email protected]> | 2024-10-18 19:58:46 +0200 |
---|---|---|
committer | GitHub <[email protected]> | 2024-10-18 19:58:46 +0200 |
commit | f763c442f7dd146c6f20376c5f50e6ca94b5e0d6 (patch) | |
tree | 13ac4f50a627b0957d5d64cd393e1aa956b9f12a /lang/lua/astal/process.lua | |
parent | 0e52e1bf37ffe8689397fc728a3979163cad7f03 (diff) | |
parent | 61f7c53529722f6754ebca567149125710a864c6 (diff) |
Merge pull request #48 from tokyob0t/lua-refactor
core: lua refactor
Diffstat (limited to 'lang/lua/astal/process.lua')
-rw-r--r-- | lang/lua/astal/process.lua | 31 |
1 files changed, 14 insertions, 17 deletions
diff --git a/lang/lua/astal/process.lua b/lang/lua/astal/process.lua index 800e83a..0031f4c 100644 --- a/lang/lua/astal/process.lua +++ b/lang/lua/astal/process.lua @@ -10,30 +10,29 @@ M.Process = Astal.Process ---@param on_stderr? fun(err: string): nil ---@return { kill: function } | nil proc function M.subprocess(commandline, on_stdout, on_stderr) - if on_stdout == nil then - on_stdout = function(out) - io.stdout:write(tostring(out) .. "\n") - end + on_stdout = on_stdout or function(out) + io.stdout:write(tostring(out) .. "\n") end - if on_stderr == nil then - on_stderr = function(err) - io.stderr:write(tostring(err) .. "\n") - end + on_stderr = on_stderr or function(err) + io.stderr:write(tostring(err) .. "\n") end + local proc, err + if type(commandline) == "table" then proc, err = Astal.Process.subprocessv(commandline) else proc, err = Astal.Process.subprocess(commandline) end + if err ~= nil then err(err) return nil end - proc.on_stdout = function(_, stdoud) - on_stdout(stdoud) + proc.on_stdout = function(_, stdout) + on_stdout(stdout) end proc.on_stderr = function(_, stderr) on_stderr(stderr) @@ -54,13 +53,11 @@ end ---@param commandline string | string[] ---@param callback? fun(out: string, err: string): nil function M.exec_async(commandline, callback) - if callback == nil then - callback = function(out, err) - if err ~= nil then - io.stdout:write(tostring(out) .. "\n") - else - io.stderr:write(tostring(err) .. "\n") - end + callback = callback or function(out, err) + if err ~= nil then + io.stdout:write(tostring(out) .. "\n") + else + io.stderr:write(tostring(err) .. "\n") end end |