SadConsole.ColoredString.Parse C# (CSharp) Method

Parse() public static method

Creates a colored string by parsing commands embedded in the string.
public static Parse ( string value, int surfaceIndex = -1, Consoles surface = null, SurfaceEditor editor = null, ParseCommandStacks initialBehaviors = null ) : ColoredString
value string The string to parse.
surfaceIndex int Index of where this string will be printed.
surface Consoles The surface the string will be printed to.
editor SadConsole.Consoles.SurfaceEditor A surface editor associated with the text surface.
initialBehaviors SadConsole.StringParser.ParseCommandStacks Any initial defaults.
return ColoredString
        public static ColoredString Parse(string value, int surfaceIndex = -1, Consoles.ITextSurface surface = null, SurfaceEditor editor = null, ParseCommandStacks initialBehaviors = null)
        {
            var commandStacks = initialBehaviors != null ? initialBehaviors : new ParseCommandStacks();
            List<ColoredGlyph> glyphs = new List<ColoredGlyph>(value.Length);

            for (int i = 0; i < value.Length; i++)
            {
                var existingGlyphs = glyphs.ToArray();

                if (value[i] == '`' && i + 1 < value.Length && value[i + 1] == '[')
                    continue;

                if (value[i] == '[' && (i == 0 || value[i - 1] != '`'))
                {
                    try
                    {
                        if (i + 4 < value.Length && value[i + 1] == 'c' && value[i + 2] == ':' && value.IndexOf(']', i + 2) != -1)
                        {
                            int commandExitIndex = value.IndexOf(']', i + 2);
                            string command = value.Substring(i + 3, commandExitIndex - (i + 3));
                            string commandParams = "";

                            if (command.Contains(" "))
                            {
                                var commandSections = command.Split(new char[] { ' ' }, 2);
                                command = commandSections[0].ToLower();
                                commandParams = commandSections[1];
                            }

                            // Check for custom command
                            ParseCommandBase commandObject = CustomProcessor != null ? CustomProcessor(command, commandParams, existingGlyphs, surface, editor, commandStacks) : null;

                            // No custom command found, run build in ones
                            if (commandObject == null)
                                switch (command)
                                {
                                    case "recolor":
                                    case "r":
                                        commandObject = new ParseCommandRecolor(commandParams);
                                        break;
                                    case "mirror":
                                    case "m":
                                        commandObject = new ParseCommandSpriteEffect(commandParams);
                                        break;
                                    case "undo":
                                    case "u":
                                        commandObject = new ParseCommandUndo(commandParams, commandStacks);
                                        break;
                                    case "grad":
                                    case "g":
                                        commandObject = new ParseCommandGradient(commandParams);
                                        break;
                                    case "blink":
                                    case "b":
                                        commandObject = new ParseCommandBlink(commandParams, existingGlyphs, commandStacks, editor);
                                        break;
                                    case "sglyph":
                                    case "sg":
                                        commandObject = new ParseCommandSetGlyph(commandParams);
                                        break;
                                    default:
                                        break;
                                }

                            if (commandObject != null && commandObject.CommandType != CommandTypes.Invalid)
                            {
                                commandStacks.AddSafe(commandObject);

                                i = commandExitIndex;
                                continue;
                            }
                        }

                    }
                    catch (System.Exception e1)
                    {
            #if DEBUG
                        throw e1;
            #endif
                    }
                }

                int fixedSurfaceIndex;

                if (surfaceIndex == -1 || surface == null)
                    fixedSurfaceIndex = -1;
                else
                    fixedSurfaceIndex = i + surfaceIndex < surface.Cells.Length ? i + surfaceIndex : -1;

                ColoredGlyph newGlyph;

                if (fixedSurfaceIndex != -1)
                    newGlyph = new ColoredGlyph(surface[i + surfaceIndex]) { Glyph = value[i] };
                else
                    newGlyph = new ColoredGlyph(new Cell()) { Glyph = value[i] };

                // Foreground
                if (commandStacks.Foreground.Count != 0)
                    commandStacks.Foreground.Peek().Build(ref newGlyph, existingGlyphs, fixedSurfaceIndex, surface, editor, ref i, value, commandStacks);

                // Background
                if (commandStacks.Background.Count != 0)
                    commandStacks.Background.Peek().Build(ref newGlyph, existingGlyphs, fixedSurfaceIndex, surface, editor, ref i, value, commandStacks);

                if (commandStacks.Glyph.Count != 0)
                    commandStacks.Glyph.Peek().Build(ref newGlyph, existingGlyphs, fixedSurfaceIndex, surface, editor, ref i, value, commandStacks);

                // SpriteEffect
                if (commandStacks.SpriteEffect.Count != 0)
                    commandStacks.SpriteEffect.Peek().Build(ref newGlyph, existingGlyphs, fixedSurfaceIndex, surface, editor, ref i, value, commandStacks);

                // Effect
                if (commandStacks.Effect.Count != 0)
                    commandStacks.Effect.Peek().Build(ref newGlyph, existingGlyphs, fixedSurfaceIndex, surface, editor, ref i, value, commandStacks);

                glyphs.Add(newGlyph);
            }

            return new ColoredString(glyphs.ToArray()) { IgnoreEffect = !commandStacks.TurnOnEffects };
        }

Usage Example

        /// <summary>
        /// Creates a new instance of the ColoredString class with the specified string value, foreground and background colors, and a cell effect.
        /// </summary>
        /// <param name="value">The backing string.</param>
        /// <param name="foreground">The foreground color for each character.</param>
        /// <param name="background">The background color for each character.</param>
        /// <param name="spriteEffect">The sprite effects for each character.</param>
        public ColoredString(string value, Color foreground, Color background)
        {
            var stacks = new ParseCommandStacks();

            stacks.AddSafe(new ParseCommandRecolor()
            {
                R = foreground.R, G = foreground.G, B = foreground.B, A = foreground.A, CommandType = CommandTypes.Foreground
            });
            stacks.AddSafe(new ParseCommandRecolor()
            {
                R = background.R, G = background.G, B = background.B, A = background.A, CommandType = CommandTypes.Background
            });
            _characters = ColoredString.Parse(value, initialBehaviors: stacks)._characters;
        }
All Usage Examples Of SadConsole.ColoredString::Parse
ColoredString