File Coverage

blib/lib/Tree/Visualize/ASCII/Layouts/Simple/TopDown.pm
Criterion Covered Total %
statement 42 42 100.0
branch 9 10 90.0
condition n/a
subroutine 8 8 100.0
pod 2 2 100.0
total 61 62 98.3


line stmt bran cond sub pod time code
1              
2             package Tree::Visualize::ASCII::Layouts::Simple::TopDown;
3              
4 2     2   3080 use strict;
  2         4  
  2         146  
5 2     2   12 use warnings;
  2         5  
  2         90  
6              
7             our $VERSION = '0.01';
8              
9 2     2   12 use base qw(Tree::Visualize::ASCII::Layouts::Simple);
  2         5  
  2         735  
10              
11 2     2   13 use Tree::Visualize::Exceptions;
  2         4  
  2         43  
12 2     2   11 use Tree::Visualize::ASCII::BoundingBox;
  2         5  
  2         46  
13 2     2   1307 use Tree::Visualize::Connector::Factory;
  2         5  
  2         866  
14              
15             sub drawConnections {
16 16     16 1 30 return ();
17             }
18              
19             sub assembleDrawing {
20 16     16 1 23 my ($self, $current, $children, $connections) = @_;
21            
22 16         18 my @children = @{$children};
  16         26  
23            
24 16 100       34 if (@children) {
25             # gather all the children
26 7         7 my $child_output;
27 7         13 foreach my $child (@children) {
28             # if we have no output yet then ...
29 14 100       23 unless ($child_output) {
30             # just draw the child
31 7         12 $child_output = $child;
32             }
33             # if we do have output then,...
34             else {
35             # just combine the output with
36             # the newly drawn child
37 7         20 $child_output = $child_output->padRight(" ")->pasteRight($child);
38             }
39             }
40            
41             # the size of the child output
42 7         21 my $child_output_width = $child_output->width();
43            
44             # get the padding needed
45 7         11 my ($padding, $child_padding) = (0, 0);
46 7 100       15 if ($child_output->width() > $current->width()) {
47 5         14 $padding = ($child_output->width() / 2) - ($current->width() / 2);
48             }
49             else {
50 2         5 $child_padding = ($current->width() / 2) - ($child_output->width() / 2);
51             }
52            
53 7 100       28 $current->padLeft(" " x $padding)->padRight(" " x $padding) if $padding;
54 7 50       14 $child_output->padLeft(" " x $child_padding)->padRight(" " x $child_padding) if $child_padding;
55            
56 7         20 my $first_child_line = ($child_output->getLinesAsArray())[0];
57 7         16 my $space_until = index($first_child_line, "|");
58 7         15 my $bar_uptil = $child_output->width() - rindex($first_child_line, "|");
59            
60 7         24 my $child_bar = Tree::Visualize::ASCII::BoundingBox->new(
61             (" " x $space_until) .
62             ("_" x (($child_output->width() / 2) - $space_until)) .
63             "|" .
64             ("_" x (($child_output->width() / 2) - $bar_uptil + 1)) .
65             (" " x ($bar_uptil - 1))
66             );
67            
68 7         31 $current->pasteBottom($child_bar)->pasteBottom($child_output);
69             }
70            
71 16         45 my $top_branch = Tree::Visualize::ASCII::BoundingBox->new(
72             (" " x ($current->width() / 2)) .
73             "|" .
74             (" " x ($current->width() / 2))
75             );
76 16         51 return $current->pasteTop($top_branch);
77             }
78              
79              
80             1;
81              
82             __END__