A tool to write a tree structure to the console standard output stream, with colors support and a lot of customization options.
To add ConsoleTree to your project, you can use the following NuGet Package Manager command:
Install-Package ConsoleTree
More options are available on the ConsoleTree page of the NuGet Gallery website.
Implement the ITreeNode
interface to obtain the node of a tree structure. Optionally override the ToString
method to provide a custom text for the node. To write a tree structure to the console standard output stream, call the Tree.Write
method specifying its root node as parameter.
class TaxonomicRank : ITreeNode
{
public string Name { get; set; }
public List<TaxonomicRank> Members { get; set; }
public IEnumerable<ITreeNode> GetNodes() => Members;
public override string ToString() => Name;
}
var taxonomy = new TaxonomicRank
{
Name = "Felidae",
Members =
[
new TaxonomicRank { Name = "Felinae" },
new TaxonomicRank { Name = "Pantherinae" }
]
};
Tree.Write(taxonomy, new DisplaySettings { IndentSize = 2 });
// Output:
//
// Felidae
// ├──Felinae
// └──Pantherinae
If it is not possible or not desired to implement the ITreeNode
interface, then use the Tree.Write<T>
method overloads.
class TaxonomicRank
{
public string Name { get; set; }
public List<TaxonomicRank> Members { get; set; }
}
var taxonomy = new TaxonomicRank
{
Name = "Felidae",
Members =
[
new TaxonomicRank { Name = "Felinae" },
new TaxonomicRank { Name = "Pantherinae" }
]
};
Tree.Write(taxonomy, (node, _) => Console.Write(node.Name), (node, _) => node.Members, new DisplaySettings { IndentSize = 2 });
// Output:
//
// Felidae
// ├──Felinae
// └──Pantherinae
If there are multiple types of nodes in the tree structure, then use the fluent interface methods.
class Family
{
public string Name { get; set; }
public List<Subfamily> Members { get; set; }
}
class Subfamily
{
public string Name { get; set; }
}
var taxonomy = new Family
{
Name = "Felidae",
Members =
[
new Subfamily { Name = "Felinae" },
new Subfamily { Name = "Pantherinae" }
]
};
new Tree(new DisplaySettings { IndentSize = 2 })
.WriteNode<Family>((node, _) => Console.Write(node.Name))
.EnumNodes<Family, Subfamily>((node, _) => node.Members)
.WriteNode<Subfamily>((node, _) => Console.Write(node.Name))
.Write(taxonomy);
// Output:
//
// Felidae
// ├──Felinae
// └──Pantherinae
See the API reference and the ConsoleTree.Demo application to learn how to customize indentation, maximum depth, type of connectors and colors.