aboutsummaryrefslogtreecommitdiff
path: root/gamemode/shared/concommands.lua
blob: 79ea0f3b0a3477978b572f892770ea06d3911617 (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
--@module 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