1package Forest::Tree::Writer::ASCIIWithBranches; 2use Moose; 3 4our $VERSION = '0.10'; 5our $AUTHORITY = 'cpan:STEVAN'; 6 7with 'Forest::Tree::Writer', 8 'Forest::Tree::Roles::HasNodeFormatter'; 9 10sub as_string { 11 my ($self) = @_; 12 13 my $out = ''; 14 my @vert_dashes; 15 16 $self->tree->traverse(sub { 17 my $t = shift; 18 $out .= $self->_process_node($t, \@vert_dashes); 19 }); 20 21 return $out; 22} 23 24sub _process_node { 25 my ($self, $t, $vert_dashes) = @_; 26 27 my $depth = $t->depth; 28 my $sibling_count = $t->is_root ? 1 : $t->parent->child_count; 29 30 my @indent = map { 31 $vert_dashes->[$_] || " " 32 } 0 .. $depth - 1; 33 34 @$vert_dashes = ( 35 @indent, 36 ($sibling_count == 1 37 ? (" ") 38 : (" |")) 39 ); 40 41 if ($sibling_count == ($t->get_index_in_siblings + 1)) { 42 $vert_dashes->[$depth] = " "; 43 } 44 45 return ((join "" => @indent[1 .. $#indent]) 46 . ($depth ? " |---" : "") 47 . $self->format_node($t) 48 . "\n"); 49} 50 51__PACKAGE__->meta->make_immutable; 52 53no Moose; 1; 54 55__END__ 56 57=pod 58 59=head1 NAME 60 61Forest::Tree::Writer::ASCIIWithBranches - A slightly more complex ASCII writer 62 63=head1 SYNOPSIS 64 65 use Forest::Tree::Writer::ASCIIWithBranches; 66 67 my $w = Forest::Tree::Writer::ASCIIWithBranches->new(tree => $tree); 68 69 print $w->as_string; # outputs .... 70 # root 71 # |---1.0 72 # | |---1.1 73 # | |---1.2 74 # | |---1.2.1 75 # |---2.0 76 # | |---2.1 77 # |---3.0 78 # |---4.0 79 # |---4.1 80 # |---4.1.1 81 82=head1 DESCRIPTION 83 84=head1 METHODS 85 86=over 4 87 88=item B<> 89 90=back 91 92=head1 BUGS 93 94All complex software has bugs lurking in it, and this module is no 95exception. If you find a bug please either email me, or add the bug 96to cpan-RT. 97 98=head1 AUTHOR 99 100Stevan Little E<lt>stevan.little@iinteractive.comE<gt> 101 102=head1 COPYRIGHT AND LICENSE 103 104Copyright 2008-2014 Infinity Interactive, Inc. 105 106L<http://www.iinteractive.com> 107 108This library is free software; you can redistribute it and/or modify 109it under the same terms as Perl itself. 110 111=cut 112