2014-08-05 12:59:19 +02:00
|
|
|
|
|
|
|
--[[
|
|
|
|
|
|
|
|
Licensed under GNU General Public License v2
|
2015-08-05 12:28:54 +02:00
|
|
|
* (c) 2015, worron
|
2014-08-05 12:59:19 +02:00
|
|
|
* (c) 2013, Alexander Yakushev
|
|
|
|
|
|
|
|
--]]
|
|
|
|
|
|
|
|
-- Asynchronous io.popen for Awesome WM.
|
2015-08-05 12:28:54 +02:00
|
|
|
-- How to use:
|
|
|
|
-- asyncshell.request('wscript -Kiev', function(output) wwidget.text = output end)
|
2014-08-05 12:59:19 +02:00
|
|
|
|
2015-08-05 12:28:54 +02:00
|
|
|
-- Grab environment
|
|
|
|
local awful = require('awful')
|
2014-08-05 12:59:19 +02:00
|
|
|
|
2015-08-05 12:28:54 +02:00
|
|
|
-- Initialize tables for module
|
|
|
|
asyncshell = { request_table = {}, id_counter = 0 }
|
2014-08-05 12:59:19 +02:00
|
|
|
|
2015-08-05 12:28:54 +02:00
|
|
|
-- Request counter
|
2014-08-05 12:59:19 +02:00
|
|
|
local function next_id()
|
2015-08-05 12:28:54 +02:00
|
|
|
asyncshell.id_counter = (asyncshell.id_counter + 1) % 10000
|
|
|
|
return asyncshell.id_counter
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Remove given request
|
|
|
|
function asyncshell.clear(id)
|
|
|
|
if asyncshell.request_table[id] then
|
|
|
|
if asyncshell.request_table[id].timer then
|
|
|
|
asyncshell.request_table[id].timer:stop()
|
|
|
|
asyncshell.request_table[id].timer = nil
|
|
|
|
end
|
|
|
|
asyncshell.request_table[id] = nil
|
|
|
|
end
|
2014-08-05 12:59:19 +02:00
|
|
|
end
|
|
|
|
|
2015-08-05 12:28:54 +02:00
|
|
|
-- Sends an asynchronous request for an output of the shell command
|
2014-08-05 12:59:19 +02:00
|
|
|
-- @param command Command to be executed and taken output from
|
|
|
|
-- @param callback Function to be called when the command finishes
|
2015-08-05 12:28:54 +02:00
|
|
|
-- @param timeout Maximum amount of time to wait for the result (optional)
|
|
|
|
function asyncshell.request(command, callback, timeout)
|
|
|
|
local id = next_id()
|
|
|
|
asyncshell.request_table[id] = { callback = callback }
|
2014-08-05 12:59:19 +02:00
|
|
|
|
2015-08-05 12:28:54 +02:00
|
|
|
local formatted_command = string.gsub(command, '"','\"')
|
|
|
|
|
|
|
|
local req = string.format(
|
|
|
|
"echo \"asyncshell.deliver(%s, [[\\\"$(%s)\\\"]])\" | awesome-client &",
|
|
|
|
id, formatted_command
|
|
|
|
)
|
|
|
|
|
|
|
|
awful.util.spawn_with_shell(req)
|
|
|
|
|
|
|
|
if timeout then
|
|
|
|
asyncshell.request_table[id].timer = timer({ timeout = timeout })
|
|
|
|
asyncshell.request_table[id].timer:connect_signal("timeout", function() asyncshell.clear(id) end)
|
|
|
|
asyncshell.request_table[id].timer:start()
|
|
|
|
end
|
2014-08-05 12:59:19 +02:00
|
|
|
end
|
|
|
|
|
2015-08-05 12:28:54 +02:00
|
|
|
-- Calls the remembered callback function on the output of the shell command
|
|
|
|
-- @param id Request ID
|
|
|
|
-- @param output Shell command output to be delievered
|
|
|
|
function asyncshell.deliver(id, output)
|
|
|
|
local output = string.sub(output, 2, -2)
|
|
|
|
if asyncshell.request_table[id] then
|
|
|
|
asyncshell.request_table[id].callback(output)
|
|
|
|
asyncshell.clear(id)
|
|
|
|
end
|
2014-08-05 12:59:19 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return asyncshell
|