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 -- Initialize tables for module
18 asyncshell = { request_table = {}, id_counter = 0 }
21 local function next_id()
22 asyncshell.id_counter = (asyncshell.id_counter + 1) % 10000
23 return asyncshell.id_counter
26 -- Remove given request
27 function asyncshell.clear(id)
28 if asyncshell.request_table[id] then
29 if asyncshell.request_table[id].timer then
30 asyncshell.request_table[id].timer:stop()
31 asyncshell.request_table[id].timer = nil
33 asyncshell.request_table[id] = nil
37 -- Sends an asynchronous request for an output of the shell command
38 -- @param command Command to be executed and taken output from
39 -- @param callback Function to be called when the command finishes
40 -- @param timeout Maximum amount of time to wait for the result (optional)
41 function asyncshell.request(command, callback, timeout)
43 asyncshell.request_table[id] = { callback = callback }
45 local formatted_command = string.gsub(command, '"','\"')
47 local req = string.format(
48 "echo \"asyncshell.deliver(%s, [[\\\"$(%s)\\\"]])\" | awesome-client &",
52 awful.util.spawn_with_shell(req)
55 asyncshell.request_table[id].timer = timer({ timeout = timeout })
56 asyncshell.request_table[id].timer:connect_signal("timeout", function() asyncshell.clear(id) end)
57 asyncshell.request_table[id].timer:start()
61 -- Calls the remembered callback function on the output of the shell command
62 -- @param id Request ID
63 -- @param output Shell command output to be delievered
64 function asyncshell.deliver(id, output)
65 local output = string.sub(output, 2, -2)
66 if asyncshell.request_table[id] then
67 asyncshell.request_table[id].callback(output)