diff --git a/examples/Console/Trees/Program.cs b/examples/Console/Trees/Program.cs index 07cc974..5e46818 100644 --- a/examples/Console/Trees/Program.cs +++ b/examples/Console/Trees/Program.cs @@ -9,17 +9,17 @@ namespace TableExample var tree = new Tree(); tree.AddNode(new FigletText("Dec 2020")); - tree.AddNode(new Markup("[link]Click to go to summary[/]")); + tree.AddNode("[link]Click to go to summary[/]"); // Add the calendar nodes - tree.AddNode(new Markup("[blue]Calendar[/]"), + tree.AddNode("[blue]Calendar[/]", node => node.AddNode( new Calendar(2020, 12) .AddCalendarEvent(2020, 12, 12) .HideHeader())); // Add video games node - tree.AddNode(new Markup("[red]Played video games[/]"), + tree.AddNode("[red]Played video games[/]", node => node.AddNode( new Table() .RoundedBorder() @@ -31,8 +31,8 @@ namespace TableExample // Add the fruit nodes - tree.AddNode(new Markup("[green]Fruits[/]"), fruits => - fruits.AddNode(new Markup("Eaten"), + tree.AddNode("[green]Fruits[/]", fruits => + fruits.AddNode("Eaten", node => node.AddNode( new BarChart().Width(40) .AddItem("Apple", 12, Color.Red) diff --git a/src/Spectre.Console/Extensions/HasTreeNodeExtensions.cs b/src/Spectre.Console/Extensions/HasTreeNodeExtensions.cs index f8b2575..a45d949 100644 --- a/src/Spectre.Console/Extensions/HasTreeNodeExtensions.cs +++ b/src/Spectre.Console/Extensions/HasTreeNodeExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using Spectre.Console.Rendering; namespace Spectre.Console @@ -9,7 +10,49 @@ namespace Spectre.Console public static class HasTreeNodeExtensions { /// - /// Adds a child tree node. + /// Adds a tree node. + /// + /// An object type with tree nodes. + /// The object that has tree nodes. + /// The node's markup text. + /// The same instance so that multiple calls can be chained. + public static T AddNode(this T obj, string markup) + where T : class, IHasTreeNodes + { + if (obj is null) + { + throw new ArgumentNullException(nameof(obj)); + } + + if (markup is null) + { + throw new ArgumentNullException(nameof(markup)); + } + + return AddNode(obj, new Markup(markup)); + } + + /// + /// Adds a tree node. + /// + /// An object type with tree nodes. + /// The object that has tree nodes. + /// The node's markup text. + /// An action that can be used to configure the created node further. + /// The same instance so that multiple calls can be chained. + public static T AddNode(this T obj, string markup, Action action) + where T : class, IHasTreeNodes + { + if (markup is null) + { + throw new ArgumentNullException(nameof(markup)); + } + + return AddNode(obj, new Markup(markup), action); + } + + /// + /// Adds a tree node. /// /// An object type with tree nodes. /// The object that has tree nodes. @@ -23,19 +66,24 @@ namespace Spectre.Console throw new ArgumentNullException(nameof(obj)); } + if (renderable is null) + { + throw new ArgumentNullException(nameof(renderable)); + } + obj.Children.Add(new TreeNode(renderable)); return obj; } /// - /// Adds a child tree node. + /// Adds a tree node. /// /// An object type with tree nodes. /// The object that has tree nodes. /// The renderable to add. - /// An action that can be used to configure the created node further. + /// An action that can be used to configure the created node further. /// The same instance so that multiple calls can be chained. - public static T AddNode(this T obj, IRenderable renderable, Action config) + public static T AddNode(this T obj, IRenderable renderable, Action action) where T : class, IHasTreeNodes { if (obj is null) @@ -43,20 +91,25 @@ namespace Spectre.Console throw new ArgumentNullException(nameof(obj)); } - if (config is null) + if (renderable is null) { - throw new ArgumentNullException(nameof(config)); + throw new ArgumentNullException(nameof(renderable)); + } + + if (action is null) + { + throw new ArgumentNullException(nameof(action)); } var node = new TreeNode(renderable); - config(node); + action(node); obj.Children.Add(node); return obj; } /// - /// Adds a child tree node. + /// Adds a tree node. /// /// An object type with tree nodes. /// The object that has tree nodes. @@ -70,8 +123,61 @@ namespace Spectre.Console throw new ArgumentNullException(nameof(obj)); } + if (node is null) + { + throw new ArgumentNullException(nameof(node)); + } + obj.Children.Add(node); return obj; } + + /// + /// Add multiple tree nodes. + /// + /// An object type with tree nodes. + /// The object that has tree nodes. + /// The tree nodes to add. + /// The same instance so that multiple calls can be chained. + public static T AddNodes(this T obj, params string[] nodes) + where T : class, IHasTreeNodes + { + if (obj is null) + { + throw new ArgumentNullException(nameof(obj)); + } + + if (nodes is null) + { + throw new ArgumentNullException(nameof(nodes)); + } + + obj.Children.AddRange(nodes.Select(node => new TreeNode(new Markup(node)))); + return obj; + } + + /// + /// Add multiple tree nodes. + /// + /// An object type with tree nodes. + /// The object that has tree nodes. + /// The tree nodes to add. + /// The same instance so that multiple calls can be chained. + public static T AddNodes(this T obj, params TreeNode[] nodes) + where T : class, IHasTreeNodes + { + if (obj is null) + { + throw new ArgumentNullException(nameof(obj)); + } + + if (nodes is null) + { + throw new ArgumentNullException(nameof(nodes)); + } + + obj.Children.AddRange(nodes); + return obj; + } } }