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

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