Skip to content
ConsoleMonster

Show nested data as a tree

Draw nested data as a tree with New-ConsoleTree or Tree, with a status mark and a muted detail on any node and children at any depth.

A tree draws each level under its parent with connecting lines. Any node can carry a status mark and a detail, so a tree reads as a report: which site has a problem, and which server causes it.

Install-Module ConsoleMonster -Scope CurrentUser
PowerShellnested-tree.ps1
# Show nested data as a tree, with a status and a detail on any node.
Import-Module -Name ConsoleMonster

# Data: an invented site layout; Children holds the next level, as objects or plain strings.
$sites = @(
    [pscustomobject] @{ Label = 'Warsaw'; Status = 'Pass'; Detail = '2 servers'; Children = @(
            [pscustomobject] @{ Label = 'dc01'; Status = 'Pass'; Detail = 'domain controller' }
            [pscustomobject] @{ Label = 'fs01'; Status = 'Pass'; Detail = 'file server' }
        ) }
    [pscustomobject] @{ Label = 'Berlin'; Status = 'Warn'; Detail = '3 servers'; Children = @(
            [pscustomobject] @{ Label = 'dc02'; Status = 'Fail'; Detail = 'replication 3 h behind' }
            [pscustomobject] @{ Label = 'sql01'; Status = 'Warn'; Detail = 'disk at 81%' }
            [pscustomobject] @{ Label = 'web01'; Status = 'Pass'; Detail = 'web server' }
        ) }
    [pscustomobject] @{ Label = 'Lisbon'; Detail = 'planned'; Children = @('no servers yet') }
)

# Layout: the title is the root; each object's Label, Status, Detail and Children shape its branch.
$tree = New-ConsoleTree -Title '[bold]contoso.example[/]' -InputObject $sites

# Behaviour: print once.
$tree | Show-Console
Full program
# Show nested data as a tree, with a status and a detail on any node.
Import-Module -Name ConsoleMonster

# Data: an invented site layout; Children holds the next level, as objects or plain strings.
$sites = @(
    [pscustomobject] @{ Label = 'Warsaw'; Status = 'Pass'; Detail = '2 servers'; Children = @(
            [pscustomobject] @{ Label = 'dc01'; Status = 'Pass'; Detail = 'domain controller' }
            [pscustomobject] @{ Label = 'fs01'; Status = 'Pass'; Detail = 'file server' }
        ) }
    [pscustomobject] @{ Label = 'Berlin'; Status = 'Warn'; Detail = '3 servers'; Children = @(
            [pscustomobject] @{ Label = 'dc02'; Status = 'Fail'; Detail = 'replication 3 h behind' }
            [pscustomobject] @{ Label = 'sql01'; Status = 'Warn'; Detail = 'disk at 81%' }
            [pscustomobject] @{ Label = 'web01'; Status = 'Pass'; Detail = 'web server' }
        ) }
    [pscustomobject] @{ Label = 'Lisbon'; Detail = 'planned'; Children = @('no servers yet') }
)

# Layout: the title is the root; each object's Label, Status, Detail and Children shape its branch.
$tree = New-ConsoleTree -Title '[bold]contoso.example[/]' -InputObject $sites

# Behaviour: print once.
$tree | Show-Console

Run: ./nested-tree.ps1

Terminal
dotnet new console -n SiteTree
cd SiteTree
dotnet add package ConsoleMonster
C#Program.cs
// Show nested data as a tree, with a status and a detail on any node.
using ConsoleMonster;
using ConsoleMonster.Styling;
using ConsoleMonster.Widgets;

// Layout: the title is the root; Add returns the new node, so its children hang from it.
Tree tree = new Tree("[bold]contoso.example[/]");

TreeNode warsaw = tree.Add("Warsaw", StatusKind.Pass, "2 servers");
warsaw.Add("dc01", StatusKind.Pass, "domain controller");
warsaw.Add("fs01", StatusKind.Pass, "file server");

TreeNode berlin = tree.Add("Berlin", StatusKind.Warn, "3 servers");
berlin.Add("dc02", StatusKind.Fail, "replication 3 h behind");
berlin.Add("sql01", StatusKind.Warn, "disk at 81%");
berlin.Add("web01", StatusKind.Pass, "web server");

