mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-19 13:28:16 +08:00
Add tree widget
This commit is contained in:
25
src/Spectre.Console/Rendering/AsciiTreeRendering.cs
Normal file
25
src/Spectre.Console/Rendering/AsciiTreeRendering.cs
Normal 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;
|
||||
}
|
||||
}
|
20
src/Spectre.Console/Rendering/ITreeRendering.cs
Normal file
20
src/Spectre.Console/Rendering/ITreeRendering.cs
Normal 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; }
|
||||
}
|
||||
}
|
@ -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>
|
||||
|
@ -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),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
23
src/Spectre.Console/Rendering/TreePart.cs
Normal file
23
src/Spectre.Console/Rendering/TreePart.cs
Normal 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,
|
||||
}
|
||||
}
|
13
src/Spectre.Console/Rendering/TreeRendering.cs
Normal file
13
src/Spectre.Console/Rendering/TreeRendering.cs
Normal 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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user