From 3ea9d58587754931989dae390ca119fb33aa634c Mon Sep 17 00:00:00 2001 From: Kevin Date: Wed, 16 Oct 2024 18:07:33 -0300 Subject: core: lua refactor --- lang/lua/astal/process.lua | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) (limited to 'lang/lua/astal/process.lua') diff --git a/lang/lua/astal/process.lua b/lang/lua/astal/process.lua index b8b7436..2886164 100644 --- a/lang/lua/astal/process.lua +++ b/lang/lua/astal/process.lua @@ -8,30 +8,29 @@ local M = {} ---@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) @@ -52,13 +51,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 -- cgit v1.2.3