TreeNode lisbon = tree.Add("Lisbon", null, "planned");
lisbon.Add("no servers yet");

// Behaviour: print once.
Terminal.Write(tree);
Full program
// Show nested data as a tree, with a status and a detail on any node.
using ConsoleMonster;
using ConsoleMonster.Styling;
using ConsoleMonster.Widgets;

// Layout: the title is the root; Add returns the new node, so its children hang from it.
Tree tree = new Tree("[bold]contoso.example[/]");

TreeNode warsaw = tree.Add("Warsaw", StatusKind.Pass, "2 servers");
warsaw.Add("dc01", StatusKind.Pass, "domain controller");
warsaw.Add("fs01", StatusKind.Pass, "file server");

TreeNode berlin = tree.Add("Berlin", StatusKind.Warn, "3 servers");
berlin.Add("dc02", StatusKind.Fail, "replication 3 h behind");
berlin.Add("sql01", StatusKind.Warn, "disk at 81%");
berlin.Add("web01", StatusKind.Pass, "web server");

TreeNode lisbon = tree.Add("Lisbon", null, "planned");
lisbon.Add("no servers yet");

// Behaviour: print once.
Terminal.Write(tree);

Run: dotnet run

Terminal
dotnet new console -n SiteTree
cd SiteTree
dotnet add package ConsoleMonster
C# fluentProgram.cs
// Show nested data as a tree, with a status and a detail on any node.
using ConsoleMonster;
using ConsoleMonster.Styling;
using ConsoleMonster.Widgets;

// Layout: the title is the root; Add returns the new node, so its children hang from it.
Tree tree = Ui.Tree().Title("[bold]contoso.example[/]");

TreeNode warsaw = tree.Add("Warsaw", StatusKind.Pass, "2 servers");
warsaw.Add("dc01", StatusKind.Pass, "domain controller");
warsaw.Add("fs01", StatusKind.Pass, "file server");

TreeNode berlin = tree.Add("Berlin", StatusKind.Warn, "3 servers");
berlin.Add("dc02", StatusKind.Fail, "replication 3 h behind");
berlin.Add("sql01", StatusKind.Warn, "disk at 81%");
berlin.Add("web01", StatusKind.Pass, "web server");

TreeNode lisbon = tree.Add("Lisbon", null, "planned");
lisbon.Add("no servers yet");

// Behaviour: print once.
tree.Show();
Full program
// Show nested data as a tree, with a status and a detail on any node.
using ConsoleMonster;
using ConsoleMonster.Styling;
using ConsoleMonster.Widgets;

// Layout: the title is the root; Add returns the new node, so its children hang from it.
Tree tree = Ui.Tree().Title("[bold]contoso.example[/]");

TreeNode warsaw = tree.Add("Warsaw", StatusKind.Pass, "2 servers");
warsaw.Add("dc01", StatusKind.Pass, "domain controller");
warsaw.Add("fs01", StatusKind.Pass, "file server");

TreeNode berlin = tree.Add("Berlin", StatusKind.Warn, "3 servers");
berlin.Add("dc02", StatusKind.Fail, "replication 3 h behind");
berlin.Add("sql01", StatusKind.Warn, "disk at 81%");
berlin.Add("web01", StatusKind.Pass, "web server");

TreeNode lisbon = tree.Add("Lisbon", null, "planned");
lisbon.Add("no servers yet");

// Behaviour: print once.
tree.Show();

Run: dotnet run

Show-Console 80×1024-bitFull

An invented domain of three sites at 80 columns.

  • In PowerShell, objects with Label, Status, Detail and Children shape the tree. A child can be another object or a plain string. -LabelProperty, -StatusProperty, -DetailProperty and -ChildrenProperty map other names.
  • For data that is already a hierarchy, such as folders, -Children takes a script block that returns the children of $_, with -Depth to stop. Get-Item . | New-ConsoleTree -Children { if ($_.PSIsContainer) { Get-ChildItem $_.FullName } } -Depth 2 draws a folder.
  • In C#, Tree.Add and TreeNode.Add return the new node, so its children hang from it.
  • A long tree stays readable: -MaxItems lists that many children per node and ends with +N more, and -MaxNodes stops the whole tree at that many nodes with a note that the limit was reached.

Next

Edit this page on GitHub