[hammerspoon] Quitter updates

Make Quitter more performant - don't iterate over all running
applications, only ones that we intend on quitting.

Re-enable quitting Discord.
pull/37/head
Alpha Chen 3 years ago
parent a814454fd4
commit bef2ae3878

@ -1,6 +1,6 @@
--- === Quitter === --- === Quitter ===
-- --
-- Quits configured apps if they have not been used since a specified amount of time. -- Quits apps if they have not been used since a specified amount of time.
local obj = {} local obj = {}
obj.__index = obj obj.__index = obj
@ -69,39 +69,45 @@ function obj:reset()
local name = app:name() local name = app:name()
local duration = self.quitAppsAfter[name] local duration = self.quitAppsAfter[name]
if not duration then return false end if duration == nil then return false end
self.lastFocused[name] = os.time() self.lastFocused[name] = os.time()
end) end)
end end
function obj:reap() function obj:shouldQuit(app)
self.logger.d("reaping") if app == nil then return false end
local appsToQuit = hs.fnutils.ifilter(hs.application.runningApplications(), function(app)
local appName = app:name()
-- Don't quit the currently focused app -- Don't quit the currently focused app
if app:isFrontmost() then return false end if app:isFrontmost() then return false end
local duration = self.quitAppsAfter[appName] local duration = self.quitAppsAfter[app:name()]
if not duration then return false end if duration == nil then return false end
local lastFocused = self.lastFocused[appName] local lastFocused = self.lastFocused[app:name()]
if not lastFocused then return false end if lastFocused == nil then return false end
self.logger.d("app: " .. appName .. " last focused at " .. lastFocused) self.logger.d("app: " .. app:name() .. " last focused at " .. lastFocused)
return (os.time() - lastFocused) > duration return (os.time() - lastFocused) > duration
end) end
function obj:reap()
self.logger.d("reaping")
for _, app in pairs(appsToQuit) do local frontmostApp = hs.application.frontmostApplication()
hs.notify.new({ for appName, _ in pairs(self.quitAppsAfter) do
title = "Hammerspoon", local app = hs.application.get(appName)
informativeText = "Quitting " .. app:name(),
withdrawAfter = 2, if self:shouldQuit(app) then
}):send() hs.notify.new({
app:kill() title = "Hammerspoon",
self.lastFocused[app:name()] = nil informativeText = "Quitting " .. app:name(),
withdrawAfter = 2,
}):send()
app:kill()
self.lastFocused[app:name()] = nil
end
end end
end end

@ -2,7 +2,7 @@
(set hs.window.animationDuration 0.0) (set hs.window.animationDuration 0.0)
(hs.loadSpoon "MiroWindowsManager") (hs.loadSpoon "MiroWindowsManager")
(set spoon.MiroWindowsManager.fullScreenSizes [1]) ;; only fullscreen ;; (set spoon.MiroWindowsManager.fullScreenSizes [1 (/ 4 3) 2]) ;; only fullscreen
(let [mash [:cmd :alt :ctrl] (let [mash [:cmd :alt :ctrl]
smash [:cmd :alt :ctrl :shift]] smash [:cmd :alt :ctrl :shift]]
(spoon.MiroWindowsManager:bindHotkeys { (spoon.MiroWindowsManager:bindHotkeys {
@ -43,7 +43,7 @@
(hs.loadSpoon "Quitter") (hs.loadSpoon "Quitter")
(set spoon.Quitter.quitAppsAfter { (set spoon.Quitter.quitAppsAfter {
;; :Discord 300 :Discord 300
:Flotato 300 :Flotato 300
:MailMate 300 :MailMate 300
:Messages 300 :Messages 300

Loading…
Cancel
Save