]> git.madduck.net Git - etc/awesome.git/blob - widgets/contrib/task.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:

6c8c9cce01518496a654b29ed2afcca05b979458
[etc/awesome.git] / widgets / contrib / task.lua
1
2 --[[
3                                                   
4      Licensed under GNU General Public License v2 
5       * (c) 2013, Jan Xie                         
6                                                   
7 --]]
8
9 local icons_dir    = require("lain.helpers").icons_dir
10
11 local awful        = require("awful")
12 local beautiful    = require("beautiful")
13 local naughty      = require("naughty")
14
15 local io           = io
16 local string       = { len = string.len }
17 local tonumber     = tonumber
18
19 local setmetatable = setmetatable
20
21 -- Taskwarrior notification
22 -- lain.widgets.contrib.task
23 local task = {}
24
25 local task_notification = nil
26
27 function findLast(haystack, needle)
28     local i=haystack:match(".*"..needle.."()")
29     if i==nil then return nil else return i-1 end
30 end
31
32 function task.hide()
33     if task_notification ~= nil then
34         naughty.destroy(task_notification)
35         task_notification = nil
36     end
37 end
38
39 function task.show(scr_pos)
40     task.hide()
41
42     local f, c_text, scrp
43
44     if task.followtag then
45         scrp = awful.screen.focused()
46     else
47         scrp = scr_pos or task.scr_pos
48     end
49
50     f = io.popen('task ' .. task.cmdline)
51     c_text = "<span font='"
52              .. task.font .. " "
53              .. task.font_size .. "'>"
54              .. awful.util.escape(f:read("*all"):gsub("\n*$", ""))
55              .. "</span>"
56     f:close()
57
58     task_notification = naughty.notify({ title = "[task next]",
59                                          text = c_text,
60                                          icon = task.notify_icon,
61                                          position = task.position,
62                                          fg = task.fg,
63                                          bg = task.bg,
64                                          timeout = task.timeout,
65                                          screen = scrp
66                                      })
67 end
68
69 function task.prompt_add()
70   awful.prompt.run({ prompt = "Add task: " },
71       mypromptbox[awful.screen.focused()].widget,
72       function (...)
73           local f = io.popen("task add " .. ...)
74           c_text = "\n<span font='"
75                    .. task.font .. " "
76                    .. task.font_size .. "'>"
77                    .. awful.util.escape(f:read("*all"))
78                    .. "</span>"
79           f:close()
80
81           naughty.notify({
82               text     = c_text,
83               icon     = task.notify_icon,
84               position = task.position,
85               fg       = task.fg,
86               bg       = task.bg,
87               timeout  = task.timeout,
88           })
89       end,
90       nil,
91       awful.util.getdir("cache") .. "/history_task_add")
92 end
93
94 function task.prompt_search()
95   awful.prompt.run({ prompt = "Search task: " },
96       mypromptbox[awful.screen.focused()].widget,
97       function (...)
98           local f = io.popen("task " .. ...)
99           c_text = f:read("*all"):gsub(" \n*$", "")
100           f:close()
101
102           if string.len(c_text) == 0
103           then
104               c_text = "No results found."
105           else
106               c_text = "<span font='"
107                        .. task.font .. " "
108                        .. task.font_size .. "'>"
109                        .. awful.util.escape(c_text)
110                        .. "</span>"
111           end
112
113           naughty.notify({
114               title    = "[task next " .. ... .. "]",
115               text     = c_text,
116               icon     = task.notify_icon,
117               position = task.position,
118               fg       = task.fg,
119               bg       = task.bg,
120               timeout  = task.timeout,
121               screen   = awful.screen.focused()
122           })
123       end,
124       nil,
125       awful.util.getdir("cache") .. "/history_task")
126 end
127
128 function task.attach(widget, args)
129     local args       = args or {}
130
131     task.font_size   = tonumber(args.font_size) or 12
132     task.font        = args.font or beautiful.font:sub(beautiful.font:find(""),
133                        findLast(beautiful.font, " "))
134     task.fg          = args.fg or beautiful.fg_normal or "#FFFFFF"
135     task.bg          = args.bg or beautiful.bg_normal or "#FFFFFF"
136     task.position    = args.position or "top_right"
137     task.timeout     = args.timeout or 7
138     task.scr_pos     = args.scr_pos or 1
139     task.followtag   = args.followtag or false
140     task.cmdline     = args.cmdline or "next"
141
142     task.notify_icon = icons_dir .. "/taskwarrior/task.png"
143     task.notify_icon_small = icons_dir .. "/taskwarrior/tasksmall.png"
144
145     widget:connect_signal("mouse::enter", function () task.show(task.scr_pos) end)
146     widget:connect_signal("mouse::leave", function () task.hide() end)
147 end
148
149 return setmetatable(task, { __call = function(_, ...) return create(...) end })