Add tree widget

This commit is contained in:
Matt Constable
2021-01-02 09:01:16 +00:00
committed by GitHub
parent 179e243214
commit b136d0299b
16 changed files with 467 additions and 16 deletions

View File

@ -0,0 +1,25 @@
using System;
namespace Spectre.Console.Rendering
{
/// <summary>
/// An ASCII rendering of a tree.
/// </summary>
public sealed class AsciiTreeRendering : ITreeRendering
{
/// <inheritdoc/>
public string GetPart(TreePart part)
{
return part switch
{
TreePart.SiblingConnector => "│ ",
TreePart.ChildBranch => "├── ",
TreePart.BottomChildBranch => "└── ",
_ => throw new ArgumentOutOfRangeException(nameof(part), part, "Unknown tree part."),
};
}
/// <inheritdoc/>
public int PartSize => 4;
}
}

View File

@ -0,0 +1,20 @@
namespace Spectre.Console.Rendering
{
/// <summary>
/// Represents the characters used to render a tree.
/// </summary>
public interface ITreeRendering
{
/// <summary>
/// Get the set of characters used to render the corresponding <see cref="TreePart"/>.
/// </summary>
/// <param name="part">The part of the tree to get rendering string for.</param>
/// <returns>Rendering string for the tree part.</returns>
string GetPart(TreePart part);
/// <summary>
/// Gets the length of all tree part strings.
/// </summary>
int PartSize { get; }
}
}

View File

@ -52,6 +52,13 @@ namespace Spectre.Console.Rendering
/// </summary>
public static Segment Empty { get; } = new Segment(string.Empty, Style.Plain, false, false);
/// <summary>
/// Creates padding segment.
/// </summary>
/// <param name="size">Number of whitespace characters.</param>
/// <returns>Segment for specified padding size.</returns>
public static Segment Padding(int size) => new(new string(' ', size));
/// <summary>
/// Initializes a new instance of the <see cref="Segment"/> class.
/// </summary>

View File

@ -48,7 +48,7 @@ namespace Spectre.Console.Rendering
var missing = Width - length;
if (missing > 0)
{
line.Add(new Segment(new string(' ', missing)));
line.Add(Segment.Padding(missing));
}
}
@ -59,7 +59,7 @@ namespace Spectre.Console.Rendering
{
lines.Add(new SegmentLine
{
new Segment(new string(' ', Width)),
Segment.Padding(Width),
});
}
}

View File

@ -0,0 +1,23 @@
namespace Spectre.Console.Rendering
{
/// <summary>
/// Defines the different rendering parts of a <see cref="Tree"/>.
/// </summary>
public enum TreePart
{
/// <summary>
/// Connection between siblings.
/// </summary>
SiblingConnector,
/// <summary>
/// Branch from parent to child.
/// </summary>
ChildBranch,
/// <summary>
/// Branch from parent to child for the last child in a set.
/// </summary>
BottomChildBranch,
}
}

View File

@ -0,0 +1,13 @@
namespace Spectre.Console.Rendering
{
/// <summary>
/// Selection of different renderings which can be used by <see cref="Tree"/>.
/// </summary>
public static class TreeRendering
{
/// <summary>
/// Gets ASCII rendering of a tree.
/// </summary>
public static ITreeRendering Ascii { get; } = new AsciiTreeRendering();
}
}