1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
--------------------------------
-- Author: Gregor Best --
-- Copyright 2009 Gregor Best --
--------------------------------
local os_time = os.time
local os_date = os.date
local tostring = tostring
local pairs = pairs
local print = print
local setmetatable = setmetatable
local tonumber = tonumber
local type = type
local os = {
date = os.date,
getenv = os.getenv
}
local io = {
lines = io.lines,
popen = io.popen,
}
local string = {
match = string.match
}
local table = {
insert = table.insert
}
local capi = {
mouse = mouse,
screen = screen
}
local awful = require("awful")
local wibox = require("wibox")
local beautiful = require("beautiful")
local naughty = require("naughty")
local lib = {
hooks = require("obvious.lib.hooks"),
markup = require("obvious.lib.markup")
}
local clock = {}
local initialized = false
local defaults = {
shorttimeformat = "%T",
longtimeformat = "%T %D",
editor = nil,
shorttimer = 60,
longtimer = 120,
scrolling = false,
scrolltimeout = 10,
}
local settings = setmetatable({}, { __index = defaults })
local menu
local function edit(file)
if not settings.editor then
naughty.notify({ text="Obvious Clock: You need to configure your" ..
" editor. See readme.",
timeout=0 })
else
awful.util.spawn(settings.editor .. " " .. file)
end
end
local function pread(cmd)
local pipe = io.popen(cmd)
if not pipe then
return ''
end
local results = pipe:read '*a'
pipe:close()
return results
end
-- non BSD-like `cal` for -h return help
local function is_bsd()
return not pread('cal -h'):find('help')
end
local show_calendar
do
local cal_notification
function show_calendar(year, month)
local cmd = 'cal'
if is_bsd() then
cmd = 'cal -h'
end
cmd = cmd .. ' ' .. tostring(month) .. ' ' .. tostring(year)
local notify_args = {
text = lib.markup.font("monospace",
pread(cmd):
gsub("([^0-9])(" .. tonumber(os.date("%d")) .. ")([^0-9])",
"%1<span foreground=\"#FF0000\">%2</span>%3"):gsub("\n+$", "")),
screen = capi.mouse.screen
}
if cal_notification and cal_notification.box.visible then
notify_args.replaces_id = cal_notification.id
end
cal_notification = naughty.notify(notify_args)
end
end
local alarmfile = awful.util.getdir("config").."/alarms"
local fulldate = false
local alarms = { }
local widget = wibox.widget.textbox()
local last_scroll_time = 0
local displayyear = 0
local displaymonth = 0
widget:buttons(awful.util.table.join(
awful.button({ }, 3, function ()
menu:toggle()
end),
awful.button({}, 4, function()
displaymonth = displaymonth + 1
if displaymonth == 13 then
displaymonth = 1
displayyear = displayyear + 1
end
show_calendar(displayyear, displaymonth)
last_scroll_time = os_time()
end),
awful.button({}, 5, function()
displaymonth = displaymonth - 1
if displaymonth == 0 then
displaymonth = 12
displayyear = displayyear - 1
end
show_calendar(displayyear, displaymonth)
last_scroll_time = os_time()
end),
awful.button({ }, 1, function ()
if #alarms > 0 then
for _, v in pairs(alarms) do
naughty.notify({
text = v[2],
title = v[1],
screen = capi.mouse.screen
})
end
alarms = { }
widget.bg = beautiful.bg_normal
else
if not settings.scrolling or os_time() - settings.scrolltimeout > last_scroll_time then
local date_info = os_date '*t'
displayyear = date_info.year
displaymonth = date_info.month
end
show_calendar(displayyear, displaymonth)
last_scroll_time = os_time()
end
end)
))
local function read_alarms(file)
local rv = { }
local date = nil
if not awful.util.file_readable(file) then
return { }
end
for line in io.lines(file) do
line = line:gsub("\\n", "\n")
if not date then
date = line
else
rv[date] = line
date = nil
end
end
return rv
end
local function update (trigger_alarms)
if trigger_alarms == nil then
trigger_alarms = true
end
local date
if fulldate then
if type(settings.longtimeformat) == "string" then
date = os.date(settings.longtimeformat)
elseif type(settings.longtimeformat) == "function" then
date = os.date(settings.longtimeformat())
end
if not date then
date = os.date(defaults.longtimeformat)
end
else
if type(settings.shorttimeformat) == "string" then
date = os.date(settings.shorttimeformat)
elseif type(settings.shorttimeformat) == "function" then
date = os.date(settings.shorttimeformat())
end
if not date then
date = os.date(defaults.shorttimeformat)
end
end
if #alarms > 0 then
date = lib.markup.fg.color(beautiful.fg_focus, date)
widget.bg = beautiful.bg_focus
else
widget.bg = beautiful.bg_normal
end
widget:set_markup(date)
if trigger_alarms then
local data = read_alarms(alarmfile)
local currentdate = os.date("%a-%d-%m-%Y:%H:%M")
for date, message in pairs(data) do
if currentdate:match(date) then
naughty.notify({
text = message,
title = currentdate,
screen = capi.screen.count()
})
local add = true
for _, v in pairs(alarms) do
if v[1] == date and v[2] == message then
add = false
break
end
end
if add then table.insert(alarms, { currentdate, message }) end
end
end
update(false)
end
end
widget:connect_signal("mouse::enter", function ()
fulldate = true
update(false)
end)
widget:connect_signal("mouse::leave", function ()
fulldate = false
update(false)
end)
function clock.set_editor(e)
settings.editor = e or defaults.editor
end
function clock.set_longformat(strOrFn)
settings.longtimeformat = strOrFn or defaults.longtimeformat
update(false)
end
function clock.set_shortformat(strOrFn)
settings.shorttimeformat = strOrFn or defaults.shorttimeformat
update(false)
end
function clock.set_shorttimer(delay)
settings.shorttimer = delay or defaults.shorttimer
end
function clock.set_longtimer(delay)
settings.longtimer = delay or defaults.longtimer
end
function clock.set_scrolling(active)
settings.scrolling = active
end
function clock.set_scrolltimeout(timeout)
settings.scrolltimeout = timeout
end
return setmetatable(clock, { __call = function ()
update()
if not initialized then
lib.hooks.timer.register(settings.shorttimer, settings.longtimer, update)
lib.hooks.timer.start(update)
menu = awful.menu.new({
id = "clock",
items = {
{ "Edit Todo", function () edit(os.getenv("HOME") .. "/todo") end },
{ "Edit Alarms", function () edit(alarmfile) end }
}
})
initialized = true
end
return widget
end })
-- vim:ft=lua:ts=2:sw=2:sts=2:tw=80:et
|