| Tree-Simple-View documentation | Contained in the Tree-Simple-View distribution. |
Tree::Simple::View::ASCII - A class for viewing Tree::Simple hierarchies in ASCII
use Tree::Simple::View::ASCII;
my $tree_view = Tree::Simple::View::ASCII->new($tree);
$tree_view->includeTrunk(1);
print $tree_view->expandAll();
# root
# |---Node Level: 1
# | |---Node Level: 1.1
# | |---Node Level: 1.2
# | | |---Node Level: 1.2.1
# | | |---Node Level: 1.2.2
# | |---Node Level: 1.3
# |---Node Level: 2
# | |---Node Level: 2.1
# | |---Node Level: 2.2
# |---Node Level: 3
# | |---Node Level: 3.1
# | |---Node Level: 3.1.1
# | |---Node Level: 3.1.2
# |---Node Level: 4
# |---Node Level: 4.1
print $tree_view->expandPath("root", "Node Level: 1", "Node Level: 1.2");
# root
# |---Node Level: 1
# | |---Node Level: 1.1
# | |---Node Level: 1.2
# | | |---Node Level: 1.2.1
# | | |---Node Level: 1.2.2
# | |---Node Level: 1.3
# |---Node Level: 2
# |---Node Level: 3
# |---Node Level: 4
This is a Tree::Simple::View subclass which provides simple ASCII formatting for trees.
I had this code lying around, and I figured it was best to put it into here. This is an early release of this, and it lacks configuration parameter handling, but that can be easily added later when I need it.
This will draw a fully expanded tree.
This currently is aliased to the expandAllSimple since we don't have config handling.
This will draw a tree with the given @path expanded.
This currently is aliased to the expandPathSimple since we don't have config handling.
Config handling, allowing you to customize the drawing. Patches welcome, I just don't currently have the time to add it.
None that I am aware of. Of course, if you find a bug, let me know, and I will be sure to fix it.
stevan little, <stevan@iinteractive.com>
Copyright 2004-2008 by Infinity Interactive, Inc.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| Tree-Simple-View documentation | Contained in the Tree-Simple-View distribution. |
package Tree::Simple::View::ASCII; use strict; use warnings; our $VERSION = '0.02'; use base 'Tree::Simple::View'; use Tree::Simple::View::Exceptions; sub expandPathSimple { my ($self, $tree, @full_path) = @_; my $output = ''; my @vert_dashes; my $traversal = sub { my ($t, $redo, $current_path, @path) = @_; $output .= $self->_processNode($t, \@vert_dashes) unless $t->isRoot; foreach my $child ($t->getAllChildren()) { if (defined $current_path && $self->_compareNodeToPath($current_path, $child)) { $output .= $redo->($child, $redo, @path); } else { $output .= $self->_processNode($child, \@vert_dashes); } } }; $output .= $self->_processNode($tree, \@vert_dashes) if $self->{include_trunk}; if ($self->{include_trunk} && defined $full_path[0] && $self->_compareNodeToPath($full_path[0], $tree)) { shift @full_path; } # Its the U combinator baby! $traversal->($tree, $traversal, @full_path); return $output; } sub expandAllSimple { my ($self) = @_; my $output = ''; my @vert_dashes; $output .= $self->_processNode($self->{tree}, \@vert_dashes) if $self->{include_trunk}; $self->{tree}->traverse(sub { my $t = shift; $output .= $self->_processNode($t, \@vert_dashes); }); return $output; } sub expandPathComplex { my ($self, $tree, undef, @full_path) = @_; # complex stuff is not supported here ... $self->expandPathSimple($tree, @full_path); } *expandAllComplex = \&expandAllSimple; sub _processNode { my ($self, $t, $vert_dashes) = @_; my $depth = $t->getDepth; my $sibling_count = $t->isRoot ? 1 : $t->getParent->getChildCount; $depth++ if $self->{include_trunk}; my @indent = map { $vert_dashes->[$_] || " " } 0 .. $depth - 1; @$vert_dashes = ( @indent, ($sibling_count == 1 ? (" ") : (" | ")) ); if ($sibling_count == ($t->getIndex + 1)) { $vert_dashes->[$depth] = " "; } my $node = exists $self->{config}->{node_formatter} ? $self->{config}->{node_formatter}->($t) : $t->getNodeValue; return ((join "" => @indent[1 .. $#indent]) . ($depth ? " |---" : "") . $node . "\n"); } 1; __END__