-
Notifications
You must be signed in to change notification settings - Fork 0
/
spaceCleaner.lua
45 lines (36 loc) · 1.08 KB
/
spaceCleaner.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
local SpaceCleaner = {}
SpaceCleaner.__index = SpaceCleaner
local log = hs.logger.new("SpaceCleaner", 'debug')
function SpaceCleaner:new()
local obj = {}
setmetatable(obj, self)
return obj
end
function SpaceCleaner:init(spoon)
log.df("SpaceCleaner:init")
-- keep a reference to spoon's API and Yabai client
self.spoon = spoon
self.client = spoon.client
-- register our event handler
spoon:registerOnSpacesChangedCB(function() self:onEvent() end)
-- fire initial event to click things off
self:onEvent()
end
-- Find any spaces with zero windows and destroy them, unless the space is
-- is currently focused.
function SpaceCleaner:onEvent()
log.df("SpaceCleaner:onEvent")
local spaces = self.client:getSpaces(false)
local windows = self.client:getWindows()
local seen_spaces = {}
for _, win in ipairs(windows) do
seen_spaces[win.space] = true
end
for _, space in ipairs(spaces) do
if not seen_spaces[space.index] and not space['has-focus'] then
log.df("SpaceCleaner: space %d is empty", space.index)
self.client:destroySpace(space)
end
end
end
return SpaceCleaner