All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
4 Licensed under GNU General Public License v2
6 * (c) 2013, Alexander Yakushev
10 -- Asynchronous io.popen for Awesome WM.
12 -- asyncshell.request('wscript -Kiev', function(output) wwidget.text = output end)
15 local awful = require('awful')
17 -- Avoid discrepancies across multiple shells
18 awful.util.shell = '/bin/sh'
20 -- Initialize tables for module
21 asyncshell = { request_table = {}, id_counter = 0 }
24 local function next_id()
25 asyncshell.id_counter = (asyncshell.id_counter + 1) % 10000
26 return asyncshell.id_counter
29 -- Remove given request
30 function asyncshell.clear(id)
31 if asyncshell.request_table[id] then
32 if asyncshell.request_table[id].timer then
33 asyncshell.request_table[id].timer:stop()
34 asyncshell.request_table[id].timer = nil
36 asyncshell.request_table[id] = nil
40 -- Sends an asynchronous request for an output of the shell command
41 -- @param command Command to be executed and taken output from
42 -- @param callback Function to be called when the command finishes
43 -- @param timeout Maximum amount of time to wait for the result (optional)
44 function asyncshell.request(command, callback, timeout)
46 asyncshell.request_table[id] = { callback = callback }
48 local formatted_command = string.gsub(command, '"','\"')
50 local req = string.format(
51 "echo \"asyncshell.deliver(%s, [[\\\"$(%s)\\\"]])\" | awesome-client &",
55 awful.util.spawn_with_shell(req)
58 asyncshell.request_table[id].timer = timer({ timeout = timeout })
59 asyncshell.request_table[id].timer:connect_signal("timeout", function() asyncshell.clear(id) end)
60 asyncshell.request_table[id].timer:start()
64 -- Calls the remembered callback function on the output of the shell command
65 -- @param id Request ID
66 -- @param output Shell command output to be delievered
67 function asyncshell.deliver(id, output)
68 local output = string.sub(output, 2, -2)
69 if asyncshell.request_table[id] then
70 asyncshell.request_table[id].callback(output)