-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.lua
390 lines (364 loc) · 9.34 KB
/
lib.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
---@diagnostic disable: duplicate-set-field, inject-field
---Executes `fn` for every element of `array` in numbered array
---@generic TValue
---@param array {[number]: TValue}
---@param fn fun(index: number, value: TValue)
---@return {[number]: TValue}
each = function(array, fn)
for index, value in ipairs(array) do
fn(index, value)
end
return array
end
---Executes `fn` for every element of `array` in key-value array
---@generic TKey
---@generic TValue
---@param array {[TKey]: TValue}
---@param fn fun(key: TKey, value: TValue)
---@return {[TKey]: TValue}
eachPair = function (array, fn)
for key, value in pairs(array) do
fn(key, value)
end
return array
end
---Returns executed `fn` for every `array` member
---@generic TKey
---@generic TValue
---@generic TReturn : table
---@param array {[TKey]: TValue}
---@param fn fun(key: TKey, value: TValue): TReturn
---@return table
mapDeepPair = function (array, fn)
local res = {}
for key, value in pairs(array) do
local fnResult = fn(key, value)
if fnResult == nil then
error('not a table, nil')
end
if type(fnResult) ~= 'table' then
res[key] = fnResult
else
for nestedKey, nestedValue in pairs(fnResult) do
res[nestedKey] = nestedValue
end
end
end
return res
end
---Returns executed `fn` for every `array` member
---@generic TKey
---@generic TValue
---@generic TReturn : table
---@param array {[TKey]: TValue}
---@param fn fun(key: TKey, value: TValue): TReturn
---@return table
mapPair = function (array, fn)
local res = {}
for key, value in pairs(array) do
local fnResult = fn(key, value)
if fnResult == nil then
error('not a table, nil')
end
res[key] = fnResult
end
return res
end
---Returns all `array` members that pass the test `fn`
---@generic TKey
---@generic TValue
---@param array {[TKey]: TValue}
---@param fn fun(key: TKey, value: TValue): boolean
---@return {[TKey]: TValue}
filterPair = function (array, fn)
local res = {}
for key, value in pairs(array) do
if fn(key, value) then
res[key] = value
end
end
return res
end
---Finds an `fn` element in table `array`
---@generic TKey
---@generic TValue
---@param array {[TKey]: TValue}
---@param fn fun(key: TKey, value: TValue): boolean
---@return {[TKey]: TValue}
findPair = function(array, fn)
local res = {}
for key, value in pairs(array) do
if fn(key, value) then
res[key] = value
break
end
end
return res
end
---Removes duplicates from a table
---@param array table
---@return table
toSet = function (array)
hash = {}
res = {}
each(array, function (_, value)
if not hash[value.name] then
res[#res+1] = value
hash[value.name] = true
end
end)
return res
end
---Removes elements with duplicate keys from a table
---@generic TKey
---@generic TValue
---@param array {[TKey]: TValue}
---@param fn fun(key: TKey, value: TValue): any
---@return {[TKey]: TValue}
toSetPair = function(array, fn)
hash = {}
res = {}
eachPair(array, function (index, value)
if not hash[fn(index, value)] then
res[#res+1] = value
hash[fn(index, value)] = true
end
end)
return res
end
---Performs `table.insert` on `array` on every `array` member that satisfies the test `fn`
---@param array table
---@param fn function
---@return boolean
insertIfContains = function (array, fn)
local doesContain = false
each(array, function (value)
if fn(value) then
doesContain = true
table.insert(array, value)
end
end)
return doesContain
end
---Removes any `array` member that satisfies the test `fn`
---@generic TValue
---@param array {[number]: TValue}
---@param fn fun(index: number, value: TValue): boolean
---@return {[number]: TValue}
removeIfContains = function (array, fn)
local arrayWithRemoved = array
for index, value in ipairs(array) do
if fn(index, value) then
arrayWithRemoved[index] = nil
end
end
return arrayWithRemoved
end
---Removes any `array` member that satisfies the test `fn`
---@generic TKey
---@generic TValue
---@param array {[TKey]: TValue}
---@param fn fun(index: TKey, value: TValue)
---@return {[TKey]: TValue}
removeIfContainsPair = function (array, fn)
local arrayWithRemoved = array
for key, value in pairs(array) do
if fn(key, value) then
arrayWithRemoved[key] = nil
end
end
return arrayWithRemoved
end
---Sorts a numbered table `array`
---@generic TArray: {[number]: table}
---@param array TArray
---@return TArray
reorder = function (array)
local arrayWithIndexes = mapPair(array, function (key, value)
value.index = key
return value
end)
table.sort(arrayWithIndexes, function (current, next)
if not next then
return true
end
return current.index < next.index
end)
return arrayWithIndexes
end
---Moves item at position `old` to position `new` in table `array`
---@generic TKey
---@param array {[TKey]: any}
---@param old TKey
---@param new TKey
shift = function(array, old, new)
local value = array[old]
if new < old then
table.move(array, new, old - 1, new + 1)
else
table.move(array, old + 1, new, old)
end
array[new] = value
end
---Returns true if any `fn` run on each `array` element return true
---@generic TValue
---@param array {[number]: TValue}
---@param fn fun(index: number, value: TValue): boolean
---@return boolean
some = function(array, fn)
local isSome = false
for index, value in ipairs(array) do
if fn(index, value) then
isSome = true
break
end
end
return isSome
end
---Returns true if any `fn` run on each `array` element return true
---@generic TKey
---@generic TValue
---@param array {[TKey]: TValue}
---@param fn fun(index: TKey, value: TValue): boolean
---@return boolean
somePair = function(array, fn)
local isSome = false
for key, value in ipairs(array) do
if fn(key, value) then
isSome = true
break
end
end
return isSome
end
---Concatenates two tables `array1` and `array2` together
---@param array1 table
---@param array2 table
concat = function(array1, array2)
for i = 1, #array2 do
array1[#array1 + 1] = array2[i]
end
end
---Gets the key of `ofValue` in table `array`
---@generic TKey
---@generic TValue
---@param array {[TKey]: TValue}
---@param ofValue TValue
---@return TKey
keyOf = function(array, ofValue)
local ofKey
for key, value in pairs(array) do
if value == ofValue then
ofKey = key
break
end
end
return ofKey
end
---Creates an `Application` instance from a names
---@param name string
---@return Application
createApp = function(name)
local app = hs.application.find(name)
return {
name = app:title(),
image = hs.image.imageFromAppBundle(app:bundleID()),
instance = app,
window = app:mainWindow(),
}
end
---Gets all the open apps under a specific format
---@param asSet boolean? Defaults to true. If true, there will be no duplicates
---@return Application[]
getAllOpenApps = function (asSet)
if asSet == nil then
asSet = true
end
local windows = hs.window.allWindows()
---@type Application[]
appsFormatted = {}
eachPair(windows, function (index, window)
local app = window:application() --[[@as hs.application]]
local appFormatted = createApp(app:title())
appsFormatted[index] = appFormatted
end)
local appsFiltered = filterPair(appsFormatted, function (_, app)
return not (app.name == 'Hammerspoon' and not app.window:isStandard())
end) --[[@as Application[] ]]
if not asSet then
return appsFiltered
end
return toSet(appsFiltered)
end
---Prints a table
---@param array table
---@param printNested boolean? Defaults to true
---@param printStacktrace boolean? Defaults to false
printTable = function(array, printNested, printStacktrace)
if printNested == nil then
printNested = true
end
if printStacktrace then
print(debug.traceback())
end
if type(array) ~= 'table' then
print('not a table, but a '..type(array), array)
return
end
if array == nil then
print('variable is nil')
return
end
eachPair(array, function (key, value)
print(key, value)
if type(value) == 'table' and printNested then
print('{')
eachPair(value, function (nestedKey, nestedValue)
print(' ', nestedKey, nestedValue)
end)
print('}')
end
end)
end
---Converts a string of kebab case to camel case
---@param str string
---@return string
kebabToCamel = function(str)
local previousChar
local newStr
for char in str:gmatch'.' do
if char ~= '-' then
if previousChar == '-' then
newStr = newStr .. string.upper(char)
else
newStr = (newStr or '') .. char
end
end
previousChar = char
end
return newStr
end
---Creates the necessary requires for export functions
---@param filenames string[] relative path to the files
---@param path string Absolute path that will go before each file name
createRequires = function(filenames, path)
mapPair(filenames, function (_, filename)
return {[string.match(kebabToCamel(filename), "/(.*)")] = require(path .. filename)}
end)
end
---Finds index of `item` in `array`
---@generic TKey : string | number
---@generic TValue : any
---@param array {[TKey]: TValue}
---@param item TValue
---@return TKey
indexOfPair = function(array, item)
local index = nil
eachPair(array, function (currentIndex, currentItem)
if currentItem == item then
index = currentIndex
return
end
end)
return index
end