Use this package to easily create an ASCII tree from any structure or list.
Code Samples:
// SIMPLE PARENT-CHILD STRUCTURE, from Node class defined above
Node Tree = new Node() { Name = "Tree" };
Tree.Childs = new List<Node>()
{
new Node() { Name = "Child 1", Age = 1 },
new Node() { Name = "Child 2", Age = 2,
Childs = new List<Node>(){
new Node() { Name ="Child 3", Age = 3 },
new Node() { SpecialProperty = new SpecialPropertyClass(){ X = "X", Y = 1, }, Age = 4 }
}
},
new Node() { Name ="Child 5", Age = 5 }
};
The classes used in the sample above:
public class NestedClassProperty
{
public NestedClassProperty Property { get; set; }
public string Name { get; set; }
}
public class SpecialPropertyClass
{
public string X { get; set; }
public int Y { get; set; }
public override string ToString()
{
return X;
}
}
public class Node
{
public string Name { get; set; }
public int Age { get; set; }
public NestedClassProperty NestProperty { get; set; }
public SpecialPropertyClass SpecialProperty { get; set; }
public List<Node> Childs { get; set; } = new List<Node>();
}
How it looks:
WPF Example:
MainWindow.xaml.cs
private void cmdMakeAscii_Click(object sender, RoutedEventArgs e)
{
txtAscii.Text = ASCIITree.GetTree(Tree.Items, "{Header??GetType().Name}", "Items", "Tree");
}
private void cmdMakeAsciiFromVisualtree_Click(object sender, RoutedEventArgs e)
{
txtAscii.Text = ASCIITree.GetTree(this.grid, "{Header??Name??$type$.Name}", "Children??Child??Items");
}
MainWindow.xaml
<Window x:Class="WpfApp4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp4"
mc:Ignorable="d"
Title="MainWindow" Height="438.35" Width="679.045" MinHeight="200" MinWidth="500" WindowStartupLocation="CenterScreen">
<Grid Name="grid">
<TreeView HorizontalAlignment="Left" Margin="10,10,0,10" Width="254" Name="Tree">
<TreeView.Items>
<TreeViewItem Header="Root" IsExpanded="True">
<TreeViewItem.Items>
<TreeViewItem Header="" IsExpanded="True" >
<TreeViewItem.Items>
<TreeViewItem Header="Sub Sub 1" />
<TreeViewItem Header="Sub Sub 2" />
</TreeViewItem.Items>
</TreeViewItem>
<TreeViewItem Header="Sub 2" IsExpanded="True" />
<TreeViewItem Header="Sub 3" IsExpanded="True" />
</TreeViewItem.Items>
</TreeViewItem>
</TreeView.Items>
</TreeView>
<Button Name="cmdMakeAscii" Click="cmdMakeAscii_Click" Content="Generate Treeview Ascii" HorizontalAlignment="Left" Margin="269,10,0,0" VerticalAlignment="Top" Width="151"/>
<Border Margin="269,60,10,10" BorderBrush="Gray" BorderThickness="1" Padding="6">
<TextBox TextWrapping="Wrap" Text="" FontFamily="Courier New" FontSize="16" Name="txtAscii" />
</Border>
<Label Content="Ascii:" HorizontalAlignment="Left" Margin="269,34,0,0" VerticalAlignment="Top" Padding="0,5,5,5"/>
<Button x:Name="cmdMakeAsciiFromVisualtree" Click="cmdMakeAsciiFromVisualtree_Click" Content="Generate VisualTree Ascii" HorizontalAlignment="Left" Margin="425,10,0,0" VerticalAlignment="Top" Width="151"/>
</Grid>
</Window>
How it looks:
In the new version 1.0.8
ASCIITreeGenerator class
The ASCIITreeGenerator can be used with event driven formatting:
/*
CUSTOM DRAWING - WITH FORMATTING
*/
var defColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("New method, with custom drawing and formatting:");
Console.ForegroundColor = defColor;
using (GDS.ASCII.ASCIITreeGenerator ag = new GDS.ASCII.ASCIITreeGenerator())
{
ag.OnNewNode += (GDS.ASCII.Node node, ref string nodePrefix, ref string nodeDisplayText,
ref int indent, int level) =>
{
var orgForeground = Console.ForegroundColor;
ConsoleColor errColor = orgForeground;
if (node.StructureObject is Node nx)
{
if (!string.IsNullOrWhiteSpace(nx.Err))
errColor = ConsoleColor.Red;
Console.Write(nodePrefix);
Console.ForegroundColor = errColor;
Console.Write(nodeDisplayText);
if (!string.IsNullOrWhiteSpace(nx.Err))
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("<-Error found!");
}
Console.WriteLine();
Console.ForegroundColor = orgForeground;
}
else
{
Console.Write(nodePrefix);
Console.WriteLine(nodeDisplayText);
}
};
string newResult = ag.GetTree(Tree.Childs, "Name:{Name??SpecialProperty??Age}", "Childs");
}
Console.WriteLine();
Or the ref variables in the event modified:
/*
CUSTOM FORMATTING, BY MODIFYING DATA IN THE REF-EVENT
*/
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("New method, with custom formatting:");
Console.ForegroundColor = defColor;
using (GDS.ASCII.ASCIITreeGenerator ag = new GDS.ASCII.ASCIITreeGenerator())
{
ag.OnNewNode += (GDS.ASCII.Node node, ref string nodePrefix, ref string nodeDisplayText, ref int indent, int level) =>
{
if (node.StructureObject is Node nx)
{
if (!string.IsNullOrWhiteSpace(nx.Err))
{
nodeDisplayText += "<--ERROR!";
}
}
};
string newResult = ag.GetTree(Tree.Childs, "Name:{Name??SpecialProperty??Age}", "Childs");
Console.Write(newResult);
Console.WriteLine();
}
Download the demo source (ASCIITreeDemo.zip) here