-
Notifications
You must be signed in to change notification settings - Fork 672
Expand file tree
/
Copy pathinit.lua
More file actions
366 lines (339 loc) · 14.9 KB
/
init.lua
File metadata and controls
366 lines (339 loc) · 14.9 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
--- === hs.timer ===
---
--- Execute functions with various timing rules
---
--- **NOTE**: timers use NSTimer internally, which will be paused when computers sleep.
--- Especially, repeating timers won't be triggered at the specified time when there are sleeps in between.
--- The workaround is to prevent system from sleeping, configured in Energy Saver in System Preferences.
local module = require("hs.timer.internal")
local log=require'hs.logger'.new('timer',3)
module.setLogLevel=log.setLogLevel
local type,ipairs,tonumber,floor,date=type,ipairs,tonumber,math.floor,os.date
-- private variables and methods -----------------------------------------
local TIME_PATTERNS={'-??:??:??-','-??:??--','??d??h---','-??h??m--','--??m??s-','??d----','-??h---','--??m--','---??s-','----????ms'}
-- ms unused, but it might be useful in the future
do
for i,s in ipairs(TIME_PATTERNS) do
TIME_PATTERNS[i]='^'..(s:gsub('%?%?%?%?','(%%d%%d?%%d?%%d?)'):gsub('%?%?','(%%d%%d?)'):gsub('%-','()'))..'$'
end
end
local function timeStringToSeconds(time)
if type(time)=='string' then
local d,h,m,s,ms
for _,pattern in ipairs(TIME_PATTERNS) do
d,h,m,s,ms=time:match(pattern) if d then break end
end
if not d then error('invalid time string '..time,3) end
if type(d)=='number' then d=0 end --remove "missing" captures
if type(h)=='number' then h=0 end
if type(m)=='number' then m=0 end
if type(s)=='number' then s=0 end
if type(ms)=='number' then ms=0 end
d=tonumber(d) h=tonumber(h) m=tonumber(m) s=tonumber(s) ms=tonumber(ms)
if h>=24 or m>=60 or s>=60 then error('invalid time string '..time,3) end
time=d*86400+h*3600+m*60+s+(ms/1000)
end
if type(time)~='number' or time<0 then error('invalid time',3) end
return time
end
-- Public interface ------------------------------------------------------
--- hs.timer.seconds(timeOrDuration) -> seconds
--- Function
--- Converts a string with a time of day or a duration into number of seconds
---
--- Parameters:
--- * timeOrDuration - a string that can have any of the following formats:
--- * "HH:MM:SS" or "HH:MM" - represents a time of day (24-hour clock), returns the number of seconds since midnight
--- * "DDdHHh", "HHhMMm", "MMmSSs", "DDd", "HHh", "MMm", "SSs", "NNNNms" - represents a duration in days, hours, minutes,
--- seconds and/or milliseconds
---
--- Returns:
--- * The number of seconds
function module.seconds(n) return timeStringToSeconds(n) end
--- hs.timer.minutes(n) -> seconds
--- Function
--- Converts minutes to seconds
---
--- Parameters:
--- * n - A number of minutes
---
--- Returns:
--- * The number of seconds in n minutes
function module.minutes(n) return 60 * n end
--- hs.timer.hours(n) -> seconds
--- Function
--- Converts hours to seconds
---
--- Parameters:
--- * n - A number of hours
---
--- Returns:
--- * The number of seconds in n hours
function module.hours(n) return 60 * 60 * n end
--- hs.timer.days(n) -> sec
--- Function
--- Converts days to seconds
---
--- Parameters:
--- * n - A number of days
---
--- Returns:
--- * The number of seconds in n days
function module.days(n) return 60 * 60 * 24 * n end
--- hs.timer.weeks(n) -> sec
--- Function
--- Converts weeks to seconds
---
--- Parameters:
--- * n - A number of weeks
---
--- Returns:
--- * The number of seconds in n weeks
function module.weeks(n) return 60 * 60 * 24 * 7 * n end
--- hs.timer.waitUntil(predicateFn, actionFn[, checkInterval]) -> timer
--- Constructor
--- Creates and starts a timer which will perform `actionFn` when `predicateFn` returns true. The timer is automatically stopped when `actionFn` is called.
---
--- Parameters:
--- * predicateFn - a function which determines when `actionFn` should be called. This function takes no arguments, but should return true when it is time to call `actionFn`.
--- * actionFn - a function which performs the desired action. This function may take a single argument, the timer itself.
--- * checkInterval - an optional parameter indicating how often to repeat the `predicateFn` check. Defaults to 1 second.
---
--- Returns:
--- * a timer object
---
--- Notes:
--- * The timer is stopped before `actionFn` is called, but the timer is passed as an argument to `actionFn` so that the actionFn may restart the timer to be called again the next time predicateFn returns true.
--- * See also `hs.timer.waitWhile`, which is essentially the opposite of this function
module.waitUntil = function(predicateFn, actionFn, checkInterval)
checkInterval = checkInterval or 1
local stopWatch
stopWatch = module.new(checkInterval, function()
if predicateFn() then
stopWatch:stop()
actionFn(stopWatch)
end
end):start()
return stopWatch
end
--- hs.timer.doUntil(predicateFn, actionFn[, checkInterval]) -> timer
--- Constructor
--- Creates and starts a timer which will perform `actionFn` every `checkinterval` seconds until `predicateFn` returns true. The timer is automatically stopped when `predicateFn` returns true.
---
--- Parameters:
--- * predicateFn - a function which determines when to stop calling `actionFn`. This function takes no arguments, but should return true when it is time to stop calling `actionFn`.
--- * actionFn - a function which performs the desired action. This function may take a single argument, the timer itself.
--- * checkInterval - an optional parameter indicating how often to repeat the `predicateFn` check. Defaults to 1 second.
---
--- Returns:
--- * a timer object
---
--- Notes:
--- * The timer is passed as an argument to `actionFn` so that it may stop the timer prematurely (i.e. before predicateFn returns true) if desired.
--- * See also `hs.timer.doWhile`, which is essentially the opposite of this function
module.doUntil = function(predicateFn, actionFn, checkInterval)
checkInterval = checkInterval or 1
local stopWatch
stopWatch = module.new(checkInterval, function()
if not predicateFn() then
actionFn(stopWatch)
else
stopWatch:stop()
end
end):start()
return stopWatch
end
--- hs.timer.doEvery(interval, fn) -> timer
--- Constructor
--- Repeats fn every interval seconds.
---
--- Parameters:
--- * interval - A number of seconds between triggers
--- * fn - A function to call every time the timer triggers
---
--- Returns:
--- * An `hs.timer` object
---
--- Notes:
--- * This function is a shorthand for `hs.timer.new(interval, fn):start()`
module.doEvery = function(...)
return module.new(...):start()
end
--- hs.timer.waitWhile(predicateFn, actionFn[, checkInterval]) -> timer
--- Constructor
--- Creates and starts a timer which will perform `actionFn` when `predicateFn` returns false. The timer is automatically stopped when `actionFn` is called.
---
--- Parameters:
--- * predicateFn - a function which determines when `actionFn` should be called. This function takes no arguments, but should return false when it is time to call `actionFn`.
--- * actionFn - a function which performs the desired action. This function may take a single argument, the timer itself.
--- * checkInterval - an optional parameter indicating how often to repeat the `predicateFn` check. Defaults to 1 second.
---
--- Returns:
--- * a timer object
---
--- Notes:
--- * The timer is stopped before `actionFn` is called, but the timer is passed as an argument to `actionFn` so that the actionFn may restart the timer to be called again the next time predicateFn returns false.
--- * See also `hs.timer.waitUntil`, which is essentially the opposite of this function
module.waitWhile = function(predicateFn, ...)
return module.waitUntil(function() return not predicateFn() end, ...)
end
--- hs.timer.doWhile(predicateFn, actionFn[, checkInterval]) -> timer
--- Constructor
--- Creates and starts a timer which will perform `actionFn` every `checkinterval` seconds while `predicateFn` returns true. The timer is automatically stopped when `predicateFn` returns false.
---
--- Parameters:
--- * predicateFn - a function which determines when to stop calling `actionFn`. This function takes no arguments, but should return false when it is time to stop calling `actionFn`.
--- * actionFn - a function which performs the desired action. This function may take a single argument, the timer itself.
--- * checkInterval - an optional parameter indicating how often to repeat the `predicateFn` check. Defaults to 1 second.
---
--- Returns:
--- * a timer object
---
--- Notes:
--- * The timer is passed as an argument to `actionFn` so that it may stop the timer prematurely (i.e. before predicateFn returns false) if desired.
--- * See also `hs.timer.doUntil`, which is essentially the opposite of this function
module.doWhile = function(predicateFn, ...)
return module.doUntil(function() return not predicateFn() end, ...)
end
--- hs.timer.localTime() -> number
--- Function
--- Returns the number of seconds since local time midnight
---
--- Parameters:
--- * None
---
--- Returns:
--- * the number of seconds
module.localTime = function()
local tnow=date('*t')
return tnow.sec+tnow.min*60+tnow.hour*3600
end
--- hs.timer.doAt(time[, repeatInterval], fn[, continueOnError]) -> timer
--- Constructor
--- Creates and starts a timer which will perform `fn` at the given (local) `time` and then (optionally) repeat it every `interval`.
---
--- Parameters:
--- * time - number of seconds after (local) midnight, or a string in the format "HH:MM" (24-hour local time), indicating
--- the desired trigger time
--- * repeatInterval - (optional) number of seconds between triggers, or a string in the format
--- "DDd", "DDdHHh", "HHhMMm", "HHh" or "MMm" indicating days, hours and/or minutes between triggers; if omitted
--- or `0` the timer will trigger only once
--- * fn - a function to call every time the timer triggers
--- * continueOnError - an optional boolean flag, defaulting to false, which indicates that the timer should not be automatically stopped if the callback function results in an error.
---
--- Returns:
--- * a timer object
---
--- Notes:
--- * The timer can trigger up to 1 second early or late
--- * The first trigger will be set to the earliest occurrence given the `repeatInterval`; if that's omitted,
--- and `time` is earlier than the current time, the timer will trigger the next day. If the repeated interval
--- results in exactly 24 hours you can schedule regular jobs that will run at the expected time independently
--- of when Hammerspoon was restarted/reloaded. E.g.:
--- * If it's 19:00, `hs.timer.doAt("20:00",somefn)` will set the timer 1 hour from now
--- * If it's 21:00, `hs.timer.doAt("20:00",somefn)` will set the timer 23 hours from now
--- * If it's 21:00, `hs.timer.doAt("20:00","6h",somefn)` will set the timer 5 hours from now (at 02:00)
--- * To run a job every hour on the hour from 8:00 to 20:00: `for h=8,20 do hs.timer.doAt(h..":00","1d",runJob) end`
module.doAt = function(time,interval,fn,continueOnError)
if type(interval)=='function' then continueOnError=fn fn=interval interval=0 end
interval=timeStringToSeconds(interval)
if interval~=0 and interval<60 then error('invalid interval',2) end -- degenerate use case for this function
time=timeStringToSeconds(time)
local now=module.localTime()
while time<=now do
time=time+(interval==0 and module.days(1) or interval)
end
local delta=time-now
time=time%86400 -- for logging
log.df('timer set for %02d:%02d:%02d, %dh%dm%ds from now',
floor(time/3600),floor(time/60)%60,floor(time%60),floor(delta/3600),floor(delta/60)%60,floor(delta%60))
return module.new(interval,fn,continueOnError):start():setNextTrigger(delta)
end
--- === hs.timer.delayed ===
---
--- Specialized timer objects to coalesce processing of unpredictable asynchronous events into a single callback
--- hs.timer.delayed:start([delay]) -> hs.timer.delayed object
--- Method
--- Starts or restarts the callback countdown
---
--- Parameters:
--- * delay - (optional) if provided, sets the countdown duration to this number of seconds for this time only; subsequent calls to `:start()` will revert to the original delay (or to the delay set with `:setDelay(delay)`)
---
--- Returns:
--- * the delayed timer object
--- hs.timer.delayed:stop() -> hs.timer.delayed object
--- Method
--- Cancels the callback countdown, if running; the callback will therefore not be triggered
---
--- Parameters:
--- * None
---
--- Returns:
--- * the delayed timer object
--- hs.timer.delayed:running() -> boolean
--- Method
--- Returns a boolean indicating whether the callback countdown is running
---
--- Parameters:
--- * None
---
--- Returns:
--- * a boolean
--- hs.timer.delayed:setDelay(delay) -> hs.timer.delayed object
--- Method
--- Changes the callback countdown duration
---
--- Parameters:
--- * None
---
--- Returns:
--- * the delayed timer object
---
--- Notes:
--- * if the callback countdown is running, calling this method will restart it
--- hs.timer.delayed:nextTrigger() -> number or nil
--- Method
--- Returns the time left in the callback countdown
---
--- Parameters:
--- * None
---
--- Returns:
--- * if the callback countdown is running, returns the number of seconds until it triggers; otherwise returns nil
--- hs.timer.delayed.new(delay, fn) -> hs.timer.delayed object
--- Constructor
--- Creates a new delayed timer.
---
--- Delayed timers have specialized methods that behave differently from regular timers.
--- When the `:start()` method is invoked, the timer will wait for `delay` seconds before calling `fn()`;
--- this is referred to as the callback countdown. If `:start()` is invoked again before `delay` has elapsed,
--- the countdown starts over again.
---
--- You can use a delayed timer to coalesce processing of unpredictable asynchronous events into a single
--- callback; for example, if you have an event stream that happens in "bursts" of dozens of events at once,
--- set an appropriate `delay` to wait for things to settle down, and then your callback will run just once.
---
--- Parameters:
--- * delay - number of seconds to wait for after a `:start()` invocation (the "callback countdown")
--- * fn - a function to call after `delay` has fully elapsed without any further `:start()` invocations
---
--- Returns:
--- * a new `hs.timer.delayed` object
---
--- Notes:
--- * these timers are meant to be long-lived: once instantiated, there's no way to remove them from the run loop; create them once at the module level.
local DISTANT_FUTURE=315360000 -- 10 years (roughly)
module.delayed = {
new=function(delay,fn)
local tmr=module.new(DISTANT_FUTURE,fn):start()
return {
start=function(self,dl) tmr:setNextTrigger(dl or delay) return self end,
stop=function(self) tmr:setNextTrigger(DISTANT_FUTURE) return self end,
nextTrigger=function() local nt=tmr:nextTrigger() return (nt>0 and nt<=delay) and nt or nil end,
running=function(self) return self:nextTrigger() and true or false end,
setDelay=function(self,dl) if self:running() then delay=dl self:start() end delay=dl return self end,
}
end
}
-- Return Module Object --------------------------------------------------
return module