Example #1
0
        /// <summary>
        /// Partitions the input text into styled and unstyled pieces.
        /// </summary>
        /// <param name="input">
        /// The text to be styled.
        /// </param>
        /// <param name="args">
        /// A collection of objects that will replace the format tokens in the input string.
        /// </param>
        /// <param name="colors">
        /// </param>
        /// <returns>
        /// Returns a map relating pieces of text to their corresponding styles.
        /// </returns>
        public List <(string, Color)> GetFormatMap(
            string input,
            object[] args,
            Color[] colors)
        {
            var formatMap = new List <(string, Color)>();
            var locations = _textPattern.GetMatchLocations(input).ToList();
            var indices   = _textPattern.GetMatchesLiteral(input).ToList();

            TryExtendColors(ref args, ref colors);

            var chocolateEnd = 0;

            for (var i = 0; i < locations.Count; i++)
            {
                var styledIndex = int.Parse(indices[i].TrimStart('{').TrimEnd('}'));

                var vanillaStart = 0;
                if (i > 0)
                {
                    vanillaStart = locations[i - 1].EndIndex;
                }

                var vanillaEnd = locations[i].Index;
                chocolateEnd = locations[i].EndIndex;

                var vanilla   = input.Substring(vanillaStart, vanillaEnd - vanillaStart);
                var chocolate = args[styledIndex].ToString();

                formatMap.Add((vanilla, _defaultColor));
                formatMap.Add((chocolate, colors[styledIndex]));