-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwatcher.lua
More file actions
50 lines (40 loc) · 1.37 KB
/
watcher.lua
File metadata and controls
50 lines (40 loc) · 1.37 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
local lfs = require 'lfs'
local function ternary(condition, if_true, if_false)
return condition and if_true or if_false
end
local last_modified = 0
local function _check_modification(dir, _last_modified, callback)
local directory = ternary(string.sub(dir, #dir, #dir) == '/', dir, dir .. '/')
last_modified = _last_modified
local file
for _file in lfs.dir(directory) do
if _file ~= '..' and _file ~= '.' then
file = directory .. _file
if lfs.attributes(file, 'mode') == 'file' and
lfs.attributes(file, 'modification') > last_modified then
last_modified = lfs.attributes(file, 'modification')
callback(last_modified)
elseif lfs.attributes(file, 'mode') == 'directory' then
_check_modification(file .. '/', last_modified, callback)
end
end
end
end
local function _get_last_modified(directory)
local last = 0
_check_modification(directory, last_modified, function(_last_modified)
last = _last_modified
end)
return last
end
local function watcher(directory, callback)
local last = _get_last_modified(directory)
callback()
while true do
_check_modification(directory, last_modified, function(_last_modified)
last = _last_modified
callback()
end)
end
end
return watcher