awful.spawn.with_line_callback: Use an arguments table
Having many arguments can easily get confusing and hard to understand. This commit uses a table instead so that we have names that identify what each callback does. Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
parent
2f5ade49c2
commit
469433e10a
|
@ -91,20 +91,24 @@ end
|
||||||
|
|
||||||
--- Spawn a program and asynchronously and capture its output line by line.
|
--- Spawn a program and asynchronously and capture its output line by line.
|
||||||
-- @tparam string|table cmd The command.
|
-- @tparam string|table cmd The command.
|
||||||
-- @tparam[opt] function stdout_callback Function that is called with each line of
|
-- @tab callbacks Table containing callbacks that should be
|
||||||
-- output on stdout, e.g. `stdout_callback(line)`.
|
-- invoked on various conditions.
|
||||||
-- @tparam[opt] function stderr_callback Function that is called with each line of
|
-- @tparam[opt] function callbacks.stdout Function that is called with each line of
|
||||||
-- output on stderr, e.g. `stderr_callback(line)`.
|
-- output on stdout, e.g. `stdout(line)`.
|
||||||
-- @tparam[opt] function done_callback Function to call when no more output is
|
-- @tparam[opt] function callbacks.stderr Function that is called with each line of
|
||||||
-- produced.
|
-- output on stderr, e.g. `stderr(line)`.
|
||||||
-- @tparam[opt] function exit_callback Function to call when the spawned process
|
-- @tparam[opt] function callbacks.output_done Function to call when no more output
|
||||||
|
-- is produced.
|
||||||
|
-- @tparam[opt] function callbacks.exit Function to call when the spawned process
|
||||||
-- exits. This function gets the exit reason and code as its argument. The
|
-- exits. This function gets the exit reason and code as its argument. The
|
||||||
-- reason can be "exit" or "signal". For "exit", the second argument is the exit
|
-- reason can be "exit" or "signal". For "exit", the second argument is the exit
|
||||||
-- code. For "signal", the second argument is the signal causing process
|
-- code. For "signal", the second argument is the signal causing process
|
||||||
-- termination.
|
-- termination.
|
||||||
-- @treturn[1] Integer the PID of the forked process.
|
-- @treturn[1] Integer the PID of the forked process.
|
||||||
-- @treturn[2] string Error message.
|
-- @treturn[2] string Error message.
|
||||||
function spawn.with_line_callback(cmd, stdout_callback, stderr_callback, done_callback, exit_callback)
|
function spawn.with_line_callback(cmd, callbacks)
|
||||||
|
local stdout_callback, stderr_callback, done_callback, exit_callback =
|
||||||
|
callbacks.stdout, callbacks.stderr, callbacks.output_done, callbacks.exit
|
||||||
local have_stdout, have_stderr = stdout_callback ~= nil, stderr_callback ~= nil
|
local have_stdout, have_stderr = stdout_callback ~= nil, stderr_callback ~= nil
|
||||||
local pid, sn_id, stdin, stdout, stderr = capi.awesome.spawn(cmd,
|
local pid, sn_id, stdin, stdout, stderr = capi.awesome.spawn(cmd,
|
||||||
false, false, have_stdout, have_stderr, exit_callback)
|
false, false, have_stdout, have_stderr, exit_callback)
|
||||||
|
|
|
@ -9,43 +9,51 @@ local steps = {
|
||||||
function(count)
|
function(count)
|
||||||
if count == 1 then
|
if count == 1 then
|
||||||
local steps_yay = 0
|
local steps_yay = 0
|
||||||
spawn.with_line_callback("echo yay", function(line)
|
spawn.with_line_callback("echo yay", {
|
||||||
assert(line == "yay", "line == '" .. tostring(line) .. "'")
|
stdout = function(line)
|
||||||
assert(steps_yay == 0)
|
assert(line == "yay", "line == '" .. tostring(line) .. "'")
|
||||||
steps_yay = steps_yay + 1
|
assert(steps_yay == 0)
|
||||||
end, nil, function()
|
steps_yay = steps_yay + 1
|
||||||
assert(steps_yay == 1)
|
end,
|
||||||
steps_yay = steps_yay + 1
|
output_done = function()
|
||||||
spawns_done = spawns_done + 1
|
assert(steps_yay == 1)
|
||||||
end, function(reason, code)
|
steps_yay = steps_yay + 1
|
||||||
assert(reason == "exit")
|
spawns_done = spawns_done + 1
|
||||||
assert(exit_yay == nil)
|
end,
|
||||||
assert(code == 0)
|
exit = function(reason, code)
|
||||||
exit_yay = code
|
assert(reason == "exit")
|
||||||
end)
|
assert(exit_yay == nil)
|
||||||
|
assert(code == 0)
|
||||||
|
exit_yay = code
|
||||||
|
end
|
||||||
|
})
|
||||||
|
|
||||||
local steps_count = 0
|
local steps_count = 0
|
||||||
local err_count = 0
|
local err_count = 0
|
||||||
spawn.with_line_callback({ "sh", "-c", "printf line1\\\\nline2\\\\nline3 ; echo err >&2 ; exit 42" },
|
spawn.with_line_callback({ "sh", "-c", "printf line1\\\\nline2\\\\nline3 ; echo err >&2 ; exit 42" }, {
|
||||||
function(line)
|
stdout = function(line)
|
||||||
assert(steps_count < 3)
|
assert(steps_count < 3)
|
||||||
steps_count = steps_count + 1
|
steps_count = steps_count + 1
|
||||||
assert(line == "line" .. steps_count, "line == '" .. tostring(line) .. "'")
|
assert(line == "line" .. steps_count, "line == '" .. tostring(line) .. "'")
|
||||||
end, function(line)
|
end,
|
||||||
assert(err_count == 0)
|
stderr = function(line)
|
||||||
err_count = err_count + 1
|
assert(err_count == 0)
|
||||||
assert(line == "err", "line == '" .. tostring(line) .. "'")
|
err_count = err_count + 1
|
||||||
end, function()
|
assert(line == "err", "line == '" .. tostring(line) .. "'")
|
||||||
assert(steps_count == 3)
|
end,
|
||||||
assert(err_count == 1)
|
output_done = function()
|
||||||
steps_count = steps_count + 1
|
assert(steps_count == 3)
|
||||||
spawns_done = spawns_done + 1
|
assert(err_count == 1)
|
||||||
end, function(reason, code)
|
steps_count = steps_count + 1
|
||||||
assert(reason == "exit")
|
spawns_done = spawns_done + 1
|
||||||
assert(exit_snd == nil)
|
end,
|
||||||
assert(code == 42)
|
exit = function(reason, code)
|
||||||
exit_snd = code
|
assert(reason == "exit")
|
||||||
end)
|
assert(exit_snd == nil)
|
||||||
|
assert(code == 42)
|
||||||
|
exit_snd = code
|
||||||
|
end
|
||||||
|
})
|
||||||
end
|
end
|
||||||
if spawns_done == 2 then
|
if spawns_done == 2 then
|
||||||
assert(exit_yay == 0)
|
assert(exit_yay == 0)
|
||||||
|
|
Loading…
Reference in New Issue