-
Notifications
You must be signed in to change notification settings - Fork 49
/
init.lua
110 lines (92 loc) · 3.16 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
--- === Anycomplete ===
---
--- The magic of Google Autocomplete while you're typing. Anywhere.
---
--- Configurable properties (with default values):
--- engine = "google", -- or "duckduckgo"
---
--- Download: [https://github.com/nathancahill/Anycomplete](https://github.com/nathancahill/Anycomplete)
local obj = {}
-- Options
obj.engine = "google"
-- Metadata
obj.name = "Anycomplete"
obj.version = "1.0"
obj.author = "Nathan Cahill <[email protected]>"
obj.homepage = "https://github.com/nathancahill/Anycomplete"
obj.license = "MIT - https://opensource.org/licenses/MIT"
local endpoints = {
google = "https://suggestqueries.google.com/complete/search?client=firefox&q=%s",
duckduckgo = "https://duckduckgo.com/ac/?q=%s",
}
-- Anycomplete
function obj:anycomplete()
local current = hs.application.frontmostApplication()
local tab = nil
local copy = nil
local choices = {}
local chooser = hs.chooser.new(function(choosen)
if copy then copy:delete() end
if tab then tab:delete() end
current:activate()
if not choosen then return end
hs.eventtap.keyStrokes(choosen.text)
end)
-- Removes all items in list
function reset()
chooser:choices({})
end
tab = hs.hotkey.bind('', 'tab', function()
local id = chooser:selectedRow()
local item = choices[id]
-- If no row is selected, but tab was pressed
if not item then return end
chooser:query(item.text)
reset()
updateChooser()
end)
copy = hs.hotkey.bind('cmd', 'c', function()
local id = chooser:selectedRow()
local item = choices[id]
if item then
chooser:hide()
hs.pasteboard.setContents(item.text)
hs.alert.show("Copied to clipboard", 1)
else
hs.alert.show("No search result to copy", 1)
end
end)
function updateChooser()
local string = chooser:query()
local query = hs.http.encodeForQuery(string)
-- Reset list when no query is given
if string:len() == 0 then return reset() end
hs.http.asyncGet(string.format(endpoints[self.engine], query), nil, function(status, data)
if not data then return end
local ok, results = pcall(function() return hs.json.decode(data) end)
if not ok then return end
if self.engine == "google" then
choices = hs.fnutils.imap(results[2], function(result)
return {
["text"] = result,
}
end)
elseif self.engine == "duckduckgo" then
choices = hs.fnutils.imap(results, function(result)
return {
["text"] = result["phrase"],
}
end)
end
chooser:choices(choices)
end)
end
chooser:queryChangedCallback(updateChooser)
chooser:searchSubText(false)
chooser:show()
end
function obj:bindHotkeys(mapping)
mapping = mapping or {{"cmd", "alt", "ctrl"}, "g"}
hs.hotkey.bind(mapping[1], mapping[2], function() obj:anycomplete() end)
end
return obj