Esempio n. 1
0
        /// <summary>
        /// Parses a Character from the given string formatted like:
        /// <code>Glyph [ForeColor [BackColor]]</code>
        /// "Glyph" can either be the case-insensitive name of a value in the <see cref="Glyph"/>
        /// enum, or a single character representing the ASCII value of the glyph. ForeColor and
        /// BackColor, if present, are the case-insensitive names of values in
        /// <see cref="TerminalColors"/>.
        /// </summary>
        /// <param name="text">The text to parse.</param>
        /// <returns>A Character defined by the given text.</returns>
        /// <exception cref="ArgumentNullException"><c>text</c> is null.</exception>
        /// <exception cref="ArgumentException"><c>text</c> is empty or contains more than three words.</exception>
        public static Character Parse(string text)
        {
            if (text == null)
            {
                throw new ArgumentNullException("text");
            }
            if (text.Length == 0)
            {
                throw new ArgumentException("Argument 'text' cannot be empty.");
            }

            text = text.Trim();

            // separate out the colors and glyph
            string[] parts = text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            // only supports three parts (max)
            if (parts.Length > 3)
            {
                throw new ArgumentException("Character.Parse() should be formatted \"Glyph\", \"ForeColor Glyph\", or \"ForeColor BackColor Glyph\".");
            }

            Glyph     glyph;
            TermColor foreColor = DefaultForeColor;
            TermColor backColor = DefaultBackColor;

            // parse the glyph
            glyph = ParseGlyph(parts[parts.Length - 1]);

            // parse the fore color
            if (parts.Length > 1)
            {
                foreColor = TermColors.FromName(parts[0]);
            }

            // parse the back color
            if (parts.Length > 2)
            {
                backColor = TermColors.FromName(parts[1]);
            }

            return(new Character(glyph, foreColor, backColor));
        }
        /// <summary>
        /// Initializes a new CharacterString using the given string of ASCII characters.
        /// Color escape codes will also be parsed out of the string.
        /// </summary>
        /// <param name="text"></param>
        public CharacterString(string text, TermColor foreColor, TermColor backColor)
        {
            TermColor originalForeColor = foreColor;

            bool waitingForColor = false;

            foreach (char c in text)
            {
                // see if this character should be a color code
                if (waitingForColor)
                {
                    if (c == '-')
                    {
                        // - means "return to original color"
                        foreColor = originalForeColor;
                    }
                    else
                    {
                        foreColor = TermColors.FromEscapeChar(c);
                    }
                    waitingForColor = false;
                }
                else
                {
                    // handle color escape keys
                    if (c == '^')
                    {
                        waitingForColor = true;
                    }
                    else
                    {
                        Add(new Character(c, foreColor, backColor));
                    }
                }
            }
        }