-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.lua
More file actions
268 lines (222 loc) · 6.31 KB
/
Copy pathinit.lua
File metadata and controls
268 lines (222 loc) · 6.31 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
local obj = {}
obj.__index = obj
-- Metadata
obj.name = 'PassChooser'
obj.version = '0.1'
obj.author = 'Raitis Stengrevics'
obj.homepage = 'https://github.com/daGrevis/PassChooser.spoon'
obj.license = 'MIT - https://opensource.org/licenses/MIT'
-- http://lua-users.org/wiki/StringRecipes
function string.ends(String, End)
return End == '' or string.sub(String, -string.len(End)) == End
end
-- https://stackoverflow.com/a/15706820
function spairs(t, order)
-- collect the keys
local keys = {}
for k in pairs(t) do keys[#keys+1] = k end
-- if order function given, sort by it by passing the table and keys a, b,
-- otherwise just sort the keys
if order then
table.sort(keys, function(a,b) return order(t, a, b) end)
else
table.sort(keys)
end
-- return the iterator function
local i = 0
return function()
i = i + 1
if keys[i] then
return keys[i], t[keys[i]]
end
end
end
-- https://gist.github.com/Badgerati/3261142
function string.levenshtein(str1, str2)
local len1 = string.len(str1)
local len2 = string.len(str2)
local matrix = {}
local cost = 0
-- quick cut-offs to save time
if (len1 == 0) then
return len2
elseif (len2 == 0) then
return len1
elseif (str1 == str2) then
return 0
end
-- initialise the base matrix values
for i = 0, len1, 1 do
matrix[i] = {}
matrix[i][0] = i
end
for j = 0, len2, 1 do
matrix[0][j] = j
end
-- actual Levenshtein algorithm
for i = 1, len1, 1 do
for j = 1, len2, 1 do
if (str1:byte(i) == str2:byte(j)) then
cost = 0
else
cost = 1
end
matrix[i][j] = math.min(matrix[i-1][j] + 1, matrix[i][j-1] + 1, matrix[i-1][j-1] + cost)
end
end
-- return the last value - this is the Levenshtein distance
return matrix[len1][len2]
end
local config = {
clearAfter=0,
storePath='~/.password-store/',
}
function obj:init(userConfig)
if not userConfig then
userConfig = {}
end
if userConfig.clearAfter then
config.clearAfter = userConfig.clearAfter
end
if userConfig.storePath then
config.storePath = userConfig.storePath
end
end
function obj:start()
local front_app = hs.application.frontmostApplication()
local function restore()
if enterBind['delete'] then enterBind:delete() end
if escapeBind['delete'] then escapeBind:delete() end
if ccBind['delete'] then ccBind:delete() end
if numberBinds then
for i, bind in pairs(numberBinds) do
if bind['delete'] then
bind:delete()
end
end
end
front_app:activate()
end
local chooser = hs.chooser.new(function()
restore()
end)
local choices = {}
local all_texts = {}
local function updateChoices(query)
local chars = {}
for i = 1, #query do
table.insert(chars, query:sub(i, i))
end
-- http://lua-users.org/wiki/PatternsTutorial
local fuzzy_query = table.concat(chars, '.-')
local matching_texts = hs.fnutils.filter(all_texts, function(text)
return text:find(fuzzy_query)
end)
local items = {}
for _, text in pairs(matching_texts) do
local distance
if query == '' then
distance = 1
else
distance = text:levenshtein(query)
end
table.insert(items, { text=text, distance=distance })
end
choices = {}
for _, v in spairs(items, function(t, a, b)
if t[a].distance == t[b].distance then
return t[a].text < t[b].text
else
return t[a].distance < t[b].distance
end
end) do
table.insert(choices, { text=v.text })
end
chooser:choices(choices)
end
chooser:queryChangedCallback(function()
updateChoices(chooser:query())
end)
local function copyPassword(index)
local item = choices[index]
if not item then return end
-- Run via the user's login+interactive shell so `pass` is found wherever it's installed.
-- item.text is passed as a positional arg to avoid shell injection.
hs.task.new(os.getenv('SHELL') or '/bin/zsh', function(exitCode, stdOut, stdErr)
if exitCode ~= 0 then
hs.alert.show('pass error: ' .. (stdErr or ''))
return
end
-- Assumes that password is on the first line just like pass does.
local password = hs.fnutils.split(stdOut, '\n')[1]
hs.pasteboard.setContents(password)
-- Clear pasteboard after N seconds if nothing else has been copied.
if config.clearAfter ~= 0 then
hs.timer.doAfter(config.clearAfter, function()
if password == hs.pasteboard.getContents() then
hs.pasteboard.setContents(' ')
end
end)
end
hs.alert.show('copied: ' .. item.text)
end, {'-lic', 'pass show "$1"', 'passchooser', item.text}):start()
end
enterBind = hs.hotkey.bind('', 'return', function()
local id = chooser:selectedRow()
chooser:cancel()
restore()
copyPassword(id)
end)
escapeBind = hs.hotkey.bind('', 'escape', function()
chooser:cancel()
restore()
end)
ccBind = hs.hotkey.bind({'ctrl'}, 'c', function()
chooser:cancel()
restore()
end)
numberBinds = {}
local i = 1
while i <= 9 do
local id = i
numberBinds[#numberBinds + 1] = hs.hotkey.bind({'cmd'}, tostring(i), function()
chooser:cancel()
restore()
copyPassword(id)
end)
i = i + 1
end
chooser:show()
-- Read the password store async so the chooser stays responsive while find runs.
local store_path = config.storePath:gsub('^~', os.getenv('HOME'))
hs.task.new('/usr/bin/find', function(exitCode, stdOut, stdErr)
if exitCode ~= 0 then return end
local lines = hs.fnutils.filter(hs.fnutils.split(stdOut, '\n'), function(line)
return line ~= '' and not line:ends('.gpg-id')
end)
all_texts = hs.fnutils.map(lines, function(line)
local filename = hs.fnutils.split(line, '//')[2]
return filename:sub(0, filename:len() - 4)
end)
if chooser:isVisible() then
updateChoices(chooser:query())
end
end, {store_path, '-type', 'f'}):start()
end
function obj:bindHotkeys(mapping)
if hotkey then
hotkey:cancel()
end
local showMapping = mapping['show']
if showMapping then
hotkey = hs.hotkey.new(
showMapping[1],
showMapping[2],
function()
obj:start()
end
):enable()
end
return self
end
return obj