Exemplo n.º 1
0
        private static IEnumerable <ConsoleStringSegment> MergeSameColor(IEnumerable <ConsoleStringSegment> segments)
        {
            var e = segments.Where(x => x.Content.Length > 0).GetEnumerator();

            if (!e.MoveNext())
            {
                yield break;
            }

            var temp = e.Current;

            while (e.MoveNext())
            {
                if (temp.Color == e.Current.Color)
                {
                    temp = new ConsoleStringSegment(temp.Content + e.Current.Content, temp.Color);
                }
                else
                {
                    yield return(temp);

                    temp = e.Current;
                }
            }

            yield return(temp);
        }
Exemplo n.º 2
0
        public ConsoleString this[Range range]
        {
            get
            {
                var(offset, length) = range.GetOffsetAndLength(Length);

                var segments = ImmutableList <ConsoleStringSegment> .Empty;

                for (int index = 0; length > 0; index++)
                {
                    if (index >= _content.Length)
                    {
                        throw new ArgumentOutOfRangeException(nameof(range), $"Index and length must refer to a location within the {nameof(ConsoleString)}.");
                    }

                    if (_content[index].Content.Length <= offset)
                    {
                        offset -= _content[index].Content.Length;
                    }
                    else
                    {
                        var segment = new ConsoleStringSegment
                                      (
                            _content[index].Content.Substring
                            (
                                offset,
                                Math.Min(_content[index].Content.Length, length) - offset
                            ),
                            _content[index].Color
                                      );

                        segments = segments.Add(segment);

                        offset  = 0;
                        length -= segment.Content.Length;
                    }
                }

                return(new ConsoleString(segments));
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Creates a new <see cref="ConsoleString"/> from a single segment.
 /// </summary>
 /// <param name="segment">The string segment.</param>
 /// <returns>A new <see cref="ConsoleString"/> defined by the single element <paramref name="segment"/>.</returns>
 public static ConsoleString Create(ConsoleStringSegment segment)
 {
     return(new ConsoleString(new[] { segment }));
 }