summaryrefslogtreecommitdiff
path: root/lua/astal/process.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lua/astal/process.lua')
-rw-r--r--lua/astal/process.lua94
1 files changed, 0 insertions, 94 deletions
diff --git a/lua/astal/process.lua b/lua/astal/process.lua
deleted file mode 100644
index 3d10f8b..0000000
--- a/lua/astal/process.lua
+++ /dev/null
@@ -1,94 +0,0 @@
-local lgi = require("lgi")
-local Astal = lgi.require("Astal", "0.1")
-
-local M = {}
-
-local defualt_proc_args = function(on_stdout, on_stderr)
- if on_stdout == nil then
- on_stdout = function(out)
- io.stdout:write(tostring(out) .. "\n")
- return tostring(out)
- end
- end
-
- if on_stderr == nil then
- on_stderr = function(err)
- io.stderr:write(tostring(err) .. "\n")
- return tostring(err)
- end
- end
-
- return on_stdout, on_stderr
-end
-
----@param commandline string | string[]
----@param on_stdout? fun(out: string): nil
----@param on_stderr? fun(err: string): nil
----@return { kill: function } | nil proc
-function M.subprocess(commandline, on_stdout, on_stderr)
- local out, err = defualt_proc_args(on_stdout, on_stderr)
- local proc, fail
- if type(commandline) == "table" then
- proc, fail = Astal.Process.subprocessv(commandline)
- else
- proc, fail = Astal.Process.subprocess(commandline)
- end
- if fail ~= nil then
- err(fail)
- return nil
- end
- proc.on_stdout = function(_, str)
- out(str)
- end
- proc.on_stderr = function(_, str)
- err(str)
- end
- return proc
-end
-
----@generic T
----@param commandline string | string[]
----@param on_stdout? fun(out: string): T
----@param on_stderr? fun(err: string): T
----@return T
-function M.exec(commandline, on_stdout, on_stderr)
- local out, err = defualt_proc_args(on_stdout, on_stderr)
- local stdout, stderr
- if type(commandline) == "table" then
- stdout, stderr = Astal.Process.execv(commandline)
- else
- stdout, stderr = Astal.Process.exec(commandline)
- end
- if stderr then
- return err(stderr)
- end
- return out(stdout)
-end
-
----@param commandline string | string[]
----@param on_stdout? fun(out: string): nil
----@param on_stderr? fun(err: string): nil
-function M.exec_async(commandline, on_stdout, on_stderr)
- local out, err = defualt_proc_args(on_stdout, on_stderr)
- if type(commandline) == "table" then
- Astal.Process.exec_asyncv(commandline, function(_, res)
- local stdout, fail = Astal.exec_asyncv_finish(res)
- if fail ~= nil then
- err(fail)
- else
- out(stdout)
- end
- end)
- else
- Astal.Process.exec_async(commandline, function(_, res)
- local stdout, fail = Astal.exec_finish(res)
- if fail ~= nil then
- err(fail)
- else
- out(stdout)
- end
- end)
- end
-end
-
-return M