Patrik Svensson 8261b25e5c Fix tree rendering
Fixes some tree rendering problems where lines were not properly drawn
at some levels during some circumstances.

* Change the API back to only allow one root.
* Now uses a stack based approach to rendering instead of recursion.
* Removes the need for measuring the whole tree in advance.
  Leave this up to each child to render.
2021-01-10 15:55:11 +01:00

48 lines
1.5 KiB
C#

using System;
namespace Spectre.Console
{
/// <summary>
/// Contains extension methods for <see cref="TreeNode"/>.
/// </summary>
public static class TreeNodeExtensions
{
/// <summary>
/// Expands the tree.
/// </summary>
/// <param name="node">The tree node.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static TreeNode Expand(this TreeNode node)
{
return Expand(node, true);
}
/// <summary>
/// Collapses the tree.
/// </summary>
/// <param name="node">The tree node.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static TreeNode Collapse(this TreeNode node)
{
return Expand(node, false);
}
/// <summary>
/// Sets whether or not the tree node should be expanded.
/// </summary>
/// <param name="node">The tree node.</param>
/// <param name="expand">Whether or not the tree node should be expanded.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static TreeNode Expand(this TreeNode node, bool expand)
{
if (node is null)
{
throw new ArgumentNullException(nameof(node));
}
node.Expanded = expand;
return node;
}
}
}