]> git.madduck.net Git - etc/awesome.git/blob - widgets/maildir.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

bat: type checking on perc #209
[etc/awesome.git] / widgets / maildir.lua
1
2 --[[
3                                                   
4      Licensed under GNU General Public License v2 
5       * (c) 2013,      Luke Bonham                
6       * (c) 2010-2012, Peter Hofmann              
7                                                   
8 --]]
9
10 local newtimer        = require("lain.helpers").newtimer
11 local read_pipe       = require("lain.helpers").read_pipe
12 local spairs          = require("lain.helpers").spairs
13
14 local wibox           = require("wibox")
15
16 local awful           = require("awful")
17 local util            = require("lain.util")
18
19 local io              = { popen  = io.popen }
20 local os              = { getenv = os.getenv }
21 local pairs           = pairs
22 local string          = { len    = string.len,
23                           match  = string.match }
24
25 local setmetatable    = setmetatable
26
27 -- Maildir check
28 -- lain.widgets.maildir
29 local maildir = {}
30
31 local function worker(args)
32     local args         = args or {}
33     local timeout      = args.timeout or 60
34     local mailpath     = args.mailpath or os.getenv("HOME") .. "/Mail"
35     local ignore_boxes = args.ignore_boxes or {}
36     local settings     = args.settings or function() end
37     local ext_mail_cmd = args.external_mail_cmd
38
39     maildir.widget = wibox.widget.textbox('')
40
41     function update()
42         if ext_mail_cmd ~= nil
43         then
44             awful.util.spawn(ext_mail_cmd)
45         end
46
47         -- Find pathes to mailboxes.
48         local p = io.popen("find " .. mailpath ..
49                            " -mindepth 1 -maxdepth 2 -type d" ..
50                            " -not -name .git")
51         local boxes = {}
52         repeat
53             line = p:read("*l")
54             if line ~= nil
55             then
56                 -- Find all files in the "new" subdirectory. For each
57                 -- file, print a single character (no newline). Don't
58                 -- match files that begin with a dot.
59                 -- Afterwards the length of this string is the number of
60                 -- new mails in that box.
61                 local mailstring = read_pipe("find " .. line ..
62                                     "/new -mindepth 1 -type f " ..
63                                     "-not -name '.*' -printf a")
64
65                 -- Strip off leading mailpath.
66                 local box = string.match(line, mailpath .. "/(.*)")
67                 local nummails = string.len(mailstring)
68                 if nummails > 0
69                 then
70                     boxes[box] = nummails
71                 end
72             end
73         until line == nil
74
75         p:close()
76
77         newmail = "no mail"
78         -- Count the total number of mails irrespective of where it was found
79         total = 0
80
81         for box, number in spairs(boxes)
82         do
83             -- Add this box only if it's not to be ignored.
84             if not util.element_in_table(box, ignore_boxes)
85             then
86                 total = total + number
87                 if newmail == "no mail"
88                 then
89                     newmail = box .. "(" .. number .. ")"
90                 else
91                     newmail = newmail .. ", " ..
92                               box .. "(" .. number .. ")"
93                 end
94             end
95         end
96
97         widget = maildir.widget
98         settings()
99     end
100
101     newtimer(mailpath, timeout, update, true)
102     return maildir.widget
103 end
104
105 return setmetatable(maildir, { __call = function(_, ...) return worker(...) end })