-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathasync.lua
More file actions
53 lines (46 loc) · 1.37 KB
/
Copy pathasync.lua
File metadata and controls
53 lines (46 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
--[[
Licensed under GNU General Public License v2
20?? Alexander Yakushev
2014 Yauheni Kirylau
--]]
-- Asynchronous io.popen for Awesome WM.
-- How to use:
-- async.execute('echo hello!', function(str) widget.text = str end)
local awful = require('awful')
-- !!! it should be GLOBAL
async = {}
async.request_table = {}
async.id_counter = 0
-- Returns next tag - unique identifier of the request
local function next_id()
-- @TODO: add lock?
async.id_counter = (async.id_counter + 1) % 100000
return string.format("%d", async.id_counter)
end
-- Sends an asynchronous request for an output of the shell command.
-- @param command Command to be executed and taken output from
-- @param callback Function to be called when the command finishes
-- @return Request ID
function async.execute(command, callback)
local id = next_id()
async.request_table[id] = {
callback = callback,
table = {}}
awful.util.spawn_with_shell(string.format(
[[
echo async.pipe_multiline_done\(\"%q\", \""$(%s | %s)"\"\) | awesome-client;
]],
--"]]-- syntax highlighter fix
id,
command:gsub('"','\"'),
[[awk 1 ORS='\\\\n' | sed 's/"/\\"/g']]
))
return id
end
function async.pipe_multiline_done(id, str)
if not async.request_table[id] then return end
str = str:gsub('\\n','\n')
async.request_table[id].callback(str)
async.request_table[id] = nil
end
return async