-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.lua
More file actions
executable file
·302 lines (264 loc) · 9.05 KB
/
init.lua
File metadata and controls
executable file
·302 lines (264 loc) · 9.05 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
local log = hs.logger.new('init', 'verbose')
local hyper = { 'shift', 'cmd', 'alt', 'ctrl' }
----------------------------------
-- __ __________ ____ __________ _____
-- / / / / ____/ / / __ \/ ____/ __ \/ ___/
-- / /_/ / __/ / / / /_/ / __/ / /_/ /\__ \
-- / __ / /___/ /___/ ____/ /___/ _, _/___/ /
-- /_/ /_/_____/_____/_/ /_____/_/ |_|/____/
--
-- Wrap code in "if running" block
function ifRunning(application, code)
-- If code is not given, we should find a way to return a boolean
if code then
return 'if application "' .. application .. '" is running then ' .. code .. ' end if'
end
end
function indexOf(table, value, valName)
for k, v in ipairs(table) do
if valName then
if v[valName] == value[valName] then return k end
else
if v == value then return k end
end
end
end
function openApplication(name)
hs.application.launchOrFocus(name)
end
function secondsToClock(seconds)
local seconds = tonumber(seconds)
if seconds <= 0 then
return "00:00:00";
else
hours = string.format("%02.f", math.floor(seconds/3600));
mins = string.format("%02.f", math.floor(seconds/60 - (hours*60)));
secs = string.format("%02.f", math.floor(seconds - hours*3600 - mins *60));
return hours..":"..mins..":"..secs
end
end
----------------------------------
-- _ _______ ______ ____ _ _______
-- | | / / _/ | / / __ \/ __ \ | / / ___/
-- | | /| / // // |/ / / / / / / / | /| / /\__ \
-- | |/ |/ // // /| / /_/ / /_/ /| |/ |/ /___/ /
-- |__/|__/___/_/ |_/_____/\____/ |__/|__//____/
--
-- Flip through current applications windows
function windowFlipper(direction)
local win = hs.window.focusedWindow()
local app = win:application()
local windows = app:allWindows()
local window
local index = indexOf(windows, win)
log.f('windows: %s, win: %s, index: %d', windows[3], win, index)
if direction == 'next' then
window = windows[#windows]
log.f('next')
else
log.f('prev')
-- This is not working at all
window = windows[#windows - 2]
end
local title = window:title()
window:focus()
hs.alert.show( title )
end
hs.hotkey.bind({ 'alt' }, 'Tab', function()
windowFlipper('next')
end)
hs.hotkey.bind( {'shift', 'alt' }, 'Tab', function()
windowFlipper('prev')
end)
-- Open or focus applications with keyboard shortcuts
hs.hotkey.bind(hyper, 'n', function()
openApplication('nvAlt')
end)
-- Send 'ctrl+c - arrow up - enter' to restart a long-running command in the terminal.
-- (Uses the 'releasefn' argument to only send ctrl and not the full hyper combo)
hs.hotkey.bind(hyper, 'q', nil, function()
hs.eventtap.keyStroke({ 'ctrl' }, 'c')
hs.eventtap.keyStroke(nil, 'up')
hs.eventtap.keyStroke(nil, 'return')
end)
----------------------------------
-- ___ __ ______ ________
-- / | / / / / __ \/ _/ __ \
-- / /| |/ / / / / / // // / / /
-- / ___ / /_/ / /_/ // // /_/ /
-- /_/ |_\____/_____/___/\____/
--
local spotifyVolume
local oldSpotifyVolume
ok, spotifyVolume = hs.applescript(ifRunning('Spotify', 'tell application "Spotify" to sound volume as string'))
if not ok then
spotifyVolume = 100
end
hs.hotkey.bind(hyper, 'f4', function()
hs.spotify.displayCurrentTrack()
end )
hs.hotkey.bind(hyper, 'f5', function()
hs.spotify.rw()
hs.alert.show(secondsToClock(hs.spotify.getPosition()))
end )
hs.hotkey.bind(hyper, 'f6', function()
hs.spotify.ff()
hs.alert.show(secondsToClock(hs.spotify.getPosition()))
end )
hs.hotkey.bind(hyper, 'f7', function()
hs.spotify.previous()
end )
hs.hotkey.bind(hyper, 'f8', function()
hs.spotify.playpause()
end )
hs.hotkey.bind(hyper, 'f9', function()
hs.spotify.next()
end )
hs.hotkey.bind(hyper, 'f10', function()
if hs.audiodevice.current().name == 'DisplayPort' then
-- ok, spotifyVolume = hs.applescript('tell application "Spotify" to sound volume as string')
-- if spotifyVolume == 0 then
-- hs.applescript( 'tell application "Spotify" to set sound volume to ' .. oldSpotifyVolume )
-- else
-- oldSpotifyVolume = spotifyVolume
-- spotifyVolume = 0
-- hs.applescript( 'tell application "Spotify" to set sound volume to 0' )
-- end
else
if hs.audiodevice.current().muted then
hs.audiodevice.defaultOutputDevice():setMuted( false )
else
hs.audiodevice.defaultOutputDevice():setMuted( true )
end
end
end )
hs.hotkey.bind(hyper, 'f11', function()
if hs.audiodevice.current().name == 'DisplayPort' then
-- DisplayPort has no volume, try changing Spotify's volume
hs.applescript( 'tell application "Spotify" to set sound volume to (sound volume - 10)' )
else
hs.audiodevice.defaultOutputDevice():setVolume( hs.audiodevice.current().volume - 10 )
end
end )
hs.hotkey.bind(hyper, 'f12', function()
if hs.audiodevice.current().name == 'DisplayPort' then
-- DisplayPort has no volume, try changing Spotify's volume
hs.applescript( 'tell application "Spotify" to set sound volume to (sound volume + 10)' )
else
hs.audiodevice.defaultOutputDevice():setVolume( hs.audiodevice.current().volume + 10 )
end
end )
----------------------------------
-- _____ ____ ____ ____ _ _______
-- / ___// __ \/ __ \/ __ \/ | / / ___/
-- \__ \/ /_/ / / / / / / / |/ /\__ \
-- ___/ / ____/ /_/ / /_/ / /| /___/ /
-- /____/_/ \____/\____/_/ |_//____/
--
-- Load some spoons
hs.loadSpoon('SpoonInstall')
Install = spoon.SpoonInstall
Install:andUse('Caffeine', {
disable = false,
hotkeys = {
toggle = { hyper, '8' },
},
start = true,
})
Install:andUse('ClipboardTool', {
disable = false,
hotkeys = {
toggle_clipboard = { hyper, 'c' },
clear_history = { hyper, 'x' },
},
config = {
show_in_menubar = true,
menubar_title = '✂',
},
start = true,
})
Install:andUse('WinWin', {
config = {
gridparts = 18,
}
})
if spoon.WinWin then
-- Snap window to grid
hs.hotkey.bind(hyper, 'pad7', function() spoon.WinWin:moveAndResize('cornerNW') end)
hs.hotkey.bind(hyper, 'pad8', function() spoon.WinWin:moveAndResize('halfup') end)
hs.hotkey.bind(hyper, 'pad9', function() spoon.WinWin:moveAndResize('cornerNE') end)
hs.hotkey.bind(hyper, 'pad4', function() spoon.WinWin:moveAndResize('halfleft') end)
hs.hotkey.bind(hyper, 'pad5', function() spoon.WinWin:moveAndResize('center') end)
hs.hotkey.bind(hyper, 'pad6', function() spoon.WinWin:moveAndResize('halfright') end)
hs.hotkey.bind(hyper, 'pad1', function() spoon.WinWin:moveAndResize('cornerSW') end)
hs.hotkey.bind(hyper, 'pad2', function() spoon.WinWin:moveAndResize('halfdown') end)
hs.hotkey.bind(hyper, 'pad3', function() spoon.WinWin:moveAndResize('cornerSE') end)
-- Increas/decreas size
hs.hotkey.bind(hyper, 'pad+', function() spoon.WinWin:moveAndResize('expand') end)
hs.hotkey.bind(hyper, 'pad-', function() spoon.WinWin:moveAndResize('shrink') end)
-- Maximize for small keyboard
hs.hotkey.bind(hyper, 'm', function() spoon.WinWin:moveAndResize('maximize') end)
-- -- Move window
hs.hotkey.bind(hyper, 'j', function() spoon.WinWin:stepMove('left') end)
hs.hotkey.bind(hyper, 'l', function() spoon.WinWin:stepMove('right') end)
hs.hotkey.bind(hyper, 'i', function() spoon.WinWin:stepMove('up') end)
hs.hotkey.bind(hyper, 'k', function() spoon.WinWin:stepMove('down') end)
-- -- Resize window
hs.hotkey.bind(hyper, 'left', function() spoon.WinWin:smartStepResize('left') end)
hs.hotkey.bind(hyper, 'right', function() spoon.WinWin:smartStepResize('right') end)
hs.hotkey.bind(hyper, 'up', function() spoon.WinWin:smartStepResize('up') end)
hs.hotkey.bind(hyper, 'down', function() spoon.WinWin:smartStepResize('down') end)
-- -- Move window between screens
hs.hotkey.bind(hyper, 'home', function() spoon.WinWin:moveToScreen('left') end )
hs.hotkey.bind(hyper, 'end', function() spoon.WinWin:moveToScreen('right') end )
end
Install:andUse('PasswordGenerator', {
hotkeys = {
paste = { hyper, 'p' }
},
config = {
password_length = 32,
},
})
-- Reload config on file change
-- and when pressing Hyper + r
Install:andUse('ReloadConfiguration', {
hotkeys = {
reloadConfiguration = { hyper, 'r' }
},
start = true,
})
-- Show a list of HTTP statuses
-- when pressing hyper + h
Install:andUse('HttpStatus', {
hotkeys = {
show = { hyper, 'h' }
},
})
-- Show a list of Unicode Symbols and
-- Dingbats when pressing hyper + s
Install:andUse('Symbats', {
hotkeys = {
show = { hyper, 's' }
},
})
-- Optionally load a local file with per-machine settings
-- and/or secret API keys etc.
local localfile = hs.configdir .. '/init-local.lua'
if hs.fs.attributes(localfile) then
dofile(localfile)
end
----------------------------------
-- __ __________________
-- / |/ / ____/_ __/ |
-- / /|_/ / __/ / / / /| |
-- / / / / /___ / / / ___ |
-- /_/ /_/_____/ /_/ /_/ |_|
--
-- All loaded, let user know
hs.alert.show('Config loaded')
-----------------
-- Places to watch:
-- https://github.com/Hammerspoon/hammerspoon/wiki/Sample-Configurations
-- https://github.com/cmsj/hammerspoon-config/blob/master/init.lua
-- https://github.com/trishume/dotfiles/tree/master/hammerspoon/hammerspoon.symlink