Do not draw tables that can't be drawn

This is a temporary fix for undrawable tables until we've
implemented a proper strategy. What this does is that it replaces
an undrawable table with an ellipsis (...). This should only
occur in either super big tables or deeply nested tables in a
console with a small buffer width.
This commit is contained in:
Patrik Svensson
2020-10-18 07:27:37 +02:00
committed by Patrik Svensson
parent a2f507e58f
commit bfffef630f
6 changed files with 155 additions and 22 deletions

View File

@ -119,6 +119,54 @@ namespace Spectre.Console.Rendering
new Segment(Text.Substring(offset, Text.Length - offset), Style));
}
/// <summary>
/// Gets the number of cells that the segments occupies in the console.
/// </summary>
/// <param name="context">The render context.</param>
/// <param name="segments">The segments to measure.</param>
/// <returns>The number of cells that the segments occupies in the console.</returns>
public static int CellLength(RenderContext context, List<Segment> segments)
{
return segments.Sum(segment => segment.CellLength(context));
}
/// <summary>
/// Truncates the segments to the specified width.
/// </summary>
/// <param name="context">The render context.</param>
/// <param name="segments">The segments to truncate.</param>
/// <param name="maxWidth">The maximum width that the segments may occupy.</param>
/// <returns>A list of segments that has been truncated.</returns>
public static List<Segment> Truncate(RenderContext context, IEnumerable<Segment> segments, int maxWidth)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}
if (segments is null)
{
throw new ArgumentNullException(nameof(segments));
}
var result = new List<Segment>();
var totalWidth = 0;
foreach (var segment in segments)
{
var segmentWidth = segment.CellLength(context);
if (totalWidth + segmentWidth > maxWidth)
{
break;
}
result.Add(segment);
totalWidth += segmentWidth;
}
return result;
}
/// <summary>
/// Splits the provided segments into lines.
/// </summary>
@ -315,11 +363,25 @@ namespace Spectre.Console.Rendering
}
else if (overflow == Overflow.Crop)
{
result.Add(new Segment(segment.Text.Substring(0, width), segment.Style));
if (Math.Max(0, width - 1) == 0)
{
result.Add(new Segment(string.Empty, segment.Style));
}
else
{
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));
if (Math.Max(0, width - 1) == 0)
{
result.Add(new Segment("…", segment.Style));
}
else
{
result.Add(new Segment(segment.Text.Substring(0, width - 1) + "…", segment.Style));
}
}
return result;