-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
128 lines (105 loc) · 3.57 KB
/
init.lua
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
--- === Hazel ===
---
--- Replace tool Hazel
local obj = { __gc = true }
obj.__gc = function(t)
t:stop()
end
-- Metadata
obj.name = "Hazel"
obj.version = "1.0"
obj.author = "northleaf"
obj.homepage = "https://github.com/northleafup/Hazel.spoon"
obj.license = "MIT - https://opensource.org/licenses/MIT"
obj.waitTime = 10 -- seconds to wait after file changed, before running rules
obj.time_scheduled = 60 * 60
-- Internal function used to find our location, so we know where to load files from
local function script_path()
local str = debug.getinfo(2, "S").source:sub(2)
return str:match("(.*/)")
end
obj.spoonPath = script_path()
dofile(obj.spoonPath.."/util.lua")
--------------------
-- paths --
--------------------
obj.paths = {}
obj.paths.base = os.getenv('HOME')
obj.paths.hs = getPathAbsolute(obj.paths.base, '.hammerspoon')
--read user customer config
customer =dofile(getPathAbsolute(obj.paths.hs ,'private/hazel.lua'))
--if user config does not esist
if not customer then
customer = dofile(obj.spoonPath.."/hazel.lua")
end
--scheduled time
if time_scheduled then
obj.time_scheduled = time_scheduled
end
function obj:init()
obj:start()
end
local timer = nil
----------------------------------------------------------------------
----------------------------------------------------------------------
-- NOTE: Be careful not to modify a file each time it is watched.
-- This will cause the watch* function to be re-run every obj.cfg.waitTime
-- seconds, since the file gets modified each time, which triggers the
-- watch* function again.
----------------------------------------------------------------------
----------------------------------------------------------------------
-- callback for watching a given directory
-- process_cb is given a single argument that is a table consisting of:
-- {file: the full file path, parent: the file's parent directory full path,
-- filename: the basename of the file with extension, ext: the extension}
local function watchPath(path, files, process_cb)
-- wait a little while before doing anything, to give files a chance to
-- settle down.
hs.timer.doAfter(obj.waitTime, function()
-- loop through the files and call the process_cb function on any that are
-- not ignored, still exist, and are found in the given path.
for _,file in ipairs(files) do
if not ignored(file) and exists(file) then
local parent, filename, ext = splitPath(file)
local data = {file=file, parent=parent, filename=filename, ext=ext}
if parent == path then process_cb(data) end
end
end
end):start()
end
-- callback by hazel module
local function watchDirectory(srcFilePath,files,processDirectory)
watchPath(srcFilePath, files, function(data)
processDirectory(srcFiePath,data)
end)
end
local function runOnFiles(path,callback)
local files = {}
local iterFn, dirObj = hs.fs.dir(path)
if iterFn then
for file in iterFn, dirObj do
table.insert(files, getPathAbsolute(path, file))
end
else
print(string.format("The following error occurred: %s", dirObj))
end
if #files > 0 then watchDirectory(path,files,callback) end
end
local function checkPaths()
if watchContent then
for _,value in ipairs(watchContent)
do
basePath = getUserFilePathhAbsolute(value.filePath)
runOnFiles(basePath,value.action)
end
end
end
function obj.start()
timer = hs.timer.new(obj.time_scheduled, checkPaths)
timer:start()
end
function obj.stop()
if timer then timer:stop() end
timer = nil
end
return obj