From 418e475b5b4eb8404e115d74fdb3756169877278 Mon Sep 17 00:00:00 2001 From: Sudo Nice Date: Wed, 18 Nov 2015 11:02:16 +0300 Subject: [PATCH 1/1] helpers: spairs() added --- helpers.lua | 20 ++++++++++++++++++++ widgets/maildir.lua | 7 +++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/helpers.lua b/helpers.lua index 4e90e16..4ece329 100644 --- a/helpers.lua +++ b/helpers.lua @@ -13,6 +13,7 @@ local io = { open = io.open, lines = io.lines, popen = io.popen } local rawget = rawget +local table = { sort = table.sort } -- Lain helper functions for internal use -- lain.helpers @@ -113,4 +114,23 @@ end -- }}} +--{{{ Iterate over table of records sorted by keys +function helpers.spairs(t) + -- collect the keys + local keys = {} + for k in pairs(t) do keys[#keys+1] = k end + + table.sort(keys) + + -- return the iterator function + local i = 0 + return function() + i = i + 1 + if keys[i] then + return keys[i], t[keys[i]] + end + end +end +--}}} + return helpers diff --git a/widgets/maildir.lua b/widgets/maildir.lua index eed6138..79d28b5 100644 --- a/widgets/maildir.lua +++ b/widgets/maildir.lua @@ -9,6 +9,7 @@ local newtimer = require("lain.helpers").newtimer local read_pipe = require("lain.helpers").read_pipe +local spairs = require("lain.helpers").spairs local wibox = require("wibox") @@ -19,7 +20,6 @@ local os = { getenv = os.getenv } local pairs = pairs local string = { len = string.len, match = string.match } -local table = { sort = table.sort } local setmetatable = setmetatable @@ -65,14 +65,13 @@ local function worker(args) end until line == nil - p:close() - table.sort(boxes) + p:close() newmail = "no mail" -- Count the total number of mails irrespective of where it was found total = 0 - for box, number in pairs(boxes) + for box, number in spairs(boxes) do -- Add this box only if it's not to be ignored. if not util.element_in_table(box, ignore_boxes) -- 2.39.5