-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselector.lua
More file actions
41 lines (40 loc) · 798 Bytes
/
selector.lua
File metadata and controls
41 lines (40 loc) · 798 Bytes
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
---@generic K
---@param values K[]
---@param choices string[]
---@param prompt string
---@param row boolean?
---@return K?
local function selector(values, choices, prompt, row)
table.insert(values, 'Cancel')
if values ~= choices then
table.insert(choices, "Cancel")
end
if not row then
for index, c in ipairs(choices) do
io.write(index .. ". " .. c .. "\n")
end
else
local s = ""
for index, c in ipairs(choices) do
s = s .. index .. ". " .. c .. "\t"
if index % 3 == 0 then
s = s .. "\n"
end
end
io.write(s .. "\n")
end
local choice
while not choice do
io.write(prompt)
local input = io.read()
if not input then
return nil
end
choice = values[tonumber(input)]
end
if choice == "Cancel" then
return nil
end
return choice
end
return selector