Advertisement
Guest User

Old school tree display - by configurator

a guest
Sep 12th, 2010
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.36 KB | None | 0 0
  1.     static class Dumper {
  2.         static public string Dump(Node root) { /* ... */
  3.             Worker dumper = new Worker();
  4.             dumper.Dump(root, "");
  5.             return dumper.ToString();
  6.         }
  7.  
  8.         /// <summary>
  9.         /// Does the actual work; I like Dumper being a static class since all public methods in it are static
  10.         /// </summary>
  11.         private class Worker {
  12.             private const string VerticalIndent = "│ ";
  13.             private const string EmptyVerticalIndent = "  ";
  14.             private const string ChildIndent = "├─";
  15.             private const string LastChildIndent = "└─";
  16.  
  17.             private StringBuilder output = new StringBuilder();
  18.             private List<string> indentStack = new List<string>();
  19.  
  20.             /// <summary>
  21.             /// Dumps the given node. The beforeRoot is printed before the root node's text.
  22.             /// </summary>
  23.             public void Dump(Node node, string beforeRoot) {
  24.                 Indent();
  25.                 output.Append(beforeRoot);
  26.                 output.AppendLine(node.Text);
  27.  
  28.                 if (node.Children.Count > 0)
  29.                     DumpChildren(node);
  30.             }
  31.  
  32.             /// <summary>
  33.             /// Dumps the children of the node. Assumes that there are children, i.e. node.Children.Count > 0
  34.             /// </summary>
  35.             private void DumpChildren(Node node) {
  36.                 indentStack.Push(VerticalIndent);
  37.  
  38.                 for (int i = 0; i < node.Children.Count - 1; i++) {
  39.                     Dump(node.Children[i], ChildIndent);
  40.                 }
  41.  
  42.                 indentStack.Pop();
  43.                 indentStack.Push(EmptyVerticalIndent);
  44.                 Dump(node.Children[node.Children.Count - 1], LastChildIndent);
  45.  
  46.                 indentStack.Pop();
  47.             }
  48.  
  49.             /// <summary>
  50.             /// Indents properly. Prints the indentStack, except the last item.
  51.             /// </summary>
  52.             private void Indent() {
  53.                 for (int i = 0; i < indentStack.Count - 1; i++) {
  54.                     output.Append(indentStack[i]);
  55.                 }
  56.             }
  57.  
  58.             /// <summary>
  59.             /// Returns the string representation of the dump
  60.             /// </summary>
  61.             /// <returns></returns>
  62.             public override string ToString() {
  63.                 return output.ToString();
  64.             }
  65.         }
  66.     }
  67.     /// <summary>
  68.     /// Allows us to treat a list as a stack, ordered from the first to last item.
  69.     /// Provides Push to add a last item and Pop to remove it.
  70.     /// </summary>
  71.     static class ListStackExtensions {
  72.         public static void Push<T>(this List<T> list, T item) {
  73.             list.Add(item);
  74.         }
  75.  
  76.         // I didn't bother returning the removed item because I don't need it in this case
  77.         public static void Pop<T>(this List<T> list) {
  78.             list.RemoveAt(list.Count - 1);
  79.         }
  80.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement