]> git.rmz.io Git - dotfiles.git/blobdiff - conky/functions.lua
conky: add mail check + change font
[dotfiles.git] / conky / functions.lua
diff --git a/conky/functions.lua b/conky/functions.lua
new file mode 100644 (file)
index 0000000..918914f
--- /dev/null
@@ -0,0 +1,82 @@
+--[[
+                                                  
+     Licensed under GNU General Public License v2 
+      * (c) 2014,      Samir Benmendil           
+      * (c) 2013,      Luke Bonham                
+      * (c) 2010-2012, Peter Hofmann              
+                                                  
+--]]
+
+local io              = io
+local os              = { getenv = os.getenv }
+local pairs           = pairs
+local string          = { len    = string.len,
+                          match  = string.match,
+                          format = string.format,
+                          sub    = string.sub }
+
+-- Maildir check
+-- lain.widgets.maildir
+local maildir = {}
+local newmail
+
+function conky_mail()
+    local updates=tonumber(conky_parse('${updates}'))
+    if newmail and updates % 30 ~= 0 then
+        return newmail
+    end
+    local mailpath     = os.getenv("HOME") .. "/mail"
+
+    -- Find pathes to mailboxes.
+    local dirs = io.popen("find " .. mailpath .. " -type d -name new -or -name cur -prune")
+    local boxes = {}
+    for line in dirs:lines() do
+        -- Strip off leading mailpath.
+        local acc = string.match(line, mailpath.."/*([^/]+)")
+        local box = string.match(line, mailpath..'/'..acc.."/*([^/]+)")
+        if boxes[acc] == nil then
+            boxes[acc] = {}
+        end
+        if boxes[acc][box] == nil then
+            boxes[acc][box] = {}
+        end
+
+        -- Find all files in the "new" subdirectory. For each
+        -- file, print a single character (no newline). Don't
+        -- match files that begin with a dot.
+        -- Afterwards the length of this string is the number of
+        -- new mails in that box.
+        if line:sub(-3) == 'new' then
+            local new = io.popen("find " .. line .. " -type f -not -name '.*' -printf a")
+            cnt_new = string.len(new:read("*all"))
+            if cnt_new > 0 then
+                boxes[acc][box]['cnt_new'] = string.format("${color e6db74}%5d${color}", cnt_new)
+            else
+                boxes[acc][box]['cnt_new'] = string.format("%5d", cnt_new)
+            end
+        elseif line:sub(-3) == 'cur' then
+            local old = io.popen("find " .. line .. " -type f -not -name '*:2,*S*' -printf a")
+            cnt_old = string.len(old:read("*all"))
+            if cnt_old > 0 then
+                boxes[acc][box]['cnt_old'] = string.format("${color fd971f}%2d${color}", cnt_old)
+            else
+                boxes[acc][box]['cnt_old'] = string.format("%2d", cnt_old)
+            end
+        end
+    end
+
+    newmail = ""
+    local accounts = {"gmail"}
+    local mailboxes = { {"inbox", "chakra"},
+                        {"youtube", "unimaas"}}
+    for a, acc in pairs(accounts) do
+        for b, box in ipairs(mailboxes) do
+            local boxl = boxes[acc][box[1]]
+            local boxr = boxes[acc][box[2]]
+            newmail = newmail .. string.format("%10s:%s/%s", box[1], boxl['cnt_new'], boxl['cnt_old'])
+            newmail = newmail .. string.format("%11s:%s/%s\n", box[2], boxr['cnt_new'], boxr['cnt_old'])
+        end
+    end
+    newmail = string.sub(newmail, 1 , #newmail - 1)
+    return newmail
+end