Replies: 2 comments 1 reply
-
I have a few suggestions on how to modify the current implementation. Modification of ParseCommandBaseCurrently, Modification of ColoredString.CustomProcessorI'd suggest modifying the implementation of how to integrate custom commands to make it more intuitive. One way that came to mind is to replace the ColoredString.CustomProcessor func ref with a base processor that supplies methods to un-/register command parsers by command name (see below for rough example). // maybe include a wrapper class for all arguments
public delegate ParseCommandBase CommandParseHandler(string args, ColoredString.ColoredGlyphEffect[] existingGlyphs, ICellSurface textSurface, ParseCommandStacks commandStacks);
// a static instance of this class should replace ColoredString.CustomProcessor
public class CommandProcessor
{
private readonly Dictionary<string, CommandParseHandler> _existingCommands;
// ...
public CommandProcessor AddCommand(string name, CommandParseHandler handler, bool replaceExisting = false)
{
// null and override checks omitted for clarity
_existingCommands[name] = handler;
return this;
}
// ...
public CommandProcessor AddAlias(string command, string alias)
{
// checks omitted for clarity
return AddCommand(alias, _existingCommands[command]);
}
// maybe add clear/remove/lookup functionality
} This would also require a slight change to command parsing in that Ultimately, this would replace the need to declare a custom command parsing function and assignment of that to ColoredString.CustomProcessor by simple fluent calls like: // ...
ColoredString.CustomProcessor
.AddCommand("custom1", (...) => new CustomCommand1())
.AddCommand("custom2", (...) => new CustomCommand2())
.AddCommand( ... )
// ... |
Beta Was this translation helpful? Give feedback.
-
Another idea is to extend the string parsing system outside of |
Beta Was this translation helpful? Give feedback.
-
Code: https://github.com/Thraka/SadConsole/blob/b94f4e2455fa32f5191df528d6fe7fb14cd57056/SadConsole/ColoredString.Parse.cs
String parsing works by checking each character in a string for the sequence of
[c:
. If that sequence is encountered, the string parser will process the contents of the string until the next]
character is encountered. The format of the command sequence is[c:name parameters]
with parameters being optional.The format for the command is
[c:name param]
. Parameters may or may not be on the command.The initial
[
can be escaped with a backtick (`) character, skipping processing. If for some reason you wanted to print[c:
then the backtick opts out and processes each character normally.Once the magic sequence is encountered. the string is processed until the closing
]
, known as the command content. If the command content contains a space, it indicates that there are parameters contained in this command. If there is no space, the string represents the command name.Running commands
As each glyph in the string is processed, it's added to the final colored string it's building. A command may affect multiple aspects of a glyph when it runs, but it can only be registered as affecting a single aspect. The following aspects are available:
While the string is being parsed, the active commands are applied to it. For example, if there is an active foreground affect, its run on every glyph encountered after the command was created. Every glyph would now have the same foreground. Commands for each aspect are put into stacks. As a new command is created that affects the same aspect as another, its put on top of the stack and is the only command for the aspect to be run. When the command is removed, any previous command is now the active command and applied to each glyph.
Adding your own commands
When the magic sequence for a command is encountered, the name of the command is checked against a built in list of commands. If found, the command is created and added to the appropriate aspect stack.
If a matching command isn't found, the command is ignored and the text of the command is added to the resulting colored string.
SadConsole does send the command data (name and parameters) to a customizable method first. If that method doesn't process the command, the internal list is checked. This is the customization point. You can override any command built into SadConsole by parsing the same name as a built in command.
Known issues
Cursor
may use the wrong styling for spaces when breaking up wordsWhen the
Cursor
is printing a colored string, the commands that built the string may be ignored when breaking words up to prevent a word being wrapped into the next line. TheCursor
uses the first character of the string as a template for the space character. The string is split by spaces and spaces are then injected between words as they're printed. If the first character has styling that is removed later in the string, the spaces keep this styling.Fix 1
Instead of using the first glyph of the string as the space template, use the last glyph of the current word.
Fix 2
Don't break words by spaces before printing the string. Instead, do the following:
Beta Was this translation helpful? Give feedback.
All reactions