mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-19 13:28:16 +08:00
Add tree example
This commit is contained in:

committed by
Patrik Svensson

parent
1aa958ced3
commit
87e6b42409
@ -72,7 +72,7 @@ namespace Spectre.Console
|
||||
|
||||
foreach (var item in items)
|
||||
{
|
||||
AddItem<T>(chart, item);
|
||||
AddItem(chart, item);
|
||||
}
|
||||
|
||||
return chart;
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -16,8 +16,8 @@ namespace Spectre.Console.Internal
|
||||
string method, List<(string Type, string Name)> parameters,
|
||||
string? path, int? lineNumber)
|
||||
{
|
||||
Method = method ?? throw new System.ArgumentNullException(nameof(method));
|
||||
Parameters = parameters ?? throw new System.ArgumentNullException(nameof(parameters));
|
||||
Method = method ?? throw new ArgumentNullException(nameof(method));
|
||||
Parameters = parameters ?? throw new ArgumentNullException(nameof(parameters));
|
||||
Path = path;
|
||||
LineNumber = lineNumber;
|
||||
}
|
||||
|
@ -55,7 +55,7 @@ namespace Spectre.Console
|
||||
/// <param name="content">The panel content.</param>
|
||||
public Panel(IRenderable content)
|
||||
{
|
||||
_child = content ?? throw new System.ArgumentNullException(nameof(content));
|
||||
_child = content ?? throw new ArgumentNullException(nameof(content));
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
|
@ -73,7 +73,7 @@ namespace Spectre.Console
|
||||
var root = new TreeNode(Text.Empty);
|
||||
foreach (var node in Nodes)
|
||||
{
|
||||
root.AddChild(node);
|
||||
root.AddNode(node);
|
||||
}
|
||||
|
||||
return MeasureAtDepth(context, maxWidth, root, depth: 0);
|
||||
@ -97,7 +97,7 @@ namespace Spectre.Console
|
||||
var root = new TreeNode(Text.Empty);
|
||||
foreach (var node in Nodes)
|
||||
{
|
||||
root.AddChild(node);
|
||||
root.AddNode(node);
|
||||
}
|
||||
|
||||
return Enumerable.Empty<Segment>()
|
||||
|
Reference in New Issue
Block a user