Added initial support for rendering composites

This is far from complete, but it's a start
and it will enable us to create things like tables
and other complex objects in the long run.
This commit is contained in:
Patrik Svensson
2020-07-29 17:34:54 +02:00
committed by Patrik Svensson
parent e596e6eb4f
commit 8e4f33bba4
41 changed files with 1164 additions and 24 deletions

View File

@ -0,0 +1,27 @@
using System.Collections.Generic;
using System.Text;
namespace Spectre.Console.Composition
{
/// <summary>
/// Represents something that can be rendered to the console.
/// </summary>
public interface IRenderable
{
/// <summary>
/// Measures the renderable object.
/// </summary>
/// <param name="encoding">The encoding to use.</param>
/// <param name="maxWidth">The maximum allowed width.</param>
/// <returns>The width of the object.</returns>
int Measure(Encoding encoding, int maxWidth);
/// <summary>
/// Renders the object.
/// </summary>
/// <param name="encoding">The encoding to use.</param>
/// <param name="width">The width of the render area.</param>
/// <returns>A collection of segments.</returns>
IEnumerable<Segment> Render(Encoding encoding, int width);
}
}

View File

@ -0,0 +1,130 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spectre.Console.Internal;
namespace Spectre.Console.Composition
{
/// <summary>
/// Represents a renderable segment.
/// </summary>
public sealed class Segment
{
/// <summary>
/// Gets the segment text.
/// </summary>
public string Text { get; }
/// <summary>
/// Gets the appearance of the segment.
/// </summary>
public Appearance Appearance { get; }
/// <summary>
/// Initializes a new instance of the <see cref="Segment"/> class.
/// </summary>
/// <param name="text">The segment text.</param>
public Segment(string text)
: this(text, Appearance.Plain)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Segment"/> class.
/// </summary>
/// <param name="text">The segment text.</param>
/// <param name="appearance">The segment appearance.</param>
public Segment(string text, Appearance appearance)
{
Text = text?.NormalizeLineEndings() ?? throw new ArgumentNullException(nameof(text));
Appearance = appearance;
}
/// <summary>
/// Gets the number of cells that this segment
/// occupies in the console.
/// </summary>
/// <param name="encoding">The encoding to use.</param>
/// <returns>The number of cells that this segment occupies in the console.</returns>
public int CellLength(Encoding encoding)
{
return Text.CellLength(encoding);
}
/// <summary>
/// Returns a new segment without any trailing line endings.
/// </summary>
/// <returns>A new segment without any trailing line endings.</returns>
public Segment StripLineEndings()
{
return new Segment(Text.TrimEnd('\n'), Appearance);
}
/// <summary>
/// Splits the provided segments into lines.
/// </summary>
/// <param name="segments">The segments to split.</param>
/// <returns>A collection of lines.</returns>
public static List<SegmentLine> Split(IEnumerable<Segment> segments)
{
if (segments is null)
{
throw new ArgumentNullException(nameof(segments));
}
var lines = new List<SegmentLine>();
var line = new SegmentLine();
foreach (var segment in segments)
{
if (segment.Text.Contains("\n"))
{
if (segment.Text == "\n")
{
lines.Add(line);
line = new SegmentLine();
continue;
}
var text = segment.Text;
while (text != null)
{
var parts = text.SplitLines();
if (parts.Length > 0)
{
line.Add(new Segment(parts[0], segment.Appearance));
}
if (parts.Length > 1)
{
lines.Add(line);
line = new SegmentLine();
text = string.Concat(parts.Skip(1).Take(parts.Length - 1));
if (string.IsNullOrWhiteSpace(text))
{
text = null;
}
}
else
{
text = null;
}
}
}
else
{
line.Add(segment);
}
}
if (line.Count > 0)
{
lines.Add(line);
}
return lines;
}
}
}

View File

@ -0,0 +1,13 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
namespace Spectre.Console.Composition
{
/// <summary>
/// Represents a line of segments.
/// </summary>
[SuppressMessage("Naming", "CA1710:Identifiers should have correct suffix")]
public sealed class SegmentLine : List<Segment>
{
}
}