-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.lua
298 lines (232 loc) · 5.92 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
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
-- Copyright (c) 2016 Etan Reisner
-- luacheck: read globals hs
-- luacheck: max comment line length 125
--- === SSHChooser ===
---
--- Start new SSH session from a chooser or menu
---
--- Download: [https://github.com/Hammerspoon/Spoons/raw/master/Spoons/SSHChooser.spoon.zip](SSHChooser.spoon.zip)
local obj = {
name = 'SSHChooser',
version = '0.5.0',
author = 'Etan Reisner <deryni@gmail.com>',
license = 'MPL-2.0',
homepage = 'https://github.com/deryni/sshchooser',
}
----
-- Variables
--- SSHChooser.logger
--- Variable
--- Logger object used within the Spoon. Can be accessed to set the default log level for the messages coming from the Spoon.
obj.logger = hs.logger.new('SSHChooser', 'info')
--- SSHChooser.useMenu
--- Variable
--- Enable menubar item
obj.useMenu = true
--- SSHChooser.launcher
--- Variable
--- Launcher to use to launch the SSH session
obj.launcher = 'iterm2'
----
-- Internals
local sshDir = ('%s/.ssh'):format(os.getenv('HOME'))
local parseFns = dofile(hs.spoons.resourcePath('parse_fns.lua'))
-- The ssh chooser
local chooser = nil
-- The SSH menu
local menu = nil
local launchCmd
local defaultHotkeys = {
showChooser = {{'shift', 'cmd',}, 'p'}
}
local function loadLauncher()
launchCmd = dofile(hs.spoons.resourcePath(('launcher/%s.lua'):format(obj.launcher)))
end
-- Launch helper functions
local launchHelpers = {
shell_quote = function (val)
if ('number' == type(val)) or tonumber(val) then
return val
end
if 'string' ~= type(val) then
return
end
return "'"..val:gsub("'", [['\'']]).."'"
end,
escape_quotes = function (val)
return val:gsub('"', [[\"]])
end,
do_applescript = function (ascmd)
local ok, _, rawout = hs.osascript.applescript(ascmd) -- luacheck: no unused
--[[
local lvl = ok and 'i' or 'e'
if type(rawout) == 'table' then
for k, v in pairs(rawout) do
obj.logger[lvl](('%s = %s'):format(tostring(k), tostring(v)))
end
else
if rawout ~= 'null()' then
obj.logger[lvl](('%s: %s'):format(type(rawout), tostring(rawout)))
end
end
--]]
end,
}
----
local function doSsh(tab)
if not tab then
return
end
local host = tab.text or tab.host or tab.title
if not host then
return
end
if type(host) ~= 'string' then
host = host:getString()
end
if not launchCmd then
loadLauncher()
end
return launchCmd(launchHelpers, host)
end
local function newChooser()
local _chooser = hs.chooser.new(doSsh)
_chooser:rows(5)
_chooser:width(40)
_chooser:searchSubText(true)
_chooser:placeholderText('SSH host')
-- Clear the query and reset the scroll on dismissal.
_chooser:hideCallback(function()
_chooser:query('')
_chooser:selectedRow(0)
end)
return _chooser
end
local function doSshMenu(mods, tab) -- luacheck: no unused args
if (not tab) or (not tab.title) then
return
end
return doSsh(tab)
end
local function newMenu()
if not obj.useMenu then
return
end
local _menu = hs.menubar.new()
_menu:setTitle('SSH')
return _menu
end
local function checkConfigChanged(paths)
for _, v in ipairs(paths) do
if v:match('/config$') or v:match('/known_hosts$') then
obj:loadHosts()
break
end
end
end
local function makeMenu(hosts)
local hostMenu = {}
for _, host in ipairs(hosts) do
local m = {
title = host.text,
fn = doSshMenu,
tooltip = host.subText,
}
if host.subText and (not host.subText:match('%s')) then
m.host = m.title
m.title = m.title..(' (%s)'):format(m.tooltip)
end
hostMenu[#hostMenu + 1] = m
end
return hostMenu
end
local function showChooser()
if not chooser then
chooser = newChooser()
obj:loadHosts()
end
chooser:show()
end
----
-- Callable functions
--- SSHChooser.loadHosts()
--- Method
--- Load the SSH configuration and Known Hosts and populate the chooser and menu.
---
--- Parameters:
--- * None
---
--- Notes:
--- * Normally called automatically by pathwatcher. Should only need to be called manually if start() is not used.
function obj:loadHosts()
local hosts = parseFns.parse_config(sshDir)
chooser:choices(hosts)
if self.useMenu then
menu:setMenu(makeMenu(hosts))
end
end
----
--- SSHChooser:init()
--- Method
--- Register a pathwatcher for SSH configuration changes.
---
--- Parameters:
--- * None
---
--- Returns:
--- * The SSHChooser
function obj:init()
self._pathWatcher = hs.pathwatcher.new(sshDir, checkConfigChanged)
return self
end
--- SSHChooser:start()
--- Method
--- Activate the pathwatcher for SSH configuration changes. Creates the chooser and menu (if enabled).
---
--- Parameters:
--- * None
---
--- Returns:
--- * The SSHChooser
function obj:start()
self._pathWatcher:start()
loadLauncher()
chooser = newChooser()
menu = newMenu()
self:loadHosts()
return self
end
--- SSHChooser:stop()
--- Method
--- Stops the pathwatcher for SSH configuration changes. Deletes the chooser and menu.
---
--- Parameters:
--- * None
---
--- Returns:
--- * The SSHChooser
function obj:stop()
self._pathWatcher:stop()
if chooser then
chooser:delete()
end
if menu then
menu:delete()
end
return self
end
--- SSHChooser:bindHotkeys(mapping)
--- Method
--- Binds hotkeys for SSHChooser
---
--- Parameters:
--- * mapping - A table containing hotkey objifier/key details for the following items:
--- * showChooser - Show the SSH session chooser
function obj:bindHotkeys(mapping)
local spec = {
showChooser = showChooser
}
hs.spoons.bindHotkeysToSpec(spec, mapping or defaultHotkeys)
return self
end
return obj