blob: ad91d5374734e2172205385741af6f3f23bb2e91 (
plain)
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
|
--- Concommand autocomplete helper.
-- Helps you write console commands' autocomplete functions
--@shared concommands.lua
local fuzzel = nrequire("fuzzel.lua")
local concmd = {}
--- Generate an autocomplete function.
-- Generates an autocomplete function based on the strings passed in.
-- The strings can be passed in as a list of arguments, or in an array.
function concmd.AutocompleteFunction(...)
local opt = {...}
opt = type(opt[1]) == "table" and opt[1] or opt
return function(cmd,strargs)
--Remove spaces and quotes, since we don't want to match them
strargs = string.gsub(strargs,"[\" ]","")
--Find the options that most closely resemble our command so far
local sorted = fuzzel.fad(strargs,opt)
--Add quotes if needed, and preppend the command to each option
for k,v in pairs(sorted) do
if string.find(v," ") ~= nil then
sorted[k] = "\"" .. v .. "\""
end
sorted[k] = cmd .. " " .. sorted[k]
end
return sorted
end
end
return concmd
|