[mdir] Switch to async call

mdir is only semi-tested that it does work:
I have not set up maildir to check if the matching patterns are correct
This commit is contained in:
Nguyễn Gia Phong 2019-05-29 14:24:24 +07:00
parent 06d96a92ba
commit 7ae6a84b52
2 changed files with 22 additions and 23 deletions

View File

@ -16,7 +16,7 @@ Added:
Fixed:
- [volume,gmail,battery(FreeBSD)] Deprecate `io.popen`
- [volume,gmail,bat_freebsd,mem_freebsd,net_freebsd,mdir] Deprecate `io.popen`
- [mpd] Lua 5.3 compatibility (for real this time); also correct a typo
- [pkg,weather,contrib/btc] Allow function call without Awesome
- [pkg] Use more updated front-ends for Debian/Ubuntu (apt) and Fedora (dnf)

View File

@ -5,9 +5,10 @@
---------------------------------------------------
-- {{{ Grab environment
local io = { popen = io.popen }
local setmetatable = setmetatable
local helpers = require("vicious.helpers")
local type = type
local helpers = require"vicious.helpers"
local spawn = require"vicious.spawn"
-- }}}
@ -17,27 +18,25 @@ local mdir_all = {}
-- {{{ Maildir widget type
local function worker(format, warg)
if not warg then return end
-- Initialize counters
local count = { new = 0, cur = 0 }
for i=1, #warg do
quoted_path = helpers.shellquote(warg[i])
-- Recursively find new messages
local f = io.popen("find "..quoted_path.." -type f -wholename '*/new/*'")
for line in f:lines() do count.new = count.new + 1 end
f:close()
-- Recursively find "old" messages lacking the Seen flag
local f = io.popen("find "..quoted_path.." -type f -regex '.*/cur/.*2,[^S]*$'")
for line in f:lines() do count.cur = count.cur + 1 end
f:close()
function mdir_all.async(format, warg, callback)
if type(warg) ~= "table" then return callback{} end
local starting_points = ""
for i,dir in ipairs(warg) do
starting_points = starting_points .. " " .. helpers.shellquote(dir)
end
if starting_points == "" then return callback{ 0, 0 } end
return {count.new, count.cur}
local new, cur = 0, 0
spawn.with_line_callback(
"find" .. starting_points .. " -type f -regex '.*/cur/.*2,[^S]*$';",
{ stdout = function (filename) cur = cur + 1 end,
output_done = function ()
spawn.with_line_callback(
"find" .. starting_points .. " -type f -path '*/new/*';",
{ stdout = function (filename) new = new + 1 end,
output_done = function () callback{ new, cur } end })
end })
end
-- }}}
return setmetatable(mdir_all, { __call = function(_, ...) return worker(...) end })
return helpers.setasyncall(mdir_all)