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

b5437bd9bee0d7d9530e6617fb235f2ce86650c0
[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 markup          = require("lain.util.markup")
11 local run_in_terminal = require("lain.helpers").run_in_terminal
12
13 local awful           = require("awful")
14 local beautiful       = require("beautiful")
15 local wibox           = require("wibox")
16
17 local io              = io
18 local os              = { getenv = os.getenv }
19 local pairs           = pairs
20 local string          = { len    = string.len,
21                           match  = string.match }
22 local table           = { sort   = table.sort }
23
24 local setmetatable    = setmetatable
25
26 -- Maildir check
27 -- lain.widgets.maildir
28 local maildir = {}
29
30 function worker(args)
31     local args = args or {}
32     local mailpath = args.mailpath or os.getenv("HOME") .. "/Mail"
33     local ignore_boxes = args.ignore_boxes or {}
34     local refresh_timeout = args.refresh_timeout or 60
35     local header = args.header or " Mail "
36     local header_color = args.header_color or beautiful.fg_normal or "#FFFFFF"
37     local color_newmail = args.color_newmail or beautiful.fg_focus or "#FFFFFF"
38     local color_nomail = args.color_nomail or beautiful.fg_normal or "#FFFFFF"
39     local app = args.app or "mutt"
40     local shadow = args.shadow or false
41
42     local mymailcheck = wibox.widget.textbox()
43     local mymailcheckupdate = function()
44         -- Find pathes to mailboxes.
45         local p = io.popen("find " .. mailpath ..
46                            " -mindepth 1 -maxdepth 1 -type d" ..
47                            " -not -name .git")
48         local boxes = {}
49         local line = ""
50         repeat
51             line = p:read("*l")
52             if line ~= nil
53             then
54                 -- Find all files in the "new" subdirectory. For each
55                 -- file, print a single character (no newline). Don't
56                 -- match files that begin with a dot.
57                 -- Afterwards the length of this string is the number of
58                 -- new mails in that box.
59                 local np = io.popen("find " .. line ..
60                                     "/new -mindepth 1 -type f " ..
61                                     "-not -name '.*' -printf a")
62                 local mailstring = np:read("*all")
63
64                 -- Strip off leading mailpath.
65                 local box = string.match(line, mailpath .. "/*([^/]+)")
66                 local nummails = string.len(mailstring)
67                 if nummails > 0
68                 then
69                     boxes[box] = nummails
70                 end
71             end
72         until line == nil
73
74         table.sort(boxes)
75
76         local newmail = ""
77         local count = 0
78         for box, number in pairs(boxes)
79         do
80             count = count + 1
81             -- Add this box only if it's not to be ignored.
82             if not util.element_in_table(box, ignore_boxes)
83             then
84                 if newmail == ""
85                 then
86                     newmail = box .. "(" .. number .. ")"
87                 else
88                     newmail = newmail .. ", " ..
89                               box .. "(" .. number .. ")"
90                 end
91             end
92         end
93
94         if count == 1 then
95             -- it will be only executed once
96             for box, number in pairs(boxes)
97             do  -- it's useless to show only INBOX(x)
98                 if box == "INBOX" then newmail = number end
99             end
100         end
101
102         if newmail == ""
103         then
104             if shadow
105             then
106                 mymailcheck:set_text('')
107             else
108                 myimapcheck:set_markup(markup(color_nomail, " no mail "))
109             end
110         else
111             myimapcheck:set_markup(markup(header_color, header) ..
112                                    markup(color_newmail, newmail) .. " ")
113         end
114     end
115
116     local mymailchecktimer = timer({ timeout = refresh_timeout })
117     mymailchecktimer:connect_signal("timeout", mymailcheckupdate)
118     mymailchecktimer:start()
119     mymailcheck:buttons(awful.util.table.join(
120         awful.button({}, 0,
121             function()
122                 run_in_terminal(app)
123             end)
124     ))
125
126     return mymailcheck
127 end
128
129 return setmetatable(maildir, { __call = function(_, ...) return worker(...) end })