]> 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:

widget module loads optimization
[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 -- wwidget.text = asyncshell.demand('wscript -Kiev', 5):read("*l") or "Error"
15
16 -- This makes things faster, but puts weight on sysload and is more cpu demanding.
17
18 local spawn = require('awful.util').spawn
19
20 asyncshell               = {}
21 asyncshell.request_table = {}
22 asyncshell.id_counter    = 0
23 asyncshell.folder        = "/tmp/asyncshell"
24 asyncshell.file_template = asyncshell.folder .. '/req'
25
26 -- Create a directory for asynchell response files
27 os.execute("mkdir -p " .. asyncshell.folder)
28
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
33 end
34
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
38 -- @return Request ID
39 function asyncshell.request(command, callback)
40    local id = next_id()
41    local tmpfname = asyncshell.file_template .. id
42    asyncshell.request_table[id] = {callback = callback}
43    local req =
44       string.format("sh -c '%s > %s; " ..
45                     'echo "asyncshell.deliver(%s)" | ' ..
46                     "awesome-client' 2> /dev/null",
47                     string.gsub(command, "'", "'\\''"), tmpfname,
48                     id, tmpfname)
49    spawn(req, false)
50    return id
51 end
52
53 -- Calls the remembered callback function on the output of the shell
54 -- command.
55 -- @param id Request ID
56 -- @param output The output file of the shell command to be delievered
57 function asyncshell.deliver(id)
58    if asyncshell.request_table[id] and
59       asyncshell.request_table[id].callback then
60       local output = io.open(asyncshell.file_template .. id, 'r')
61       asyncshell.request_table[id].callback(output)
62    end
63 end
64
65 -- Sends a synchronous request for an output of the command. Waits for
66 -- the output, but if the given timeout expires returns nil.
67 -- @param command Command to be executed and taken output from
68 -- @param timeout Maximum amount of time to wait for the result
69 -- @return File handler on success, nil otherwise
70 function asyncshell.demand(command, timeout)
71    local id = next_id()
72    local tmpfname = asyncshell.file_template .. id
73    local f = io.popen(string.format("(%s > %s; echo asyncshell_done) & " ..
74                                     "(sleep %s; echo asynchell_timeout)",
75                                     command, tmpfname, timeout))
76    local result = f:read("*line")
77    if result == "asyncshell_done" then
78       return io.open(tmpfname)
79    end
80 end
81
82 return asyncshell