-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathauto_reload.lua
More file actions
137 lines (104 loc) · 2.8 KB
/
auto_reload.lua
File metadata and controls
137 lines (104 loc) · 2.8 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
local _M = {}
_M.name = "auto_reload"
_M.description = "lua文件变动自动reload, 使实时生效"
local log = hs.logger.new("reload")
local reload_delay_seconds = 0.25
local reload_notification_settings_key = "auto_reload.pending_notification"
local started = false
local reload_timer = nil
local function persist_reload_notification()
if type(hs.settings) ~= "table" or type(hs.settings.set) ~= "function" then
return
end
hs.settings.set(reload_notification_settings_key, true)
end
local function flush_reload_notification()
if type(hs.settings) ~= "table" or type(hs.settings.get) ~= "function" then
return
end
if hs.settings.get(reload_notification_settings_key) ~= true then
return
end
if type(hs.settings.clear) == "function" then
hs.settings.clear(reload_notification_settings_key)
elseif type(hs.settings.set) == "function" then
hs.settings.set(reload_notification_settings_key, false)
end
hs.alert.show("hammerspoon reloaded")
end
local function stop_reload_timer()
if reload_timer == nil then
return
end
reload_timer:stop()
reload_timer = nil
end
local function schedule_reload(reason)
stop_reload_timer()
reload_timer = hs.timer.doAfter(reload_delay_seconds, function()
reload_timer = nil
log.i("reload hammerspoon config: " .. tostring(reason or "file change"))
persist_reload_notification()
hs.reload()
end)
end
local function is_recursive_scan_event(flags)
if type(flags) ~= "table" then
return false
end
return flags.mustScanSubDirs == true or flags.rootChanged == true
end
local function is_relevant_lua_change(path, flags)
if is_recursive_scan_event(flags) then
return true
end
if type(path) ~= "string" or path:sub(-4) ~= ".lua" then
return false
end
if type(flags) ~= "table" then
return true
end
if flags.itemIsFile ~= true and flags.itemRenamed ~= true then
return false
end
return flags.itemCreated == true or flags.itemRemoved == true or flags.itemRenamed == true or flags.itemModified == true
end
local function reload(paths, flag_tables)
for index, path in ipairs(paths or {}) do
if is_relevant_lua_change(path, flag_tables and flag_tables[index]) then
schedule_reload(path)
return
end
end
end
function _M.start()
if started == true then
return true
end
if _M.watcher == nil then
_M.watcher = hs.pathwatcher.new(hs.configdir, reload)
end
if _M.watcher == nil then
log.i("failed to create auto reload watcher")
return false
end
local ok, started_watcher = pcall(function()
return _M.watcher:start()
end)
if ok ~= true or started_watcher == false then
log.i("failed to start auto reload watcher")
return false
end
started = true
flush_reload_notification()
return true
end
function _M.stop()
stop_reload_timer()
if _M.watcher ~= nil then
_M.watcher:stop()
end
started = false
return true
end
return _M