/// <summary> /// Gets the number of cells that this segment /// occupies in the console. /// </summary> /// <param name="context">The render context.</param> /// <returns>The number of cells that this segment occupies in the console.</returns> public int CellLength(RenderContext context) { return(Text.CellLength(context)); }
/// <summary> /// Splits an overflowing segment into several new segments. /// </summary> /// <param name="segment">The segment to split.</param> /// <param name="overflow">The overflow strategy to use.</param> /// <param name="context">The render context.</param> /// <param name="width">The maximum width.</param> /// <returns>A list of segments that has been split.</returns> public static List <Segment> SplitOverflow(Segment segment, Overflow?overflow, RenderContext context, int width) { if (segment is null) { throw new ArgumentNullException(nameof(segment)); } if (segment.CellLength(context) <= width) { return(new List <Segment>(1) { segment }); } // Default to folding overflow ??= Overflow.Fold; var result = new List <Segment>(); if (overflow == Overflow.Fold) { var totalLength = segment.Text.CellLength(context); var lengthLeft = totalLength; while (lengthLeft > 0) { var index = totalLength - lengthLeft; // How many characters should we take? var take = Math.Min(width, totalLength - index); if (take == 0) { // This shouldn't really occur, but I don't like // never ending loops if it does... throw new InvalidOperationException("Text folding failed since 'take' was zero."); } result.Add(new Segment(segment.Text.Substring(index, take), segment.Style)); lengthLeft -= take; } } else if (overflow == Overflow.Crop) { result.Add(new Segment(segment.Text.Substring(0, width), segment.Style)); } else if (overflow == Overflow.Ellipsis) { result.Add(new Segment(segment.Text.Substring(0, width - 1) + "…", segment.Style)); } return(result); }