blib/lib/Text/Tree.pm

filter: Statementstmt 90.2% 102 / 113 Branchbran 78.1% 25 / 32 Conditioncond 100% 4 / 4 MC/DCmcdc 100% 2 / 2 Subroutinesub 92.3% 12 / 13 Podpod 22.2% 2 / 9 totaltotal 84.9% 147 / 173 CC 26 SCAR 17.5
CC
26
Cov
88%
layout_tree
10.0
text
7.5
center
5.1
CRAP 5.8
↑ Summary
1 # Text::Tree - format a simple tree of strings into a textual tree graph
2  
3 #----------------------------------------------------------------------------
4 #
5 # Copyright (C) 2003-2004 Ron Isaacson
6 # Portions Copyright (C) 2003 Mark Jason Dominus
7 # Portions Copyright (C) 2004 Ed Halley
8 #
9 #----------------------------------------------------------------------------
10  
11 package Text::Tree;
12 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 $VERSION = 1.0;
14  
15 =head1 NAME
16  
17 Text::Tree - format a simple tree of strings into a textual tree graph
18  
19 =head1 SYNOPSIS
20  
21 use Text::Tree;
22  
23 my $tree = new Text::Tree( "root",
24 [ "left\nnode" ],
25 [ "right", [ "1" ], [ "2" ] ] );
26 print $tree->layout("boxed");
27  
28 __OUTPUT__
29  
30 +----+
31 |root|
32 +----+
33 .---^---.
34 +----+ +-----+
35 |left| |right|
36 |node| +-----+
37 +----+ .-^-.
38 +-+ +-+
39 |1| |2|
40 +-+ +-+
41  
42 =cut
43  
44 #----------------------------------------------------------------------------
45  
46 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 sub NBSP() { "\x01" }
50  
51 =head1 METHODS
52  
53 =head2 new()
54  
55 my $tree = new Text::Tree( "label",
56 [ "left child label", [ ... ] ],
57 [ "right child label", [ ... ] );
58  
59 Create a new tree object from a nested set of array references. The
60 first element of each array must be a string used as a node label. The
61 remaining elements must each be an array reference for a child of the
62 node. Labels may contain newlines to support multiple lines of text.
63  
64 =cut
65  
66 sub new
67 5 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
68 5 my ($pack, $label, @subnodes) = @_;
69 5 my @subobjects = map { $pack->new(@$_) } @subnodes;
70 5 bless [ $label, @subobjects ] => $pack;
71 }
72  
73 =head2 layout()
74  
75 my @lines = $tree->layout( "centered in boxes" );
76 print @lines;
77  
78 Lays out the tree into an array of newline-terminated strings, ready for
79 printing or displaying. The optional style argument may contain various
80 keywords such as 'center', 'box', 'line', 'oval' and/or 'space'. These
81 style keywords affect how the tree nodes are formatted.
82  
83 =cut
84  
85 sub layout
86 2 ▶{
Subroutine layout: called CC=2 SCAR=6.9
Pod documented
87 2 my $tree = shift;
88 2 ▶ my $style = shift || undef;
Condition100%2/2
shift() || undef
l!l
11
MC/DC100%1/1
shift() || undef
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
89 2 my @lines = layout_tree($tree, $style);
90 2 return map { s/\Q@{[NBSP]}/ /g; s/\s+$//; "$_\n" } @lines;
91 }
92  
93 #----------------------------------------------------------------------------
94  
95 # Support routines.
96  
97 # Return the length of longest line in all arguments. Assumes arguments
98 # are chomped and contain a single line of text.
99  
100 sub longest
101 32 ▶{
Subroutine longest: called CC=1 SCAR=0.0
Pod undocumented
102 32 return (sort { $b <=> $a } map { length } @_)[0];
103 }
104  
105 # Ensure all lines match given width (or longest line if 0).
106 sub pad
107 53 ▶{
Subroutine pad: called CC=5 SCAR=16.1
Pod undocumented
108 53 my $want = shift;
109 53 ▶ $want = longest(@_) if not $want;
Branch: 1 branch
BranchTrueFalseTotal
unless $want 15 38 53
110 53 my @lines = @_;
111 53 for (@lines)
112 {
113 56 ▶ $_ .= ' ' x ($want-length($_))
Branch: 1 branch
BranchTrueFalseTotal
if $want > length $_ 19 37 56
114 if $want > length($_);
115 }
116 53 ▶ return @lines if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 29 24 53
117 24 return $lines[0];
118 }
119  
120 # Center and pad to the given width (or longest line if 0).
121 sub center
122 24 ▶{
Subroutine center: called CC=5 SCAR=16.2
Pod undocumented
123 24 my $want = shift;
124 24 ▶ $want = longest(@_) if not $want;
Branch: 1 branch
BranchTrueFalseTotal
unless $want 0 24 24
125 24 my @lines = @_;
126 24 for (@lines)
127 {
128 24 ▶ $_ = ' ' x (($want-length($_))/2) . $_
Branch: 1 branch
BranchTrueFalseTotal
if $want > length $_ 16 8 24
129 if $want > length($_);
130 24 $_ = pad($want, $_);
131 }
132 24 ▶ return @lines if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 24 24
133 24 return $lines[0];
134 }
135  
136 # Add box-border characters according to an 8-char style string.
137 # The characters are the four corners and four edges of the border.
138 sub border
139 5 ▶{
Subroutine border: called CC=2 SCAR=6.9
Pod undocumented
140 5 my $style = shift;
141 5 my @style = split //, $style;
142 5 my @lines = pad(0, @_);
143  
144 5 my $want = longest(@lines);
145 5 for (@lines)
146 {
147 6 $_ = $style[5] . $_ . $style[7];
148 }
149 5 unshift(@lines, $style[0] . $style[4]x$want . $style[1]);
150 5 push(@lines, $style[2] . $style[6]x$want . $style[3]);
151 5 return @lines;
152 }
153  
154 # Turn the single string label (which may have newlines) into a properly
155 # centered and/or padded array. The style argument may contain keywords
156 # to specify different aspects of the formatting. All spaces in the
157 # label are turned into special NBSP characters during layout processing.
158  
159 sub text
160 10 ▶{
Subroutine text: called CC=7 SCAR=20.2
Pod undocumented
161 10 my $self = shift;
162 10 my $label = $self->[0];
163 10 ▶ my $style = shift || '';
Condition100%2/2
shift() || ''
l!l
55
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
164  
165 # pad with spaces to width 5
166 10 my @lines = split /\n/, $label;
167  
168 10 ▶ if ($style =~ /center/)
Branch: 1 branch
BranchTrueFalseTotal
if ($style =~ /center/) { } 0 10 10
169 0 { @lines = center(0, @lines); }
170 else
171 10 { @lines = pad(0, @lines); }
172  
173 10 ▶ @lines = border(" ", @lines) if $style =~ /space/;
Branch: 1 branch
BranchTrueFalseTotal
if $style =~ /space/ 0 10 10
174 10 ▶ @lines = border('++++-|-|', @lines) if $style =~ /line|box/;
Branch: 1 branch
BranchTrueFalseTotal
if $style =~ /line|box/ 5 5 10
175 10 ▶ @lines = border("..`'-|-|", @lines) if $style =~ /oval|round/;
Branch: 1 branch
BranchTrueFalseTotal
if $style =~ /oval|round/ 0 10 10
176  
177 0 s/ /@{[NBSP]}/g for @lines;
178 10 return \@lines;
179 }
180  
181 # Return list of children trees.
182 sub children
183 10 ▶{
Subroutine children: called CC=1 SCAR=0.0
Pod undocumented
184 10 my $self = shift;
185 10 my @children = @$self;
186  
187 # throw away the label
188 10 shift @children;
189 10 return @children;
190 }
191  
192 # Lay out one subtree into a space-padded rectangle.
193 sub layout_tree
194 10 ▶{
Subroutine layout_tree: called CC=10 SCAR=23.0
Pod undocumented
195 10 my $tree = shift;
196 10 my $style = shift;
197 10 my @text = @{text($tree, $style)};
198 10 my @children = children($tree);
199  
200 # recurse depth-first, left-right through $tree, returning a
201 # downward view of the tree at each stop
202  
203 # if we're at a leaf node, then just return it; this is where the
204 # recursion stops
205 10 ▶ return @text unless @children;
Branch: 1 branch
BranchTrueFalseTotal
unless @children 6 4 10
206  
207 # build a picture of this node's children
208 4 my @out = ();
209 4 my $shift_len = 0;
210 4 foreach my $child (@children)
211 {
212 8 ▶ if (@out)
Branch: 1 branch
BranchTrueFalseTotal
if (@out) { } 4 4 8
213 {
214 # find the length of the longest line seen so far (in the
215 # picture of this node's children), and pad all the lines seen
216 # so far to that length
217 4 my $pad_len = longest(@out);
218 4 @out = map { pad($pad_len, $_) } @out;
219  
220 # get the downward picture from this child, and tack each line
221 # of that picture on to the right of the current picture
222 4 my @child = layout_tree($child, $style);
223 4 for (0 .. $#child)
224 {
225 14 ▶ $out[$_] = ' ' x $shift_len if not $out[$_];
Branch: 1 branch
BranchTrueFalseTotal
unless $out[$_] 4 10 14
226 14 $out[$_] .= ' ' . $child[$_];
227 }
228 }
229 else
230 {
231 # this is the first child seen
232 4 @out = layout_tree($child, $style);
233 }
234  
235 8 $shift_len += longest(@out);
236 }
237  
238 # now we have the picture of all of this node's children, so we need
239 # to add the text of the node itself to the top
240  
241 # we're going to want to center this node above the picture of its
242 # children, but there may be additional padding on the left side if
243 # any of those children have children of their own; so for the
244 # purposes of centering, find the space occupied only by this node's
245 # immediate children, and center the text over that
246  
247 4 my $blank = ($out[0] =~ /^( *)/)[0];
248 4 my $len0 = length $out[0];
249 4 my $center = $len0 - length($blank);
250  
251 4 ▶ if (@children == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (@children == 1) { } 0 4 4
252 {
253 # if this node has only one child, then just center a "|" above it
254 0 unshift (@out, pad($len0, $blank . center($center, "|")));
255 }
256 else
257 {
258 # if this node has multiple children, then we're not so lucky...
259 # we're going to take the first line of the existing output,
260 # duplicate it, and transform all of the cell borders into
261 # connection points
262  
263 # start by stripping off any whitespace to the left, and holding
264 # it for later
265 4 my ($pad, $lines) = ($out[0] =~ /^( *)(.*)$/);
266  
267 # replace each block of non-whitespace (ie, a cell border) with a
268 # ".", centered in the space where the border was
269 4 $lines =~ s/(\S+)/center(length($1), ".")/ge;
270  
271 # this is going to make some additional whitespace on the left, so
272 # strip that off too (and add it to what we've saved). any
273 # remaining spaces are part of the connection lines, so turn them
274 # into "-"'s.
275 4 ▶ $pad .= $1 if ($lines =~ s/^( *)//);
Branch: 1 branch
BranchTrueFalseTotal
if $lines =~ s/^( *)// 4 0 4
276 4 $lines =~ s/ *$//;
277 4 $lines =~ s/ /-/g;
278  
279 # now we have a line that connects all of the children; figure out
280 # where to attach it to its parent
281 4 my $text0 = $blank . center($center, $text[-1]);
282 4 $text0 =~ s/(\S+)/center(length($1), "x")/e;
283 4 my $pos = index($text0, "x") - length($pad);
284  
285 # attach it with a reasonable character ("+" if directly over a
286 # child's connection point, "^" otherwise)
287 4 ▶ substr($lines, $pos, 1) =
Branch: 1 branch
BranchTrueFalseTotal
substr($lines, $pos, 1) eq '.' ? : 1 3 4
288 (substr($lines, $pos, 1) eq '.' ? '+' : '^');
289  
290 # add this mess to the output
291 4 unshift @out, pad($len0, $pad . $lines);
292 }
293  
294 # now add this cell itself, properly positioned, to the output, and
295 # we're done
296  
297 4 unshift(@out, $blank . center($center, $_)) for reverse @text;
298 4 return @out;
299 }
300  
301 #----------------------------------------------------------------------------
302  
303 sub _test
304 0 ▶{
Subroutine _test: not called CC=1 SCAR=6.9
305 0 my $s = new Text::Tree( "5",
306 [ "4 1",
307 [ "3 1\n1",
308 [ "2 1\n1 1 1", [ "1 1 1 1 1" ], ], ],
309 ] );
310 0 my $t = Text::Tree->new( "5",
311 [ "4 1",
312 [ "3 1\n1",
313 [ "2 1\n1 1 1", [ "1 1 1 1 1" ], ], ],
314 [ "2 2 1", [ "1" ], $s ],
315 ],
316 [ "3 2\n1" ] );
317  
318 0 my $u = Text::Tree->new( "5",
319 [ "4 1",
320 [ "2 2 1", [ "1" ] ],
321 [ "3 1\n1",
322 [ "2 1\n1 1 1", [ "1 1 1 1 1" ], ], ],
323 ],
324 [ "3 2\n1" ] );
325 0 my $v = Text::Tree->new( "0", $t, $u );
326 0 print $v->layout();
327 0 print $/;
328  
329 0 my $tree = new Text::Tree( "root node",
330 [ "left node\nfunny node\nnode" ],
331 [ "right node", [ "r 1" ], [ "r 2" ] ] );
332 0 print $tree->layout("spaced and centered in ovals");
333 }
334  
335 1 1;
336 __END__
↑ Summary