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

asyncshell: minor fix to spawn with shell invocation
[etc/awesome.git] / asyncshell.lua
1
2 --[[
3                                                   
4      Licensed under GNU General Public License v2 
5       * (c) 2015, worron                          
6       * (c) 2013, Alexander Yakushev              
7                                                   
8 --]]
9
10 -- Asynchronous io.popen for Awesome WM.
11 -- How to use:
12 -- asyncshell.request('wscript -Kiev', function(output) wwidget.text = output end)
13
14 -- Grab environment
15 local awful = require('awful')
16
17 -- Avoid discrepancies across multiple shells
18 awful.util.shell = '/bin/sh'
19
20 -- Initialize tables for module
21 asyncshell = { request_table = {}, id_counter = 0 }
22
23 -- Request counter
24 local function next_id()
25     asyncshell.id_counter = (asyncshell.id_counter + 1) % 10000
26     return asyncshell.id_counter
27 end
28
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
35         end
36         asyncshell.request_table[id] = nil
37     end
38 end
39
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)
45     local id = next_id()
46     asyncshell.request_table[id] = { callback = callback }
47
48     local formatted_command = string.gsub(command, '"','\"')
49
50     local req = string.format(
51         "echo \"asyncshell.deliver(%s, [[\\\"$(%s)\\\"]])\" | awesome-client &",
52         id, formatted_command
53     )
54
55     if type(awful.spawn) == 'table' and awful.spawn.with_shell then
56         awful.spawn.with_shell(req)
57     else
58         awful.util.spawn_with_shell(req)
59     end
60
61     if timeout then
62         asyncshell.request_table[id].timer = timer({ timeout = timeout })
63         asyncshell.request_table[id].timer:connect_signal("timeout", function() asyncshell.clear(id) end)
64         asyncshell.request_table[id].timer:start()
65     end
66 end
67
68 -- Calls the remembered callback function on the output of the shell command
69 -- @param id Request ID
70 -- @param output Shell command output to be delievered
71 function asyncshell.deliver(id, output)
72     local output = string.sub(output, 2, -2)
73     if asyncshell.request_table[id] then
74         asyncshell.request_table[id].callback(output)
75         asyncshell.clear(id)
76     end
77 end
78
79 return asyncshell