-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathinit.lua
272 lines (228 loc) · 7.29 KB
/
init.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
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
-- Hammerspoon configuration, heavily influenced by sdegutis default configuration
require "pomodoor"
require "homebrew"
-- init grid
hs.grid.MARGINX = 5
hs.grid.MARGINY = 5
hs.grid.GRIDWIDTH = 7
hs.grid.GRIDHEIGHT = 3
-- disable animation
hs.window.animationDuration = 0
-- hs.hints.style = "vimperator"
-- hotkey mash
local mash = {"ctrl", "cmd"}
-- local mash_app = {"cmd", "alt", "ctrl"}
local mash_app = {"shift", "ctrl"}
local mash_shift = {"shift", "ctrl", "cmd"}
local mash_test = {"ctrl", "shift"}
-- Hyper key, for the Kinesis360 CAPS
local HYPER = {"shift", "ctrl", "alt", "cmd"}
local watcher = hs.window.filter.new()
local function snap_to_grid(window)
-- Snap a window to the grid
if window and window:isStandard() and window:isVisible() then
hs.grid.snap(window)
end
end
local function snap_all_windows()
-- Snap all the windows to the grid
local windows = hs.window.visibleWindows()
for _, window in ipairs(windows) do
snap_to_grid(window)
end
end
local function init_watcher()
-- Init the automatic snapper
watcher:subscribe(hs.window.filter.windowCreated, snap_to_grid)
watcher:subscribe(hs.window.filter.windowFocused, snap_to_grid)
-- watcher:start()
end
--------------------------------------------------------------------------------
local appCuts = {
q = 'Twitter',
w = 'Whatsapp',
e = 'Finder',
r = 'Cronometer',
t = 'iterm',
a = 'Notion',
s = 'Spotify',
d = 'Cron',
f = 'Firefox',
g = 'Gmail',
z = 'Double Commander',
x = 'Microsoft Edge',
c = 'Google Chrome',
v = 'Visual Studio Code',
b = 'BingGPT'
}
-- Display Help
local function display_help()
local t = {}
table.insert(t, "Keyboard shortcuts\n")
table.insert(t, "--------------------\n")
for key, app in pairs(appCuts) do
local str = "Control + CMD + " .. key .. "\t :\t" .. app .. "\n"
-- hs.alert.show(str)
table.insert(t, str)
end
local concat_t = table.concat(t)
hs.alert.show(concat_t, 2)
end
-- snap all newly launched windows
local function auto_tile(appName, event)
if event == hs.application.watcher.launched then
local app = hs.appfinder.appFromName(appName)
-- protect against unexpected restarting windows
if app == nil then
return
end
hs.fnutils.map(app:allWindows(), hs.grid.snap)
end
end
-- Moves all windows outside the view into the curent view
local function rescue_windows()
local screen = hs.screen.mainScreen()
local screenFrame = screen:fullFrame()
local wins = hs.window.visibleWindows()
for i, win in ipairs(wins) do
local frame = win:frame()
if not frame:inside(screenFrame) then
win:moveToScreen(screen, true, true)
end
end
end
local function init_wm_binding()
hs.hotkey.bind(mash_app, '/', function()
display_help()
end)
-- global operations
hs.hotkey.bind(mash, ';', function()
hs.grid.snap(hs.window.focusedWindow())
end)
hs.hotkey.bind(HYPER, "G", function()
hs.fnutils.map(snap_all_windows, hs.grid.snap)
end)
-- adjust grid size
hs.hotkey.bind(mash, '=', function()
hs.grid.adjustWidth(1)
end)
hs.hotkey.bind(mash, '-', function()
hs.grid.adjustWidth(-1)
end)
hs.hotkey.bind(mash, ']', function()
hs.grid.adjustHeight(1)
end)
hs.hotkey.bind(mash, '[', function()
hs.grid.adjustHeight(-1)
end)
--
-- change focus
hs.hotkey.bind(mash_shift, 'H', function()
hs.window.focusedWindow():focusWindowWest()
end)
hs.hotkey.bind(mash_shift, 'L', function()
hs.window.focusedWindow():focusWindowEast()
end)
hs.hotkey.bind(mash_shift, 'K', function()
hs.window.focusedWindow():focusWindowNorth()
end)
hs.hotkey.bind(mash_shift, 'J', function()
hs.window.focusedWindow():focusWindowSouth()
end)
hs.hotkey.bind(mash, 'M', hs.grid.maximizeWindow)
-- multi monitor
hs.hotkey.bind(mash, 'N', hs.grid.pushWindowNextScreen)
hs.hotkey.bind(mash, 'P', hs.grid.pushWindowPrevScreen)
-- move windows
hs.hotkey.bind(mash, 'H', hs.grid.pushWindowLeft)
hs.hotkey.bind(mash, 'J', hs.grid.pushWindowDown)
hs.hotkey.bind(mash, 'K', hs.grid.pushWindowUp)
hs.hotkey.bind(mash, 'L', hs.grid.pushWindowRight)
hs.hotkey.bind(mash, 'R', function()
rescue_windows()
end)
-- resize windows
hs.hotkey.bind(mash, 'Y', hs.grid.resizeWindowThinner)
hs.hotkey.bind(mash, 'U', hs.grid.resizeWindowShorter)
hs.hotkey.bind(mash, 'I', hs.grid.resizeWindowTaller)
hs.hotkey.bind(mash, 'O', hs.grid.resizeWindowWider)
-- Window Hints
-- hs.hotkey.bind(mash, '.', function() hs.hints.windowHints(hs.window.allWindows()) end)
hs.hotkey.bind(mash, '.', hs.hints.windowHints)
-- pomodoro key binding
hs.hotkey.bind(mash, '9', function()
pom_enable()
end)
hs.hotkey.bind(mash, '0', function()
pom_disable()
end)
hs.hotkey.bind(mash_shift, '0', function()
pom_reset_work()
end)
end
local function toggle_app(app)
-- Minimize the window if the focused window is the same as the launched window
-- This is done to easliy pop and hide an application
-- If the focused app is the one with assigned shortcut, hide it
local front_app = hs.application.frontmostApplication()
local app_name = app:lower()
if app ~= nil then
local title = front_app:name():lower()
if title ~= nil then
-- Check both ways, the naming conventions of the title are not consistent
if string.find(title, app_name) or string.find(app_name, title) then
front_app:hide()
return
end
end
end
hs.application.launchOrFocus(app)
end
-- Init Launch applications bindings
local function init_app_binding()
for key, app in pairs(appCuts) do
-- hs.hotkey.bind(mash_app, key, function () hs.application.launchOrFocus(app) end)
hs.hotkey.bind(mash_app, key, function()
toggle_app(app)
end)
end
end
local function init_custom_binding()
hs.hotkey.bind(HYPER, "ESCAPE", function()
toggle_app("Activity Monitor")
end)
end
local function increase_brightness()
-- Get the currently focused screen
local screen = hs.screen.mainScreen()
local brightness = hs.screen.getBrightness()
local target_brightness = math.min(brightness + 0.1, 1)
ha.screen.setBrightness(target_brightness)
end
local function decrease_brightness()
local screen = hs.screen.mainScreen()
local brightness = hs.screen.getBrightness()
local target_brightness = math.max(brightness - 0.1, 0)
hs.screen.setBrightness(target_brightness)
end
local function init_brigheness()
hs.hotkey.bind(HYPER, "up", function()
increase_brightness()
end)
hs.hotkey.bind(HYPER, "down", function()
decrease_brightness()
end)
end
local function init()
-- Load Spoons
hs.loadSpoon("RoundedCorners")
spoon.RoundedCorners:start()
init_wm_binding()
init_app_binding()
init_custom_binding()
init_watcher()
-- init_brigheness() -- Doesn't work on my externl Dell display
-- start app launch watcher
-- hs.application.watcher.new(auto_tile):start()
end
init()