Files
spectre.console/src/Spectre.Console/Rendering/SegmentShape.cs
Patrik Svensson 52c1d9122b Add global usings (#668)
* Use global usings

* Fix namespace declarations for test projects
2021-12-23 16:50:31 +01:00

63 lines
1.5 KiB
C#

namespace Spectre.Console.Rendering;
internal readonly struct SegmentShape
{
public int Width { get; }
public int Height { get; }
public SegmentShape(int width, int height)
{
Width = width;
Height = height;
}
public static SegmentShape Calculate(RenderContext context, List<SegmentLine> lines)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}
if (lines is null)
{
throw new ArgumentNullException(nameof(lines));
}
var height = lines.Count;
var width = lines.Max(l => Segment.CellCount(l));
return new SegmentShape(width, height);
}
public SegmentShape Inflate(SegmentShape other)
{
return new SegmentShape(
Math.Max(Width, other.Width),
Math.Max(Height, other.Height));
}
public void Apply(RenderContext context, ref List<SegmentLine> lines)
{
foreach (var line in lines)
{
var length = Segment.CellCount(line);
var missing = Width - length;
if (missing > 0)
{
line.Add(Segment.Padding(missing));
}
}
if (lines.Count < Height && Width > 0)
{
var missing = Height - lines.Count;
for (var i = 0; i < missing; i++)
{
lines.Add(new SegmentLine
{
Segment.Padding(Width),
});
}
}
}
}