Example #1
0
        public static Tuple <ConsoleSegment, ConsoleSegment> Split(ConsoleSegment segment, int lengthOfFirst)
        {
            var first  = new ConsoleSegment(segment.Text.Substring(0, lengthOfFirst), segment.ForegroundColor, segment.BackgroundColor);
            var second = new ConsoleSegment(segment.Text.Substring(lengthOfFirst), segment.ForegroundColor, segment.BackgroundColor);

            return(Tuple.Create(first, second));
        }
        public void InsertSegment(ConsoleSegment segment, int cursor)
        {
            if (segments.Count == 0)
            {
                segments.AddLast(segment);
                return;
            }

            int cursorTemp = cursor;

            LinkedListNode <ConsoleSegment> node = segments.First;

            while (true)
            {
                if (node.Value.Text.Length == cursor)
                {
                    segments.AddLast(segment);
                    return;
                }
                else if (node.Value.Text.Length > cursor)
                {
                    if (cursor > 0)
                    {
                        var split = ConsoleSegment.Split(node.Value, cursor);

                        segments.AddBefore(node, split.Item1);
                        var next = segments.AddAfter(node, split.Item2);
                        segments.Remove(node);

                        node   = next;
                        cursor = 0;
                    }

                    if (segment.Text.Length < node.Value.Text.Length)
                    {
                        var split = ConsoleSegment.Split(node.Value, segment.Text.Length);
                        node.Value = segment;
                        segments.AddAfter(node, split.Item2);
                    }
                    else
                    {
                        node.Value = segment;
                    }

                    return;
                }
                else
                {
                    cursor -= node.Value.Text.Length;
                    node    = node.Next;
                }
            }
        }