-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.lua
More file actions
403 lines (332 loc) · 11.3 KB
/
init.lua
File metadata and controls
403 lines (332 loc) · 11.3 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
-- Load the IPC module for command line tool support
require("hs.ipc")
-- Install the CLI tool
hs.ipc.cliInstall()
-- Basic configuration
hs.window.animationDuration = 0
-- Reload configuration hotkey
hs.hotkey.bind({ "cmd", "alt", "ctrl" }, "R", function()
hs.reload()
end)
hs.alert.show("Config loaded")
-- Window management helpers
local TOLERANCE = 20
local function isApprox(a, b)
return math.abs(a - b) <= TOLERANCE
end
local function isLeftHalf(win, screen)
local f = win:frame()
local s = screen:frame()
return isApprox(f.x, s.x) and isApprox(f.y, s.y) and isApprox(f.w, s.w / 2) and isApprox(f.h, s.h)
end
local function isRightHalf(win, screen)
local f = win:frame()
local s = screen:frame()
return isApprox(f.x, s.x + s.w / 2) and isApprox(f.y, s.y) and isApprox(f.w, s.w / 2) and isApprox(f.h, s.h)
end
local function moveToLeftHalf(win, screen)
win:moveToScreen(screen)
local s = screen:frame()
win:setFrame({ x = s.x, y = s.y, w = s.w / 2, h = s.h })
end
local function moveToRightHalf(win, screen)
win:moveToScreen(screen)
local s = screen:frame()
win:setFrame({ x = s.x + s.w / 2, y = s.y, w = s.w / 2, h = s.h })
end
hs.hotkey.bind({ "cmd", "alt" }, "h", function()
local win = hs.window.focusedWindow()
if not win then return end
local currentScreen = win:screen()
local westScreen = currentScreen:toWest()
if isLeftHalf(win, currentScreen) and westScreen then
moveToRightHalf(win, westScreen)
else
moveToLeftHalf(win, currentScreen)
end
end)
hs.hotkey.bind({ "cmd", "alt" }, "l", function()
local win = hs.window.focusedWindow()
if not win then return end
local currentScreen = win:screen()
local eastScreen = currentScreen:toEast()
if isRightHalf(win, currentScreen) and eastScreen then
moveToLeftHalf(win, eastScreen)
else
moveToRightHalf(win, currentScreen)
end
end)
hs.hotkey.bind({ "cmd", "alt" }, "k", function()
local win = hs.window.focusedWindow()
if win then
local screen = win:screen():frame()
win:setFrame({ x = screen.x, y = screen.y, w = screen.w, h = screen.h })
end
end)
-- Move focused window to a specific Mission Control space (and follow it).
-- hs.spaces.moveWindowToSpace is broken on macOS Sequoia, so we simulate a
-- title-bar drag while switching spaces — macOS carries the window with us.
local function moveCurrentWindowToSpace(n)
local win = hs.window.focusedWindow()
if not win then return end
local screen = win:screen()
local spaces = hs.spaces.spacesForScreen(screen)
if not (spaces and spaces[n]) then
hs.alert.show("No desktop " .. n)
return
end
local zoom = win:zoomButtonRect()
if not zoom then return end
local clickPoint = {
x = zoom.x + zoom.w + 40,
y = zoom.y + zoom.h / 2,
}
local mouseOrigin = hs.mouse.absolutePosition()
hs.eventtap.event.newMouseEvent(hs.eventtap.event.types.leftMouseDown, clickPoint):post()
hs.timer.usleep(50000)
hs.spaces.gotoSpace(spaces[n])
hs.timer.doAfter(0.6, function()
hs.eventtap.event.newMouseEvent(hs.eventtap.event.types.leftMouseUp, clickPoint):post()
hs.mouse.absolutePosition(mouseOrigin)
end)
end
hs.hotkey.bind({ "cmd", "shift" }, "1", function() moveCurrentWindowToSpace(1) end)
hs.hotkey.bind({ "cmd", "shift" }, "2", function() moveCurrentWindowToSpace(2) end)
-- Fuzzy matching function
local function fuzzyMatch(str, pattern)
if pattern == "" then return true end
local strLower = string.lower(str)
local patternLower = string.lower(pattern)
local strIdx = 1
local patternIdx = 1
while strIdx <= #strLower and patternIdx <= #patternLower do
if string.sub(strLower, strIdx, strIdx) == string.sub(patternLower, patternIdx, patternIdx) then
patternIdx = patternIdx + 1
end
strIdx = strIdx + 1
end
return patternIdx > #patternLower
end
-- Track tab focus times for sorting
local tabFocusTimes = {}
local function makeTabKey(windowId, tabIndex)
return string.format("%d:%d", windowId, tabIndex)
end
-- Create window filter once for better performance
local ghosttyWindowFilter = hs.window.filter.new(false):setAppFilter("Ghostty")
-- Chooser-based Ghostty tab switcher
local allGhosttyTabs = {}
local ghosttyChooser = hs.chooser.new(function(choice)
if not choice then return end
local tabKey = makeTabKey(choice.windowId, choice.tabIndex)
tabFocusTimes[tabKey] = os.time()
selectGhosttyTab(choice.windowId, choice.tabIndex)
end)
ghosttyChooser:queryChangedCallback(function(query)
if query == "" then
ghosttyChooser:choices(allGhosttyTabs)
return
end
local filtered = {}
for _, choice in ipairs(allGhosttyTabs) do
local searchText = choice.text .. " " .. choice.subText
if fuzzyMatch(searchText, query) then
table.insert(filtered, choice)
end
end
ghosttyChooser:choices(filtered)
end)
function showGhosttyChooser()
local now = os.time()
-- Get Ghostty app
local ghostty = hs.application.find("Ghostty")
if not ghostty then
hs.alert.show("Ghostty not running")
return
end
-- Get all Ghostty windows (including minimized)
local allGhosttyWindows = ghostty:allWindows()
-- Create a map of window IDs to their focus order
local focusOrder = {}
for i, window in ipairs(hs.window.orderedWindows()) do
focusOrder[window:id()] = i
end
-- Sort Ghostty windows by focus order (minimized windows will have no order, so put them last)
table.sort(allGhosttyWindows, function(a, b)
local orderA = focusOrder[a:id()] or math.huge
local orderB = focusOrder[b:id()] or math.huge
return orderA < orderB
end)
local sortedWindows = allGhosttyWindows
if #sortedWindows == 0 then
hs.alert.show("No Ghostty windows found")
return
end
allGhosttyTabs = {}
-- First pass: collect all windows and their tabs
local windowsToShow = {} -- Array of {windowId, tabs=[...]}
local childWindowTitles = {} -- Set of window titles that are children/tabs
local parentWindows = {} -- Set of window IDs that have tab groups
-- Iterate through windows in focus order
for _, window in ipairs(sortedWindows) do
local windowId = window:id()
local windowTitle = window:title()
-- Get tabs for this window
local success, windowTabs = pcall(function()
local tabs = {}
local axWindow = hs.axuielement.windowElement(window)
if axWindow then
local children = axWindow:attributeValue("AXChildren")
local foundTabGroup = false
if children then
for _, child in ipairs(children) do
if child:attributeValue("AXRole") == "AXTabGroup" then
foundTabGroup = true
local axTabs = child:attributeValue("AXChildren")
if axTabs then
parentWindows[windowId] = true
for j, tab in ipairs(axTabs) do
local tabTitle = tab:attributeValue("AXTitle")
local tabSelected = tab:attributeValue("AXSelected")
if tabTitle then
-- Mark this tab title as a child so we can filter out matching windows
childWindowTitles[tabTitle] = true
local tabKey = makeTabKey(windowId, j)
local focusTime = tabFocusTimes[tabKey] or 0
-- Currently selected tabs get a recent time boost
if tabSelected then
tabFocusTimes[tabKey] = now
focusTime = now
end
table.insert(tabs, {
text = tabTitle,
subText = string.format("Window: %s | Tab %d", windowTitle, j),
windowId = windowId,
tabIndex = j,
focusTime = focusTime,
})
end
end
end
break
end
end
end
-- If no tab group found, treat the window as a single tab
if not foundTabGroup then
local tabKey = makeTabKey(windowId, 1)
tabFocusTimes[tabKey] = now
table.insert(tabs, {
text = windowTitle,
subText = string.format("Window: %s | Tab 1", windowTitle),
windowId = windowId,
tabIndex = 1,
focusTime = now,
})
end
end
return tabs
end)
-- Store window info for second pass
if success and windowTabs and #windowTabs > 0 then
-- Sort tabs within this window by focus time
table.sort(windowTabs, function(a, b)
return a.focusTime > b.focusTime
end)
table.insert(windowsToShow, {
windowId = windowId,
windowTitle = windowTitle,
tabs = windowTabs,
})
end
end
-- Second pass: filter out child windows and build final list
for _, windowInfo in ipairs(windowsToShow) do
-- Skip if this window is a parent (has tab group) but all its tabs are also individual windows
-- OR if this window's title matches a child tab title
local isParent = parentWindows[windowInfo.windowId]
local titleIsChild = false
-- Check if any tab in this window has a title that's marked as a child
for _, tab in ipairs(windowInfo.tabs) do
if childWindowTitles[tab.text] then
titleIsChild = true
break
end
end
if isParent then
-- This is a parent window with tab group - include it
for _, tab in ipairs(windowInfo.tabs) do
table.insert(allGhosttyTabs, tab)
end
elseif not titleIsChild then
-- This is a standalone window that isn't a child tab - include it
for _, tab in ipairs(windowInfo.tabs) do
table.insert(allGhosttyTabs, tab)
end
end
end
if #allGhosttyTabs == 0 then
hs.alert.show("No Ghostty tabs found")
return
end
ghosttyChooser:choices(allGhosttyTabs)
ghosttyChooser:show()
end
-- Bind hotkey for chooser-based tab switcher
hs.hotkey.bind({ "cmd" }, "p", function()
showGhosttyChooser()
end)
-- Select a specific Ghostty tab
function selectGhosttyTab(windowId, tabIndex)
local ghostty = hs.application.find("Ghostty")
if not ghostty then
return false
end
-- Find the window by ID
local targetWindow = nil
for _, window in ipairs(ghostty:allWindows()) do
if window:id() == windowId then
targetWindow = window
break
end
end
if not targetWindow then
return false
end
-- Focus the window first
targetWindow:focus()
-- Find and click the tab (if there's a tab group)
local success, result = pcall(function()
local axWindow = hs.axuielement.windowElement(targetWindow)
if axWindow then
local children = axWindow:attributeValue("AXChildren")
local foundTabGroup = false
if children then
for _, child in ipairs(children) do
local role = child:attributeValue("AXRole")
if role == "AXTabGroup" then
foundTabGroup = true
local tabs = child:attributeValue("AXChildren")
if tabs and tabs[tabIndex] then
-- Click the tab
tabs[tabIndex]:performAction("AXPress")
return true
end
break
end
end
end
-- If no tab group found, this is a single-tab window
-- Just focusing the window is sufficient
if not foundTabGroup then
return true
end
end
return false
end)
if success and result then
return true
else
return false
end
end