Facepunch / garrysmod-requests

Feature requests for Garry's Mod
83 stars 24 forks source link

Provide 'generic' concommand autocomplete generator #2384

Closed TetraTheta closed 1 month ago

TetraTheta commented 1 month ago

I know this kind of syntax would not be 'generic', but I really think this kind of stuff will help people whose heads got messed up from creating complicated concommand autocomplete stuff.

Most of concommand would look like this:

command [argument1] [argument2] ... [player]

Maybe some people prefer putting [player] in front of arguments, but I'll start from this syntax.

Then here is how the requested function look like:

function GenerateAutoComplete(cmd, arg, suggestPlayer, ...)
  -- ...
end
local function MyCmdAC(cmd, args)
  return GenerateAutoComplete(cmd, args, true, {"all", "alpha", "beta", "gamma"}, {"first", "second", "third", "fourth"})
end
concommand.Add("mycmd", MyCmd, MyCmdAC, "my command", FCVAR_NONE)

If args is mycmd a, GenerateAutoComplete() should return a table like this:

{
  "mycmd all first ...", -- '...' means it requires additional arguments
  "mycmd all second ...",
  "mycmd all third ...",
  "mycmd all fourth ...",
  "mycmd alpha first ...",
  "mycmd alpha second ..."
  "mycmd alpha third ...",
  "mycmd alpha fourth ..."
  -- no 'beta' and 'gamma' because they don't start with 'a'
  -- no suggesting player because user didn't provide enough arguments
}

This only suggest 'current' and 'next' arguments.

If args is mycmd all t, it should return a table like this:

{
  "mycmd all third \"Player 1\"", -- player name is encased with double-quote because their name can contain space
  "mycmd all third \"Player 2\"",
  ...
  -- only suggest 'third' because it is only argument that starts with 't'
}

If args is mycmd ala which is not valid argument, it should suggest argument that start with first letter, a.

{
  -- same table with above
  "mycmd all first ...",
  "mycmd all second ...",
  "mycmd all third ...",
  "mycmd all fourth ...",
  "mycmd alpha first ...",
  "mycmd alpha second ..."
  "mycmd alpha third ...",
  "mycmd alpha fourth ..."
}
robotboy655 commented 1 month ago

So what exactly is stopping you from implementing the auto complete as you described?

TetraTheta commented 1 month ago

I tried to implement myself few times, but my brain got melted whenever I tried.

When I ask question about this, I wished that if there is any helper function for concommand auto-completion.

If you think this is niche case or trivial, you can close this as 'not planned'.

garryspins commented 1 month ago

I think this would be a good addition but I dont know if this is the way to go about it since it controls the autocomplete entirely, maybe concommand.AddWIthAutocomplete(..., {{"a", "b"}, {"c", "d"}}) Would make a good pr since we dont have anything like this atm

robotboy655 commented 1 month ago

I have added an example how to use autocomplete on the wiki: https://wiki.facepunch.com/gmod/concommand.Add

For future reference, you can ask for help on official Discord server (https://discord.gg/gmod). This is not a place for such things.