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# 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-ConsoleFull 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-ConsoleRun: ./nested-tree.ps1
dotnet new console -n SiteTree
cd SiteTree
dotnet add package ConsoleMonster// 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
dotnet new console -n SiteTree
cd SiteTree
dotnet add package ConsoleMonster// 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
contoso.example ├── ✓ Warsaw 2 servers │ ├── ✓ dc01 domain controller │ └── ✓ fs01 file server ├── ▲ Berlin 3 servers │ ├── ✗ dc02 replication 3 h behind │ ├── ▲ sql01 disk at 81% │ └── ✓ web01 web server └── Lisbon planned └── no servers yet
contoso.example ├── ✓ Warsaw 2 servers │ ├── ✓ dc01 domain controller │ └── ✓ fs01 file server ├── ▲ Berlin 3 servers │ ├── ✗ dc02 replication 3 h behind │ ├── ▲ sql01 disk at 81% │ └── ✓ web01 web server └── Lisbon planned └── no servers yet
An invented domain of three sites at 80 columns.
- In PowerShell, objects with
Label,Status,DetailandChildrenshape the tree. A child can be another object or a plain string.-LabelProperty,-StatusProperty,-DetailPropertyand-ChildrenPropertymap other names. - For data that is already a hierarchy, such as folders,
-Childrentakes a script block that returns the children of$_, with-Depthto stop.Get-Item . | New-ConsoleTree -Children { if ($_.PSIsContainer) { Get-ChildItem $_.FullName } } -Depth 2draws a folder. - In C#,
Tree.AddandTreeNode.Addreturn the new node, so its children hang from it. - A long tree stays readable:
-MaxItemslists that many children per node and ends with+N more, and-MaxNodesstops the whole tree at that many nodes with a note that the limit was reached.
Next
- Show pass, warn and fail results: a flat list of the same statuses.
- Widgets: the tree beside every other widget, with its options.
- Explore objects: browse a nested object interactively.