-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp-prefix-launch-search.lua
More file actions
64 lines (53 loc) · 1.37 KB
/
app-prefix-launch-search.lua
File metadata and controls
64 lines (53 loc) · 1.37 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
-- name = "App Prefix"
-- description = "Finds apps by name prefix and auto-launches when there is exactly one match"
-- type = "search"
-- author = "Codex"
-- version = "1.0"
all_apps = {}
shown_apps = {}
local max_results = 3
function on_load()
all_apps = apps:apps()
end
function on_search(query)
if all_apps == nil or next(all_apps) == nil then
all_apps = apps:apps()
end
if query == nil or query == "" then
search:show_lines({})
return
end
local q = string.lower(query)
local names = {}
local colors = {}
shown_apps = {}
local match_count = 0
for _, app in ipairs(all_apps) do
local name = app.name or ""
if string.sub(string.lower(name), 1, #q) == q then
match_count = match_count + 1
if match_count <= max_results then
table.insert(shown_apps, app)
table.insert(names, name)
table.insert(colors, app.color)
end
end
end
if match_count == 1 then
apps:launch(shown_apps[1].pkg)
return
end
if match_count == 0 then
search:show_lines({"No matches"})
return
end
search:show_lines(names, colors)
end
function on_click(idx)
local app = shown_apps[idx]
if app ~= nil then
apps:launch(app.pkg)
return true
end
return false
end