]> git.madduck.net Git - etc/awesome.git/blob - asyncshell.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

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.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

0aafa17e7df9ffb2194c4ee76baa25575af65774
[etc/awesome.git] / asyncshell.lua
1
2 --[[
3                                                   
4      Licensed under GNU General Public License v2 
5       * (c) 2013, Alexander Yakushev              
6                                                   
7 --]]
8
9 -- Asynchronous io.popen for Awesome WM.
10 -- How to use...
11 -- ...asynchronously:
12 -- asyncshell.request('wscript -Kiev', function(f) wwidget.text = f:read("*l") end)
13 -- ...synchronously:
14 -- widget:set_text(asyncshell.demand('wscript -Kiev', 5):read("*l") or "Error")
15
16 local spawn = require('awful.util').spawn
17
18 asyncshell               = {}
19 asyncshell.request_table = {}
20 asyncshell.id_counter    = 0
21 asyncshell.folder        = "/tmp/asyncshell"
22 asyncshell.file_template = asyncshell.folder .. '/req'
23
24 -- Create a directory for asynchell response files
25 os.execute("mkdir -p " .. asyncshell.folder)
26
27 -- Returns next tag - unique identifier of the request
28 local function next_id()
29    asyncshell.id_counter = (asyncshell.id_counter + 1) % 100000
30    return asyncshell.id_counter
31 end
32
33 -- Sends an asynchronous request for an output of the shell command.
34 -- @param command Command to be executed and taken output from
35 -- @param callback Function to be called when the command finishes
36 -- @return Request ID
37 function asyncshell.request(command, callback)
38    local id = next_id()
39    local tmpfname = asyncshell.file_template .. id
40    asyncshell.request_table[id] = { callback = callback }
41    local req =
42       string.format("sh -c '%s > %s; " ..
43                     'echo "asyncshell.deliver(%s)" | ' ..
44                     "awesome-client' 2> /dev/null",
45                     string.gsub(command, "'", "'\\''"), tmpfname, id)
46    spawn(req, false)
47    return id
48 end
49
50 -- Calls the remembered callback function on the output of the shell
51 -- command.
52 -- @param id Request ID
53 -- @param output The output file of the shell command to be delievered
54 function asyncshell.deliver(id)
55    if asyncshell.request_table[id] and
56       asyncshell.request_table[id].callback then
57       local output = io.open(asyncshell.file_template .. id, 'r')
58       asyncshell.request_table[id].callback(output)
59    end
60 end
61
62 -- Sends a synchronous request for an output of the command. Waits for
63 -- the output, but if the given timeout expires returns nil.
64 -- @param command Command to be executed and taken output from
65 -- @param timeout Maximum amount of time to wait for the result
66 -- @return File handler on success, nil otherwise
67 function asyncshell.demand(command, timeout)
68    local id = next_id()
69    local tmpfname = asyncshell.file_template .. id
70    local f = io.popen(string.format("(%s > %s; echo asyncshell_done) & " ..
71                                     "(sleep %s; echo asynchell_timeout)",
72                                     command, tmpfname, timeout))
73    local result = f:read("*line")
74    if result == "asyncshell_done" then
75       return io.open(tmpfname)
76    end
77 end
78
79 return asyncshell