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
5 * (c) 2013, Alexander Yakushev
9 -- Asynchronous io.popen for Awesome WM.
12 -- asyncshell.request('wscript -Kiev', function(f) wwidget.text = f:read("*l") end)
14 -- widget:set_text(asyncshell.demand('wscript -Kiev', 5):read("*l") or "Error")
16 -- This is more cpu demanding, but makes things faster.
18 local spawn = require('awful.util').spawn
21 asyncshell.request_table = {}
22 asyncshell.id_counter = 0
23 asyncshell.folder = "/tmp/asyncshell"
24 asyncshell.file_template = asyncshell.folder .. '/req'
26 -- Create a directory for asynchell response files
27 os.execute("mkdir -p " .. asyncshell.folder)
29 -- Returns next tag - unique identifier of the request
30 local function next_id()
31 asyncshell.id_counter = (asyncshell.id_counter + 1) % 100000
32 return asyncshell.id_counter
35 -- Sends an asynchronous request for an output of the shell command.
36 -- @param command Command to be executed and taken output from
37 -- @param callback Function to be called when the command finishes
39 function asyncshell.request(command, callback)
41 local tmpfname = asyncshell.file_template .. id
42 asyncshell.request_table[id] = { callback = callback }
44 string.format("sh -c '%s > %s; " ..
45 'echo "asyncshell.deliver(%s)" | ' ..
46 "awesome-client' 2> /dev/null",
47 string.gsub(command, "'", "'\\''"), tmpfname, id)
52 -- Calls the remembered callback function on the output of the shell
54 -- @param id Request ID
55 -- @param output The output file of the shell command to be delievered
56 function asyncshell.deliver(id)
57 if asyncshell.request_table[id] and
58 asyncshell.request_table[id].callback then
59 local output = io.open(asyncshell.file_template .. id, 'r')
60 asyncshell.request_table[id].callback(output)
64 -- Sends a synchronous request for an output of the command. Waits for
65 -- the output, but if the given timeout expires returns nil.
66 -- @param command Command to be executed and taken output from
67 -- @param timeout Maximum amount of time to wait for the result
68 -- @return File handler on success, nil otherwise
69 function asyncshell.demand(command, timeout)
71 local tmpfname = asyncshell.file_template .. id
72 local f = io.popen(string.format("(%s > %s; echo asyncshell_done) & " ..
73 "(sleep %s; echo asynchell_timeout)",
74 command, tmpfname, timeout))
75 local result = f:read("*line")
76 if result == "asyncshell_done" then
77 return io.open(tmpfname)