mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-20 05:48:14 +08:00
Add status support
This commit is contained in:

committed by
Patrik Svensson

parent
cbed41e637
commit
501db5d287
67
src/Spectre.Console/Rendering/SegmentShape.cs
Normal file
67
src/Spectre.Console/Rendering/SegmentShape.cs
Normal file
@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
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(context, 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 SetShape(RenderContext context, List<SegmentLine> lines)
|
||||
{
|
||||
foreach (var line in lines)
|
||||
{
|
||||
var length = Segment.CellCount(context, line);
|
||||
var missing = Width - length;
|
||||
if (missing > 0)
|
||||
{
|
||||
line.Add(new Segment(new string(' ', missing)));
|
||||
}
|
||||
}
|
||||
|
||||
if (lines.Count < Height)
|
||||
{
|
||||
var missing = Height - lines.Count;
|
||||
for (var i = 0; i < missing; i++)
|
||||
{
|
||||
var line = new SegmentLine();
|
||||
line.Add(new Segment(new string(' ', Width)));
|
||||
lines.Add(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user