| 1 | -- wrapper around Awesome awful.spawn with fallback |
| 2 | -- Copyright (C) 2019 Nguyễn Gia Phong <[email protected]m> |
| 3 | -- |
| 4 | -- This file is part of Vicious. |
| 5 | -- |
| 6 | -- Vicious is free software: you can redistribute it and/or modify |
| 7 | -- it under the terms of the GNU General Public License as |
| 8 | -- published by the Free Software Foundation, either version 2 of the |
| 9 | -- License, or (at your option) any later version. |
| 10 | -- |
| 11 | -- Vicious is distributed in the hope that it will be useful, |
| 12 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | -- GNU General Public License for more details. |
| 15 | -- |
| 16 | -- You should have received a copy of the GNU General Public License |
| 17 | -- along with Vicious. If not, see <https://www.gnu.org/licenses/>. |
| 18 | |
| 19 | local status, awful = pcall(require, "awful") |
| 20 | if status then |
| 21 | local spawn = awful.spawn |
| 22 | function spawn.with_line_callback_with_shell(cmd, callbacks) |
| 23 | spawn.with_line_callback({ awful.util.shell, "-c", cmd }, callbacks) |
| 24 | end |
| 25 | return spawn |
| 26 | end |
| 27 | |
| 28 | local io = { popen = io.popen } |
| 29 | |
| 30 | -- In case the Awesome library is not available, |
| 31 | -- fall back to this synchronous, handicapped API. |
| 32 | local spawn = {} |
| 33 | |
| 34 | --- Spawn a program and capture its output line by line. |
| 35 | -- @tparam string cmd The command. |
| 36 | -- @tab callbacks Table containing callbacks that should be invoked on |
| 37 | -- various conditions. |
| 38 | -- @tparam[opt] function callbacks.stdout Function that is called with each |
| 39 | -- line of output on stdout, e.g. `stdout(line)`. |
| 40 | -- @tparam[opt] function callbacks.stderr Function that is supposed to be called |
| 41 | -- with each line of output on stderr, but due to limitation of io.popen, |
| 42 | -- stderr will be discarded (sorry!). |
| 43 | -- @tparam[opt] function callbacks.output_done Function to call when no more |
| 44 | -- output is produced. |
| 45 | -- @tparam[opt] function callbacks.exit Function to call when the spawned |
| 46 | -- process exits. This function gets the exit reason and code as its |
| 47 | -- arguments. |
| 48 | -- The reason can be "exit" or "signal". |
| 49 | -- For "exit", the second argument is the exit code. |
| 50 | -- For "signal", the second argument is the signal causing process |
| 51 | -- termination. |
| 52 | -- @treturn boolean|nil true if cmd terminated successfully, or nil otherwise |
| 53 | function spawn.with_line_callback_with_shell(cmd, callbacks) |
| 54 | local stdout_callback, stdout = callbacks.stdout, io.popen(cmd) |
| 55 | if stdout_callback then |
| 56 | for line in stdout:lines() do stdout_callback(line) end |
| 57 | end |
| 58 | if callbacks.output_done then callbacks.output_done() end |
| 59 | |
| 60 | local success, reason, code = stdout:close() -- this requires Lua 5.2 |
| 61 | if callbacks.exit then callbacks.exit(reason, code) end |
| 62 | return success |
| 63 | end |
| 64 | |
| 65 | --- Spawn a program and capture its output. |
| 66 | -- @tparam string cmd The command. |
| 67 | -- @tab callback Function with the following arguments |
| 68 | -- @tparam string callback.stdout Output on stdout. |
| 69 | -- @tparam string callback.stderr Output on stderr, |
| 70 | -- which will be an empty due to limitation of io.popen. |
| 71 | -- @tparam string callback.exitreason Exit reason ("exit" or "signal"). |
| 72 | -- @tparam integer callback.exitcode Exit code (exit code or signal number, |
| 73 | -- depending on "exitreason"). |
| 74 | -- @treturn boolean|nil true if cmd terminated successfully, or nil otherwise |
| 75 | function spawn.easy_async_with_shell(cmd, callback) |
| 76 | local out_stream = io.popen(cmd) |
| 77 | local stdout = out_stream:read("*all") |
| 78 | local success, reason, code = out_stream:close() -- requiring Lua 5.2 |
| 79 | callback(stdout, "", reason, code) |
| 80 | return success |
| 81 | end |
| 82 | |
| 83 | -- Since io.popen always use a shell |
| 84 | spawn.easy_async = spawn.easy_async_with_shell |
| 85 | spawn.with_line_callback = spawn.with_line_callback_with_shell |
| 86 | |
| 87 | return spawn |
| 88 | -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 |