-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
264 lines (232 loc) · 7.15 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
require('env')
require('HotkeyMappings')
-- SPOONS CONFIG
TextClipboardHistory = hs.loadSpoon("TextClipboardHistory")
TextClipboardHistory.frequency = 1
TextClipboardHistory.menubar_title = "\u{1f4be}"
TextClipboardHistory:start()
--
hs.application.enableSpotlightForNameSearches(true)
-- screen & grid config
Screens = hs.screen.allScreens()
-- globals
Spaces = hs.spaces.spacesForScreen()
--
-------------------------------------------------------
------------------- HOTKEY MAPPINGS -------------------
-------------------------------------------------------
MockingTyperToggle = false
BindResizers()
-- Bind hotkeys for all mappings in HotkeyMappings
BindHotkeys()
-------------------------------------------------------
----------------- END HOTKEY MAPPINGS -----------------
-------------------------------------------------------
function Resize(numberKey)
local number = tonumber(numberKey)
local window = hs.window.focusedWindow()
-- horizontal, vertical, width, height
window:moveToUnit(hs.geometry.unitrect(1 / number, 0, number * 0.1, number * 0.1))
print(window:size())
end
local function mapChoices(hotkeys)
table.sort(hotkeys, function(a, b) return a.name < b.name end)
local choices = {}
for i, hk in ipairs(hotkeys) do
local choice = {
["text"] = hk.name,
["subText"] = hk.key and table.concat(hk.modifier, ", ") .. " '" .. hk.key .. "'",
index = i
}
table.insert(choices, i, choice)
end
return choices
end
function HotkeyHelpMenu()
hs.chooser.new(function(choice) if choice ~= nil then HotkeyMappings[choice.index].callback() end end)
:choices(mapChoices(HotkeyMappings))
:show()
end
function MockingTyper()
MockingTyperToggle = not MockingTyperToggle
if not MockingTyperToggle then
hs.hid.capslock.set(false)
else
hs.timer.doWhile(function() return MockingTyperToggle end, hs.hid.capslock.toggle, 0.3)
end
end
function ChromeSplitTab()
local chrome = hs.application.get("Google Chrome")
chrome:selectMenuItem("Move Tab to New Window")
local windowlist = chrome:allWindows()
SplitWindow(windowlist[1], "left")
SplitWindow(windowlist[2], "right")
end
function SplitWindow(window, dir)
local places = {
left = hs.layout.left50,
right = hs.layout.right50,
-- horizontal, vertical, width, height
up = hs.geometry.unitrect(0, 0, 1, 0.5),
down = hs.geometry.unitrect(0, 0.5, 1, 0.5),
}
window:moveToUnit(places[dir])
end
-- args = {appName:string, action:string, opts:table of menu options}
function LaunchApp(args)
local app = hs.application.find(args.appName)
if app == nil then
hs.application.open(args.appName)
hs.timer.doAfter(1, function()
LaunchApp(args)
end)
print("openeing app:")
else
print(app)
app:setFrontmost()
app:selectMenuItem(args.action)
if args.opts then
for i, opt in ipairs(args.opts) do
print("selecting " .. table.concat(opt, ", "))
app:selectMenuItem(opt)
end
end
end
end
function RandomStickyColor()
local colors = { "Blue", "Green", "Pink", "Purple", "Gray" }
return colors[math.random(#colors)]
end
function SendToSpace(direction)
local currentSpace = hs.spaces.activeSpaceOnScreen()
local window = hs.window.focusedWindow()
local adjacentSpaces = {
"left",
"right"
}
for index, space in pairs(Spaces) do
if space == currentSpace then
adjacentSpaces["left"] = Spaces[index - 1]
adjacentSpaces["right"] = Spaces[index + 1]
end
end
hs.spaces.moveWindowToSpace(window, adjacentSpaces[direction])
end
function PrintTable(table)
local values = ""
for k, v in pairs(table) do
values = values .. " " .. k .. " = " .. v .. ", \n"
end
print("\n{\n" .. values .. "}")
end
function PromptSetGrid()
hs.focus()
local choice, size = hs.dialog.textPrompt("Set grid dimensions", "Enter a number", "", "OK", "Cancel")
if choice == "OK" then
local grid_size = tonumber(string.match(size, "%d"))
SetGrid(Screens, hs.geometry(nil, nil, grid_size, grid_size), hs.geometry(0, 0))
end
end
function SetGrid(screens, dimensions, margins)
for _, screen in pairs(screens) do
hs.grid.setGrid(dimensions, screen).setMargins(margins)
end
end
local 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()
print("Config reloaded")
hs.alert.show("Config reloaded")
end
end
local function sanitizeTimestamp(timestamp)
return string.gsub(timestamp, ",", "")
end
function ConvertEpochTimestamp(timestamp)
timestamp = sanitizeTimestamp(timestamp)
local length = string.len(timestamp)
local dateTime
if length < 10 then
print("invalid epoch timestamp")
return
elseif length > 10 then
print("epoch in miliseconds")
dateTime = os.date(nil, tonumber(timestamp) / 1000) -- gotta round this off because its prob returning a decimal
else
print("epoch in seconds")
dateTime = os.date(nil, tonumber(timestamp))
end
-- hs.pasteboard.setContents(dateTime)
hs.dialog.alert(700, 250, function() end, dateTime, "Epoch Timestamp: " .. timestamp, "ok", nil, "informational")
hs.application.get("Hammerspoon"):setFrontmost()
end
function EtherscanLookup(hash)
local baseUrl = "https://etherscan.io"
local url = baseUrl .. IsAddressOrTxn(hash) .. hash
hs.urlevent.openURL(url)
end
function IsHash(text)
local s, e = string.find(text, "0x");
return s == 1 and e == 2
end
function IsAddressOrTxn(hash)
local length = string.len(hash)
if length == 66 then
return "/tx/"
elseif length == 42 then
return "/address/"
else
return false
end
end
function GrabSelectedText()
hs.eventtap.keyStroke({ "cmd" }, "c");
return hs.pasteboard.getContents();
end
function RearrangeScreenLayout(data)
-- productID of USB Hub
if data["productID"] == 2069 then
-- move vscode to first space of external monitor when plugged in
if data["eventType"] == "added" then
local vscode = hs.window("VS Code")
hs.timer.doAfter(1, function()
vscode:moveOneScreenEast(false, true):maximize()
end)
end
-- move vscode to second to last space when external monitor is unplugged
if data["eventType"] == "removed" then
local spaces = hs.spaces.spacesForScreen()
local vscode = hs.window("VS Code")
hs.spaces.moveWindowToSpace(vscode, spaces[#spaces - 1])
vscode:maximize()
end
end
end
function ScreenCallback()
print("screen event")
end
-- receive text from "send to hammerspoon"
hs.textDroppedToDockIconCallback = function(text)
if IsHash(text) and IsAddressOrTxn(text) then
EtherscanLookup(text)
elseif tonumber(text) ~= nil then
ConvertEpochTimestamp(text)
else
print("received non-integer or non-hash")
end
end
-- auto reload
AutoReload = hs.pathwatcher.new(os.getenv("HOME") .. "/.hammerspoon/", reloadConfig):start()
SetGrid(Screens, hs.geometry(nil, nil, 2, 2), hs.geometry(0, 0))
-- watch for usb connect/disconnect events
UsbWatcher = hs.usb.watcher.new(RearrangeScreenLayout)
UsbWatcher:start()
-- watch for screen connect/disconnect events
-- ScreenWatcher = hs.screen.watcher.new(ScreenCallback)
-- ScreenWatcher:start()