-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
244 lines (211 loc) · 6.7 KB
/
Copy pathinit.lua
File metadata and controls
244 lines (211 loc) · 6.7 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
local secrets = require("secrets")
secrets.start("secrets.json")
mash = {"ctrl", "alt", "cmd"}
-- Logging
hs.logger.defaultLogLevel = "debug"
logger = hs.logger.new("main")
-- I can never remember how to print a table
function walk(table)
for k, v in pairs(table) do
print(k, v)
end
end
-- Spoons
hs.loadSpoon("SpoonInstall")
spoon.SpoonInstall.use_syncinstall = true
Install = spoon.SpoonInstall
-- Better looking alerts
hs.alert.defaultStyle.radius = 10
-- Disable window size/location animations
hs.window.animationDuration = 0
-- Open console
hs.hotkey.bind(mash, "Y", nil, hs.toggleConsole)
-- Window layout
local retina = "Built-in Retina Display"
local split = 0.5
local maximized = false
function external()
return hs.screen.allScreens()[2]
end
function video(app, match)
local r = {}
if app then
local comp = function(title)
-- not not to cast to bool
return not not (
-- %f[%a]: not letter followed by letter frontier pattern
-- %f[%A]: letter followed by not letter frontier pattern
string.match(title, "%f[%a]YouTube%f[%A]")
or string.match(title, "%f[%a]ITV Hub%f[%A]")
or string.match(title, "%f[%a]Nest%f[%A]")
or string.match(title, "%f[%a]Meet%f[%A]")
or string.match(title, "%f[%a]GCN+%f[%A]")
or string.match(title, "%f[%a]BBC iPlayer+%f[%A]")
)
end
local wins = hs.application.get(app):visibleWindows()
for _, w in ipairs(wins) do
if comp(w:title()) == match then
r[#r + 1] = w
end
end
end
return r
end
function is_video(app)
return video(app, true)
end
function is_not_video(app)
return video(app, false)
end
function split_left()
return hs.geometry.rect(0, 0, split, 1)
end
function split_right()
return hs.geometry.rect(split, 0, 1 - split, 1)
end
function apply_layout()
local layouts = {
[1] = { -- one screen
{"Google Chrome", nil, retina, hs.layout.maximized, nil, nil},
-- {"Firefox", nil, retina, hs.layout.maximized, nil, nil},
-- {"Safari", nil, retina, hs.layout.maximized, nil, nil},
-- {"Code", nil, retina, hs.layout.maximized, nil, nil},
{"iTerm2", nil, retina, hs.layout.maximized, nil, nil},
},
[2] = { -- two screens
{"Google Chrome", is_not_video, external, maximized and hs.layout.maximized or split_left, nil, nil},
{"Google Chrome", is_video, retina, hs.layout.maximized, nil, nil},
-- {"Firefox", is_not_video, external, maximized and hs.layout.maximized or split_left, nil, nil},
-- {"Firefox", is_video, retina, hs.layout.maximized, nil, nil},
-- {"Safari", is_not_video, external, maximized and hs.layout.maximized or split_left, nil, nil},
-- {"Safari", is_video, retina, hs.layout.maximized, nil, nil},
{"Code", nil, external, maximized and hs.layout.maximized or split_left, nil, nil},
{"iTerm2", nil, external, maximized and hs.layout.maximized or split_right, nil, nil},
-- {"Signal", nil, retina, hs.geometry.rect(0.2, 0.15, 0.6, 0.7), nil, nil},
}
}
local screens = hs.screen.allScreens()
hs.layout.apply(layouts[#screens])
end
hs.hotkey.bind(mash, "J", nil, function()
split = math.max(0, split - 0.05)
apply_layout()
end)
hs.hotkey.bind(mash, "K", nil, function()
split = math.min(1, split + 0.05)
apply_layout()
end)
hs.hotkey.bind(mash, "L", nil, function()
split = 0.5
apply_layout()
end)
hs.hotkey.bind(mash, "P", nil, function()
maximized = not maximized
apply_layout()
end)
-- Window movement
function set_frame(func)
local win = hs.window.focusedWindow()
local frame = win:frame()
local max = win:screen():frame()
func(frame, max)
win:setFrame(frame)
end
hs.hotkey.bind(mash, "M", function()
set_frame(function(f, m) f.x = m.x; f.y = m.y; f.w = m.w; f.h = m.h end)
end)
hs.hotkey.bind(mash, "Left", function()
set_frame(function(f, m) f.x = m.x; f.y = m.y; f.w = m.w / 2; f.h = m.h end)
end)
hs.hotkey.bind(mash, "Right", function()
set_frame(function(f, m) f.x = m.x + (m.w / 2); f.y = m.y; f.w = m.w / 2; f.h = m.h end)
end)
hs.hotkey.bind(mash, "Up", function()
set_frame(function(f, m) f.y = m.y; f.h = m.h / 2 end)
end)
hs.hotkey.bind(mash, "Down", function()
set_frame(function(f, m) f.y = m.y + (m.h / 2) end)
end)
-- Browser history
hs.hotkey.bind(mash, "H", nil, function()
local iterm = hs.application.get("iTerm2")
local originalWindows = {}
if iterm then
originalWindows = iterm:visibleWindows()
end
-- make a new window if iTerm isn't running or has no windows
hs.application.launchOrFocus("/Applications/iTerm.app/Contents/MacOS/iTerm2")
hs.timer.waitUntil(
function()
return hs.window.focusedWindow():application():name() == "iTerm2"
end,
function()
if #originalWindows > 0 then
-- already had some windows open so make a new one
hs.eventtap.keyStroke({"cmd"}, "N")
end
-- leave time to open the window
hs.timer.doAfter(0.05, function()
local win = hs.window.focusedWindow()
if win:screen():name() ~= retina then
win:move({0.15, 0.15, 0.7, 0.7})
else
win:maximize()
end
hs.eventtap.keyStrokes(" unset HISTFILE\n")
hs.eventtap.keyStrokes("echo -ne \"\\033]0;\"Browser history\"\\007\"; browser-history; exit\n")
end)
end,
0.05
)
end)
-- DeepL translate
local wm = hs.webview.windowMasks
Install:andUse("DeepLTranslate", {
disable = false,
config = {
popup_style = wm.utility|wm.HUD|wm.titled|wm.closable|wm.resizable,
},
hotkeys = {
translate = { mash, "E" },
}
})
-- Toggle caffeine
Install:andUse("Caffeine", {
start = true,
hotkeys = {
toggle = { mash, "C" }
}
})
-- Mute when displays sleep
local originalMuted = true
function caffeinateCallback(eventType)
local device = hs.audiodevice.defaultOutputDevice()
if device then
if (eventType == hs.caffeinate.watcher.screensDidSleep) then
originalMuted = device:muted()
device:setMuted(true)
print("Screen sleeping, muted audio (previous mute state was " .. tostring(originalMuted) .. ")")
elseif (eventType == hs.caffeinate.watcher.screensDidWake) then
device:setMuted(originalMuted)
print("Screen awake, restored mute state to " .. tostring(originalMuted))
end
end
end
caffeinateWatcher = hs.caffeinate.watcher.new(caffeinateCallback):start()
-- Automatically reload config
function reloadConfig(files)
local doReload = false
for _, file in pairs(files) do
if file:sub(-4) == ".lua" then
doReload = true
end
end
if doReload then
hs.reload()
end
end
configWatcher = hs.pathwatcher.new(os.getenv("HOME") .. "/.hammerspoon/", reloadConfig):start()
hs.hotkey.bind(mash, "R", function() hs.reload() end)
hs.alert.show("Hammerspoon config loaded")