]> 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:

from asynchshell to awful.spawn.easy_async; started making every widget asynchronous
[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 spawn            = require("awful").spawn
11 local wibox            = require("wibox")
12
13 local element_in_table = require("lain.helpers").element_in_table
14 local newtimer         = require("lain.helpers").newtimer
15 local read_pipe        = require("lain.helpers").read_pipe
16 local spairs           = require("lain.helpers").spairs
17
18 local io               = { popen  = io.popen }
19 local os               = { getenv = os.getenv }
20 local pairs            = pairs
21 local string           = { format = string.format,
22                            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 then awful.spawn(ext_mail_cmd) end
43
44         -- Find pathes to mailboxes.
45         local p = io.popen(string.format("find %s -mindepth 1 -maxdepth 2 -type d -not -name .git", mailpath))
46         local boxes = {}
47         repeat
48             line = p:read("*l")
49             if line then
50                 -- Find all files in the "new" subdirectory. For each
51                 -- file, print a single character (no newline). Don't
52                 -- match files that begin with a dot.
53                 -- Afterwards the length of this string is the number of
54                 -- new mails in that box.
55                 local mailstring = read_pipe(string.format("find %s /new -mindepth 1 -type f -not -name '.*' -printf a", line))
56
57                 -- Strip off leading mailpath.
58                 local box      = string.match(line, mailpath .. "/(.*)")
59                 local nummails = string.len(mailstring)
60
61                 if nummails > 0 then
62                     boxes[box] = nummails
63                 end
64             end
65         until not line
66         p:close()
67
68         local newmail = "no mail"
69         local total = 0
70
71         for box, number in spairs(boxes) do
72             -- Add this box only if it's not to be ignored.
73             if not util.element_in_table(box, ignore_boxes) then
74                 total = total + number
75                 if newmail == "no mail" then
76                     newmail = string.format("%s(%s)", box, number)
77                 else
78                     newmail = string.format("%s, %s(%s)", newmail, box, number)
79                 end
80             end
81         end
82
83         widget = maildir.widget
84
85         settings()
86     end
87
88     newtimer(mailpath, timeout, update, true)
89
90     return setmetatable(maildir, { __index = maildir.widget })
91 end
92
93 return setmetatable(maildir, { __call = function(_, ...) return worker(...) end })