-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtofi.lua
More file actions
69 lines (62 loc) · 1.41 KB
/
tofi.lua
File metadata and controls
69 lines (62 loc) · 1.41 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
-- Open a Tofi menu with Lua
-- require("tofi").options({...}).choices({...}).open()
local function choice_name(choice)
return type(choice) == "table" and choice.name or choice
end
local function choice_value(choices, name)
for _, c in ipairs(choices) do
if choice_name(c) == name then
if type(c) == "table" then
return c.value ~= nil and c.value or c.name
end
return c
end
end
return name
end
local function execute_tofi(choices, options)
local cmd = ""
if choices then
cmd = "echo '"
for _, choice in ipairs(choices) do
cmd = cmd .. choice_name(choice) .. "\n"
end
cmd = cmd .. "' | tofi "
else
cmd = "tofi-drun "
end
for k, v in pairs(options or {}) do
cmd = cmd .. " --" .. k .. "=" .. v
end
local handle = io.popen(cmd)
local retval = ""
if handle then
retval = handle:read("*a")
handle:close()
end
return retval:gsub("%s+$", "")
end
local function create_opener(choices, opts)
return {
open = function()
local selection = execute_tofi(choices, opts)
if selection == "" then
return nil
end
if choices then
return choice_value(choices, selection)
end
return selection
end,
info = function()
return { choices = choices, options = opts }
end,
choices = function(new_choices)
return create_opener(new_choices, opts)
end,
options = function(new_opts)
return create_opener(choices, new_opts)
end,
}
end
return create_opener(nil, nil)