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

Fixes for the silly case where a client's borders are bigger than its allotted space
[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 -- Initialize tables for module
18 asyncshell = { request_table = {}, id_counter = 0 }
19
20 -- Request counter
21 local function next_id()
22     asyncshell.id_counter = (asyncshell.id_counter + 1) % 10000
23     return asyncshell.id_counter
24 end
25
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
32         end
33         asyncshell.request_table[id] = nil
34     end
35 end
36
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)
42     local id = next_id()
43     asyncshell.request_table[id] = { callback = callback }
44
45     local formatted_command = string.gsub(command, '"','\"')
46
47     local req = string.format(
48         "echo \"asyncshell.deliver(%s, [[\\\"$(%s)\\\"]])\" | awesome-client &",
49         id, formatted_command
50     )
51
52     awful.util.spawn_with_shell(req)
53
54     if timeout then
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()
58     end
59 end
60
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)
68         asyncshell.clear(id)
69     end
70 end
71
72 return asyncshell