mirror of https://github.com/lcpz/lain.git
IMAP wiget is now asynchronous
This commit is contained in:
parent
0f35771a52
commit
f2fb4f6fda
|
@ -0,0 +1,80 @@
|
||||||
|
|
||||||
|
--[[
|
||||||
|
|
||||||
|
Licensed under GNU General Public License v2
|
||||||
|
* (c) 2013, Alexander Yakushev
|
||||||
|
|
||||||
|
--]]
|
||||||
|
|
||||||
|
-- Asynchronous io.popen for Awesome WM.
|
||||||
|
-- How to use...
|
||||||
|
-- ...asynchronously:
|
||||||
|
-- asyncshell.request('wscript -Kiev', function(f) wwidget.text = f:read("*l") end)
|
||||||
|
-- ...synchronously
|
||||||
|
-- wwidget.text = asyncshell.demand('wscript -Kiev', 5):read("*l") or "Error"
|
||||||
|
|
||||||
|
local spawn = require('awful.util').spawn
|
||||||
|
|
||||||
|
asyncshell = {}
|
||||||
|
asyncshell.request_table = {}
|
||||||
|
asyncshell.id_counter = 0
|
||||||
|
asyncshell.folder = "/tmp/asyncshell"
|
||||||
|
asyncshell.file_template = asyncshell.folder .. '/req'
|
||||||
|
|
||||||
|
-- Create a directory for asynchell response files
|
||||||
|
os.execute("mkdir -p " .. asyncshell.folder)
|
||||||
|
|
||||||
|
-- Returns next tag - unique identifier of the request
|
||||||
|
local function next_id()
|
||||||
|
asyncshell.id_counter = (asyncshell.id_counter + 1) % 100000
|
||||||
|
return asyncshell.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 asyncshell.request(command, callback)
|
||||||
|
local id = next_id()
|
||||||
|
local tmpfname = asyncshell.file_template .. id
|
||||||
|
asyncshell.request_table[id] = {callback = callback}
|
||||||
|
local req =
|
||||||
|
string.format("bash -c '%s > %s; " ..
|
||||||
|
'echo "asyncshell.deliver(%s)" | ' ..
|
||||||
|
"awesome-client' 2> /dev/null",
|
||||||
|
string.gsub(command, "'", "'\\''"), tmpfname,
|
||||||
|
id, tmpfname)
|
||||||
|
spawn(req, false)
|
||||||
|
return id
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Calls the remembered callback function on the output of the shell
|
||||||
|
-- command.
|
||||||
|
-- @param id Request ID
|
||||||
|
-- @param output The output file of the shell command to be delievered
|
||||||
|
function asyncshell.deliver(id)
|
||||||
|
if asyncshell.request_table[id] and
|
||||||
|
asyncshell.request_table[id].callback then
|
||||||
|
local output = io.open(asyncshell.file_template .. id, 'r')
|
||||||
|
asyncshell.request_table[id].callback(output)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Sends a synchronous request for an output of the command. Waits for
|
||||||
|
-- the output, but if the given timeout expires returns nil.
|
||||||
|
-- @param command Command to be executed and taken output from
|
||||||
|
-- @param timeout Maximum amount of time to wait for the result
|
||||||
|
-- @return File handler on success, nil otherwise
|
||||||
|
function asyncshell.demand(command, timeout)
|
||||||
|
local id = next_id()
|
||||||
|
local tmpfname = asyncshell.file_template .. id
|
||||||
|
local f = io.popen(string.format("(%s > %s; echo asyncshell_done) & " ..
|
||||||
|
"(sleep %s; echo asynchell_timeout)",
|
||||||
|
command, tmpfname, timeout))
|
||||||
|
local result = f:read("*line")
|
||||||
|
if result == "asyncshell_done" then
|
||||||
|
return io.open(tmpfname)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return asyncshell
|
|
@ -30,7 +30,7 @@ local function worker(args)
|
||||||
|
|
||||||
function alsa.update()
|
function alsa.update()
|
||||||
local f = assert(io.popen('amixer -M get ' .. channel))
|
local f = assert(io.popen('amixer -M get ' .. channel))
|
||||||
local mixer = f:read("*all")
|
local mixer = f:read("*a")
|
||||||
f:close()
|
f:close()
|
||||||
|
|
||||||
volume_now = {}
|
volume_now = {}
|
||||||
|
|
|
@ -116,7 +116,7 @@ local function worker(args)
|
||||||
function alsabar.update()
|
function alsabar.update()
|
||||||
-- Get mixer control contents
|
-- Get mixer control contents
|
||||||
local f = io.popen("amixer -M get " .. alsabar.channel)
|
local f = io.popen("amixer -M get " .. alsabar.channel)
|
||||||
local mixer = f:read("*all")
|
local mixer = f:read("*a")
|
||||||
f:close()
|
f:close()
|
||||||
|
|
||||||
-- Capture mixer control state: [5%] ... ... [on]
|
-- Capture mixer control state: [5%] ... ... [on]
|
||||||
|
|
|
@ -26,7 +26,7 @@ local function worker(args)
|
||||||
|
|
||||||
function base.update()
|
function base.update()
|
||||||
local f = assert(io.popen(cmd))
|
local f = assert(io.popen(cmd))
|
||||||
output = f:read("*all")
|
output = f:read("*a")
|
||||||
f:close()
|
f:close()
|
||||||
widget = base.widget
|
widget = base.widget
|
||||||
settings()
|
settings()
|
||||||
|
|
|
@ -88,7 +88,7 @@ function calendar:show(t_out, inc_offset)
|
||||||
.. calendar.font_size .. "'><b>"
|
.. calendar.font_size .. "'><b>"
|
||||||
.. f:read() .. "</b>\n\n"
|
.. f:read() .. "</b>\n\n"
|
||||||
.. f:read() .. "\n"
|
.. f:read() .. "\n"
|
||||||
.. f:read("*all"):gsub("\n*$", "")
|
.. f:read("*a"):gsub("\n*$", "")
|
||||||
.. "</span></tt>"
|
.. "</span></tt>"
|
||||||
f:close()
|
f:close()
|
||||||
|
|
||||||
|
|
|
@ -58,7 +58,7 @@ local function worker(args)
|
||||||
local dactive = active - cpu.last_active
|
local dactive = active - cpu.last_active
|
||||||
local dtotal = total - cpu.last_total
|
local dtotal = total - cpu.last_total
|
||||||
|
|
||||||
cpu_now = {}
|
cpu_noj = {}
|
||||||
cpu_now.usage = tostring(math.ceil((dactive / dtotal) * 100))
|
cpu_now.usage = tostring(math.ceil((dactive / dtotal) * 100))
|
||||||
|
|
||||||
widget = cpu.widget
|
widget = cpu.widget
|
||||||
|
|
|
@ -40,7 +40,7 @@ function fs:show(t_out)
|
||||||
fs:hide()
|
fs:hide()
|
||||||
|
|
||||||
local f = io.popen(helpers.scripts_dir .. "dfs")
|
local f = io.popen(helpers.scripts_dir .. "dfs")
|
||||||
ws = f:read("*all"):gsub("\n*$", "")
|
ws = f:read("*a"):gsub("\n*$", "")
|
||||||
f:close()
|
f:close()
|
||||||
|
|
||||||
notification = naughty.notify({
|
notification = naughty.notify({
|
||||||
|
|
|
@ -7,11 +7,11 @@
|
||||||
--]]
|
--]]
|
||||||
|
|
||||||
local helpers = require("lain.helpers")
|
local helpers = require("lain.helpers")
|
||||||
|
local async = require("lain.asyncshell")
|
||||||
|
|
||||||
local naughty = require("naughty")
|
local naughty = require("naughty")
|
||||||
local wibox = require("wibox")
|
local wibox = require("wibox")
|
||||||
|
|
||||||
local io = { popen = io.popen }
|
|
||||||
local string = { format = string.format,
|
local string = { format = string.format,
|
||||||
gsub = string.gsub }
|
gsub = string.gsub }
|
||||||
local tonumber = tonumber
|
local tonumber = tonumber
|
||||||
|
@ -42,7 +42,7 @@ local function worker(args)
|
||||||
if not is_plain
|
if not is_plain
|
||||||
then
|
then
|
||||||
local f = io.popen(password)
|
local f = io.popen(password)
|
||||||
password = f:read("*all"):gsub("\n", "")
|
password = f:read("*a"):gsub("\n", "")
|
||||||
f:close()
|
f:close()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -57,34 +57,37 @@ local function worker(args)
|
||||||
curl = string.format("%s --url imaps://%s:%s/INBOX -u %s:%s %s -k",
|
curl = string.format("%s --url imaps://%s:%s/INBOX -u %s:%s %s -k",
|
||||||
head_command, server, port, mail, password, request)
|
head_command, server, port, mail, password, request)
|
||||||
|
|
||||||
f = io.popen(curl)
|
async.request(curl, function(f)
|
||||||
ws = f:read("*all")
|
ws = f:read("*a")
|
||||||
f:close()
|
f:close()
|
||||||
|
|
||||||
_, mailcount = string.gsub(ws, "%d+", "")
|
_, mailcount = string.gsub(ws, "%d+", "")
|
||||||
_ = nil
|
_ = nil
|
||||||
|
|
||||||
widget = imap.widget
|
widget = imap.widget
|
||||||
settings()
|
settings()
|
||||||
|
|
||||||
if mailcount >= 1 and mailcount > helpers.get_map(mail)
|
if mailcount >= 1 and mailcount > helpers.get_map(mail)
|
||||||
then
|
then
|
||||||
if mailcount == 1 then
|
if mailcount == 1 then
|
||||||
nt = mail .. " has one new message"
|
nt = mail .. " has one new message"
|
||||||
else
|
else
|
||||||
nt = mail .. " has <b>" .. mailcount .. "</b> new messages"
|
nt = mail .. " has <b>" .. mailcount .. "</b> new messages"
|
||||||
|
end
|
||||||
|
naughty.notify({
|
||||||
|
preset = mail_notification_preset,
|
||||||
|
text = nt,
|
||||||
|
screen = client.focus and client.focus.screen or 1
|
||||||
|
})
|
||||||
end
|
end
|
||||||
naughty.notify({
|
|
||||||
preset = mail_notification_preset,
|
|
||||||
text = nt,
|
|
||||||
screen = client.focus and client.focus.screen or 1
|
|
||||||
})
|
|
||||||
end
|
|
||||||
|
|
||||||
helpers.set_map(mail, mailcount)
|
helpers.set_map(mail, mailcount)
|
||||||
|
end)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
helpers.newtimer(mail, timeout, update, true)
|
helpers.newtimer(mail, timeout, update, true)
|
||||||
|
|
||||||
return setmetatable(imap, { __index = imap.widget })
|
return setmetatable(imap, { __index = imap.widget })
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -53,7 +53,7 @@ local function worker(args)
|
||||||
local np = io.popen("find " .. line ..
|
local np = io.popen("find " .. line ..
|
||||||
"/new -mindepth 1 -type f " ..
|
"/new -mindepth 1 -type f " ..
|
||||||
"-not -name '.*' -printf a")
|
"-not -name '.*' -printf a")
|
||||||
local mailstring = np:read("*all")
|
local mailstring = np:read("*a")
|
||||||
|
|
||||||
-- Strip off leading mailpath.
|
-- Strip off leading mailpath.
|
||||||
local box = string.match(line, mailpath .. "/*([^/]+)")
|
local box = string.match(line, mailpath .. "/*([^/]+)")
|
||||||
|
|
|
@ -29,7 +29,7 @@ local net = {
|
||||||
|
|
||||||
function net.get_device()
|
function net.get_device()
|
||||||
f = io.popen("ip link show | cut -d' ' -f2,9")
|
f = io.popen("ip link show | cut -d' ' -f2,9")
|
||||||
ws = f:read("*all")
|
ws = f:read("*a")
|
||||||
f:close()
|
f:close()
|
||||||
ws = ws:match("%w+: UP")
|
ws = ws:match("%w+: UP")
|
||||||
if ws ~= nil then
|
if ws ~= nil then
|
||||||
|
|
|
@ -30,7 +30,7 @@ local function worker(args)
|
||||||
|
|
||||||
function update()
|
function update()
|
||||||
local f = io.open("/proc/loadavg")
|
local f = io.open("/proc/loadavg")
|
||||||
local ret = f:read("*all")
|
local ret = f:read("*a")
|
||||||
f:close()
|
f:close()
|
||||||
|
|
||||||
load_1, load_5, load_15 = string.match(ret, "([^%s]+) ([^%s]+) ([^%s]+)")
|
load_1, load_5, load_15 = string.match(ret, "([^%s]+) ([^%s]+) ([^%s]+)")
|
||||||
|
|
|
@ -31,7 +31,7 @@ local function worker(args)
|
||||||
local f = io.open(tempfile)
|
local f = io.open(tempfile)
|
||||||
if f ~= nil
|
if f ~= nil
|
||||||
then
|
then
|
||||||
coretemp_now = tonumber(f:read("*all")) / 1000
|
coretemp_now = tonumber(f:read("*a")) / 1000
|
||||||
f:close()
|
f:close()
|
||||||
else
|
else
|
||||||
coretemp_now = "N/A"
|
coretemp_now = "N/A"
|
||||||
|
|
Loading…
Reference in New Issue