Quantcast
Download
(9 Kb)
Download
Updated: 10/19/16 09:56 AM
Pictures
File Info
Compatibility:
One Tamriel (2.6)
Updated:10/19/16 09:56 AM
Created:10/19/16 09:56 AM
Monthly downloads:21
Total downloads:342
Favorites:5
MD5:
LibSlashCommander
Version: 1.0 r4
by: sirinsidiator [More]
This library wraps around slash commands and adds many useful features.
  • Autocompletion for all slash commands
  • Color coding and description for all built-in commands
  • Functions for addon authors to register their own commands with a description
  • Subcommands for structured menus
  • Autocompletion for subcommands
  • Custom autocompletion handlers for easy parameter selection

Quick Start

In order to use the library, the following lines have to be included in the addon manifest:

Code:
## OptionalDependsOn: LibStub LibSlashCommander

lib/LibStub/LibStub.lua
lib/LibSlashCommander/LibSlashCommander.lua
lib/LibSlashCommander/descriptions/$(language).lua
A simple slash command is then registered with the following code:

Lua Code:
  1. local LSC = LibStub("LibSlashCommander")
  2. LSC:Register("/mycommand", function(input) d(input) end, "Prints the specified text")

You can also add multiple names for the same command

Lua Code:
  1. LSC:Register({"/mycommand", "/mycmd"}, function(input) d(input) end, "Prints the specified text")

Alternatively you can also set the values on the Command object which is returned from Register.

Lua Code:
  1. local LSC = LibStub("LibSlashCommander")
  2. local command = LSC:Register()
  3. command:AddAlias("/mycommand")
  4. command:AddAlias("/mycmd")
  5. command:SetCallback(function(input) d(input) end)
  6. command:SetDescription("Prints the specified text")

In order to add a subcommand to the new command you can call RegisterSubCommand and set the properties like before.
Lua Code:
  1. local subCommand = command:RegisterSubCommand()
  2. subCommand:AddAlias("fail")
  3. subCommand:SetCallback(function() error("as expected") end)
  4. subCommand:SetDescription("Produces an error")

If the base command has a callback, it will still get called if there is no matching subcommand alias. You can also register subcommands to a subcommand to create a tree of commands.

Autocompletion for your registered subcommands will automatically turn on as long as there is no custom autocompletion enabled. In order to disable the built in autocompletion for your subcommands you can either call command:SetAutoComplete(false) or set your own autocompletion.

There are two ways to make your own autocompletion.
Either you supply a numerically indexed table of strings to SetAutoComplete:
Lua Code:
  1. command:SetAutoComplete({"foo", "bar", "baz"})

Or you create your own AutoCompleteProvider:
Lua Code:
  1. local MyAutoCompleteProvider = LSC.AutoCompleteProvider:Subclass()
  2. function MyAutoCompleteProvider:New()
  3.     return LSC.AutoCompleteProvider.New(self)
  4. end
  5.  
  6. function MyAutoCompleteProvider:GetResultList()
  7.     return {
  8.         foo = "foo",
  9.         bar = "bar",
  10.         baz = "baz",
  11.     }
  12. end
  13.  
  14. command:SetAutoComplete(MyAutoCompleteProvider:New())

The key in the table returned from GetResultList will be used to determine which results should show up in the autocomplete box.
The values will be used as the label for the entries and also show up in the chat input box when you select an entry.

If you want to have a different label, you can use the GetResultFromLabel method to transform the displayed entry to something else on select.
Lua Code:
  1. function MyAutoCompleteProvider:GetResultFromLabel(label)
  2.     return "always the same"
  3. end

The simplest way to handle it is to generate a lookup table in GetResultList and then just return lookup[label].
You can also generate the results on the fly, but keep in mind that GetResultList will be called on every change to the chat input, so it is usually better to cache the results.

API Reference
Will follow soon.
Optional Files (0)


Post A Reply Comment Options
Unread 10/21/16, 06:17 AM  
sirinsidiator
 
sirinsidiator's Avatar
AddOn Author - Click to view AddOns

Forum posts: 890
File comments: 541
Uploads: 27
Originally Posted by code65536
Could you please add a check so that the auto-complete selection box closes itself once the slash command has been completed and there are no auto-completes available for the parameters?

For example, once I type "/script<SPACEBAR>", the auto-complete selection box should close because I've completed the slash command portion with the <SPACEBAR>, and for the "/script" command, there are no parameters to auto-complete.

The reason I'm asking for this is because for a number of slash commands, I need to see or reference what is being shown in the chat box, and the auto-complete selection box covers up the chat. "/script" is the most prominent example because I use it to test/debug stuff which often requires looking at chat output, but there are probably other cases too.

Thanks in advance.
I'll see what I can do. For now you can use the tab key instead of space to get it to disappear.
__________________
>siri.exe MyAddon
Does your addon work? [y/n] n
There is a typo in there.
Report comment to moderator  
Reply With Quote
Unread 10/21/16, 05:42 AM  
code65536
AddOn Author - Click to view AddOns

Forum posts: 12
File comments: 31
Uploads: 8
Could you please add a check so that the auto-complete selection box closes itself once the slash command has been completed and there are no auto-completes available for the parameters?

For example, once I type "/script<SPACEBAR>", the auto-complete selection box should close because I've completed the slash command portion with the <SPACEBAR>, and for the "/script" command, there are no parameters to auto-complete.

The reason I'm asking for this is because for a number of slash commands, I need to see or reference what is being shown in the chat box, and the auto-complete selection box covers up the chat. "/script" is the most prominent example because I use it to test/debug stuff which often requires looking at chat output, but there are probably other cases too.

Thanks in advance.
Report comment to moderator  
Reply With Quote
Post A Reply



Category Jump: