]> git.rmz.io Git - dotfiles.git/blob - conky/functions.lua
Other changes at cadscan (with the ones I want to keep stripped)
[dotfiles.git] / conky / functions.lua
1 --[[
2
3 Licensed under GNU General Public License v2
4 * (c) 2014, Samir Benmendil
5 * (c) 2013, Luke Bonham
6 * (c) 2010-2012, Peter Hofmann
7
8 --]]
9
10 local io = io
11 local os = { getenv = os.getenv }
12 local pairs = pairs
13 local string = { len = string.len,
14 match = string.match,
15 format = string.format,
16 sub = string.sub }
17
18 -- Maildir check
19 -- lain.widgets.maildir
20 local maildir = {}
21 local newmail
22
23 function conky_mail()
24 local updates=tonumber(conky_parse('${updates}'))
25 if newmail and updates % 30 ~= 0 then
26 return newmail
27 end
28 local mailpath = os.getenv("HOME") .. "/mail"
29
30 -- Find pathes to mailboxes.
31 local dirs = io.popen("find " .. mailpath .. " -type d -name new -or -name cur -prune")
32 local boxes = {}
33 for line in dirs:lines() do
34 -- Strip off leading mailpath.
35 local acc = string.match(line, mailpath.."/*([^/]+)")
36 local box = string.match(line, mailpath..'/'..acc.."/*([^/]+)")
37 if boxes[acc] == nil then
38 boxes[acc] = {}
39 end
40 if boxes[acc][box] == nil then
41 boxes[acc][box] = {}
42 end
43
44 -- Find all files in the "new" subdirectory. For each
45 -- file, print a single character (no newline). Don't
46 -- match files that begin with a dot.
47 -- Afterwards the length of this string is the number of
48 -- new mails in that box.
49 if line:sub(-3) == 'new' then
50 local new = io.popen("find " .. line .. " -type f -not -name '.*' -printf a")
51 cnt_new = string.len(new:read("*all"))
52 new:close()
53 if cnt_new > 0 then
54 boxes[acc][box]['cnt_new'] = string.format("${color e6db74}%5d${color}", cnt_new)
55 else
56 boxes[acc][box]['cnt_new'] = string.format("%5d", cnt_new)
57 end
58 elseif line:sub(-3) == 'cur' then
59 local old = io.popen("find " .. line .. " -type f -not -name '*:2,*S*' -printf a")
60 cnt_old = string.len(old:read("*all"))
61 old:close()
62 if cnt_old > 0 then
63 boxes[acc][box]['cnt_old'] = string.format("${color fd971f}%2d${color}", cnt_old)
64 else
65 boxes[acc][box]['cnt_old'] = string.format("%2d", cnt_old)
66 end
67 end
68 end
69 dirs:close()
70
71 newmail = ""
72 local accounts = {"gmail"}
73 local mailboxes = { {"inbox", "chakra"},
74 {"youtube", "unimaas"}}
75 for a, acc in pairs(accounts) do
76 for b, box in ipairs(mailboxes) do
77 local boxl = boxes[acc][box[1]]
78 local boxr = boxes[acc][box[2]]
79 newmail = newmail .. string.format("%10s:%s/%s", box[1], boxl['cnt_new'], boxl['cnt_old'])
80 newmail = newmail .. string.format("%11s:%s/%s\n", box[2], boxr['cnt_new'], boxr['cnt_old'])
81 end
82 end
83 newmail = string.sub(newmail, 1 , #newmail - 1)
84 return newmail
85 end