Add tree example

This commit is contained in:
Patrik Svensson
2021-01-02 12:00:18 +01:00
committed by Patrik Svensson
parent 1aa958ced3
commit 87e6b42409
10 changed files with 150 additions and 23 deletions

View File

@ -72,7 +72,7 @@ namespace Spectre.Console
foreach (var item in items)
{
AddItem<T>(chart, item);
AddItem(chart, item);
}
return chart;

View File

@ -1,3 +1,6 @@
using System;
using Spectre.Console.Rendering;
namespace Spectre.Console
{
/// <summary>
@ -6,21 +9,68 @@ namespace Spectre.Console
public static class HasTreeNodeExtensions
{
/// <summary>
/// Adds a child to the end of the node's list of children.
/// Adds a child tree node.
/// </summary>
/// <typeparam name="T">An object type with tree nodes.</typeparam>
/// <param name="obj">The object that has tree nodes.</param>
/// <param name="child">Child to be added.</param>
/// <param name="renderable">The renderable to add.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static T AddChild<T>(this T obj, TreeNode child)
public static T AddNode<T>(this T obj, IRenderable renderable)
where T : class, IHasTreeNodes
{
if (obj is null)
{
throw new System.ArgumentNullException(nameof(obj));
throw new ArgumentNullException(nameof(obj));
}
obj.Children.Add(child);
obj.Children.Add(new TreeNode(renderable));
return obj;
}
/// <summary>
/// Adds a child tree node.
/// </summary>
/// <typeparam name="T">An object type with tree nodes.</typeparam>
/// <param name="obj">The object that has tree nodes.</param>
/// <param name="renderable">The renderable to add.</param>
/// <param name="config">An action that can be used to configure the created node further.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static T AddNode<T>(this T obj, IRenderable renderable, Action<TreeNode> config)
where T : class, IHasTreeNodes
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
if (config is null)
{
throw new ArgumentNullException(nameof(config));
}
var node = new TreeNode(renderable);
config(node);
obj.Children.Add(node);
return obj;
}
/// <summary>
/// Adds a child tree node.
/// </summary>
/// <typeparam name="T">An object type with tree nodes.</typeparam>
/// <param name="obj">The object that has tree nodes.</param>
/// <param name="node">The tree node to add.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static T AddNode<T>(this T obj, TreeNode node)
where T : class, IHasTreeNodes
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
obj.Children.Add(node);
return obj;
}
}

View File

@ -22,7 +22,7 @@ namespace Spectre.Console
{
if (obj is null)
{
throw new System.ArgumentNullException(nameof(obj));
throw new ArgumentNullException(nameof(obj));
}
obj.Alignment = alignment;