From 7ae6a84b52ee990ce708a8afbf3f35be05aa292d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Gia=20Phong?= Date: Wed, 29 May 2019 14:24:24 +0700 Subject: [PATCH] [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 --- Changes.md | 2 +- widgets/mdir_all.lua | 43 +++++++++++++++++++++---------------------- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/Changes.md b/Changes.md index 2fd183d..c277149 100644 --- a/Changes.md +++ b/Changes.md @@ -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) diff --git a/widgets/mdir_all.lua b/widgets/mdir_all.lua index 793609e..184f41e 100644 --- a/widgets/mdir_all.lua +++ b/widgets/mdir_all.lua @@ -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)