blib/lib/Mail/Thread/Chronological.pm

filter: Statementstmt 20.8% 20 / 96 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 50.0% 6 / 12 Podpod 100% 3 / 3 totaltotal 16.3% 29 / 177 CC 33 SCAR 57.0
CC
33
Cov
13%
arrange
552.0
_draw_cells
42.0
_cell
30.0
CRAP 298.2
↑ Summary
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Mail::Thread::Chronological;
3 1 ▶use Mail::Thread ();
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Date::Parse qw( str2time );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use List::Util qw( max );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use vars qw/$VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 $VERSION = '1.22';
8  
9 1 ▶use constant debug => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 Mail::Thread::Chronological - rearrange Mail::Thread::Containers into a Chronological structure
14  
15 =head1 SYNOPSIS
16  
17 use Mail::Thread;
18 use Mail::Thread::Chronological;
19  
20 my $threader = Mail::Thread->new( @messages );
21 my $lurker = Mail::Thread::Chronological->new;
22  
23 $threader->thread;
24  
25 for my $thread ($threader->rootset) {
26 for my $row ( $lurker->arrange( $thread ) ) {
27 my $container = grep { ref $_ } @$row;
28 print join('', map { ref $_ ? '*' : $_ } @$row),
29 " ", $container->messageid, "\n";
30 }
31 }
32  
33 =head1 DESCRIPTION
34  
35 Given a Mail::Thread::Container, Mail::Thread::Chronological transforms the
36 tree structure into a 2-dimensional array representing the history of
37 a given thread in time.
38  
39 The output is similar to that of the Lurker mail archiving system,
40 with a couple of small exceptions:
41  
42 =over
43  
44 =item Characters used
45  
46 The grid is populated with the characters ' ' (space), '-', '+', '|',
47 '{', or Mail::Thread::Container objects. Lurker uses [a-g], and
48 differentiates T-junctions from corners for you, this module assumes
49 you will do that for yourself.
50  
51 The characters mean:
52  
53 =over
54  
55 =item space
56  
57 empty cell
58  
59 =item -
60  
61 horizontal line
62  
63 =item +
64  
65 T junction or corner
66  
67 =item |
68  
69 vertical line
70  
71 =item {
72  
73 vertical line crossing over a horizontal line
74  
75 =back
76  
77 =item Vertical stream crossing is permitted
78  
79 In the original lurker crossing a path vertically is not allowed, this
80 results in a lot of horizontal space being used.
81  
82 =back
83  
84 =head1 METHODS
85  
86 =head2 new
87  
88 your common or garden constructor
89  
90 =cut
91  
92 0 ▶sub new { bless {}, $_[0] }
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
93  
94 =head2 arrange
95  
96 Returns an array of arrays representing the thread tree.
97  
98 =cut
99  
100  
101 # identify the co-ordinates of something
102 0 ▶sub _cell {
Subroutine _cell: not called CC=5 SCAR=34.0
103 0 my $cells = shift;
104 0 my $find = shift;
105 0 ▶ for (my $y = 0; $y < @$cells; ++$y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < @$cells) 0 0 0
106 0 ▶ for (my $x = 0; $x < @{ $cells->[$y] }; ++$x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < @{$cells->[$y];}) 0 0 0
107 0 my $here = $cells->[$y][$x];
108 0 ▶ return [$y, $x] if ref $here && $here == $find;
Branch: 1 branch
BranchTrueFalseTotal
if ref $here and $here == $find 0 0 0
Condition0%0/3
ref $here and $here == $find
!ll&&!rl&&r
000
MC/DC0%0/2
ref $here and $here == $find
ref $here $here == $find
Truth table0%0/3
A and B
A= ref $hereB= $here == $find
ABresult
0X0
100
111
109 }
110 }
111 0 return;
112 }
113  
114 0 ▶sub _draw_cells {
Subroutine _draw_cells: not called CC=6 SCAR=37.4
115 0 my $cells = shift;
116 # and again in their new state
117 0 print map { $_ % 10 } 0..20;
118 0 print "\n";
119 0 for my $row (@$cells) {
120 0 my $this;
121 0 for (@$row) {
122 0 ▶ $this = $_ if ref $_;
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ 0 0 0
123 0 ▶ print ref $_ ? '*' : $_ ? $_ : ' ';
Branch: 2 branches
BranchTrueFalseTotal
ref $_ ? : 0 0 0
$_ ? : 0 0 0
124 }
125 0 print "\t", $this->messageid, "\n";
126 }
127 0 print "\n";
128 }
129  
130 0 ▶sub arrange {
Subroutine arrange: not called CC=23 SCAR=63.1
Pod documented
131 0 my $self = shift;
132 0 my $thread = shift;
133  
134 # show them in the old order, and take a copy of the containers
135 # with messages on while we're at it
136 0 my @messages;
137 $thread->iterate_down(
138 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
139 0 my ($c, $d) = @_;
140 0 print ' ' x $d, $c->messageid, "\n" if debug;
141 0 ▶ push @messages, $c if $c->message;
Branch: 1 branch
BranchTrueFalseTotal
if $c->message 0 0 0
142 0 } );
143  
144 # cells is the 2-d representation, row, col. the first
145 # message will be at [0][0], it's first reply, [0][1]
146 0 my @cells;
147  
148 # okay, wander them in date order
149 0 @messages = sort { $self->extract_time( $a ) <=>
150 $self->extract_time( $b ) } @messages;
151 0 ▶ for (my $row = 0; $row < @messages; ++$row) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row < @messages) 0 0 0
152 0 my $c = $messages[$row];
153 # and place them in cells
154  
155 # the first one - [0][0]
156 0 ▶ unless (@cells) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@cells) 0 0 0
157 0 $cells[$row][0] = $c;
158 0 next;
159 }
160  
161 # look up our parent
162 0 my $first_parent = $c->parent;
163 0 ▶ while ($first_parent && !$first_parent->message) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_parent and not $first_parent->message) 0 0 0
Condition0%0/3
$first_parent and not $first_parent->message
!ll&&!rl&&r
000
MC/DC0%0/2
$first_parent and not $first_parent->message
$first_parent not $first_parent->message
Truth table0%0/3
A and not(B)
A= $first_parentB= not $first_parent->message
ABresult
0X0
100
111
164 0 $first_parent = $first_parent->parent;
165 }
166  
167 0 ▶ unless ($first_parent && $first_parent->message &&
Branch: 1 branch
BranchTrueFalseTotal
unless ($first_parent and $first_parent->message and _cell(\@cells, $first_parent)) 0 0 0
Condition0%0/6
$first_parent and $first_parent->message and _cell(\@cells, $first_parent)
!ll&&!rl&&r
000
$first_parent and $first_parent->message
!ll&&!rl&&r
000
MC/DC0%0/3
$first_parent and $first_parent->message and _cell(\@cells, $first_parent)
$first_parent $first_parent->message _cell(\@cells, $first_parent)
Truth table0%0/4
A and B and C
A= $first_parentB= $first_parent->messageC= _cell(\@cells, $first_parent)
ABCresult
0XX0
10X0
1100
1111
168 _cell(\@cells, $first_parent) ) {
169 # just drop it randomly to one side, since it doesn't
170 # have a clearly identifiable parent
171 0 my $col = (max map { scalar @$_ } @cells );
172 0 $cells[$row][$col] = $c;
173 0 next;
174 }
175 0 my $col;
176 0 my ($parent_row, $parent_col) = @{ _cell( \@cells, $first_parent ) };
177 0 ▶ if ($first_parent->child == $c) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_parent->child == $c) { } 0 0 0
178 # if we're the first child, then we directly beneath
179 # them
180 0 $col = $parent_col;
181 }
182 else {
183 # otherwise, we have to shuffle accross into the first
184 # free column
185  
186 # okay, figure out what the max col is
187 0 $col = my $max_col = (max map { scalar @$_ } @cells );
188  
189 # would drawing the simple horizontal line cross the streams?
190 0 ▶ if (grep {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {($cells[$parent_row][$_] || "") eq "|";} $parent_col + 1 .. $max_col) 0 0 0
191 0 ▶ ($cells[$parent_row][$_] || '') eq '|'
Condition0%0/2
$cells[$parent_row][$_] || ''
l!l
00
MC/DC0%0/1
$cells[$parent_row][$_] || ''
$cells[$parent_row][$_]
Truth table0%0/2
A
A= $cells[$parent_row][$_]
Aresult
11
00
192 } $parent_col+1..$max_col) {
193 # we must not cross the streams (that would be bad).
194 # if given this tree:
195 # a + +
196 # b | |
197 # c |
198 # d
199 #
200 # e arrives, and is a reply to b, we can't just do this:
201 # a + +
202 # b - - +
203 # c | |
204 # d |
205 # e
206 #
207 # it's messy and confusing. instead we have to do
208 # extra work so we end up at
209 # a - + +
210 # b + | |
211 # | c |
212 # | d
213 # e
214  
215 0 print "Crossing the streams, horizontally\n" if debug;
216 # we want to end up in $parent_col + 1 and
217 # everything in that column needs to get shuffled
218 # over one
219 0 $col = $parent_col + 1;
220 0 for my $r (@cells[0 .. $row - 1]) {
221 0 ▶ next if @$r < $col;
Branch: 1 branch
BranchTrueFalseTotal
if @$r < $col 0 0 0
222 0 ▶ my $here = $r->[$col] || '';
Condition0%0/2
$r->[$col] || ''
l!l
00
MC/DC0%0/1
$r->[$col] || ''
$r->[$col]
Truth table0%0/2
A
A= $r->[$col]
Aresult
11
00
223 # what to splice in
224 0 ▶ my $splice = $here =~/[+\-]/ ? '-' : ' ';
Branch: 1 branch
BranchTrueFalseTotal
$here =~ /[+\-]/ ? : 0 0 0
225 0 splice(@$r, $col, 0, $splice);
226 }
227 0 $col = $parent_col + 1;
228 }
229  
230 # the path is now clear, add the line in
231 0 for ($parent_col..$col) {
232 0 ▶ $cells[$parent_row][$_] ||= '-';
Condition0%0/2
$cells[$parent_row][$_] ||= "-"
l!l
00
MC/DC0%0/1
$cells[$parent_row][$_] ||= "-"
$cells[$parent_row][$_]
Truth table0%0/2
A
A= $cells[$parent_row][$_]
Aresult
11
00
233 }
234 0 $cells[$parent_row][$col] = '+';
235 }
236  
237 # place the message
238 0 $cells[$row][$col] = $c;
239 # link with vertical dashes
240 0 for ($parent_row+1..$row-1) {
241 0 ▶ $cells[$_][$col] = ($cells[$_][$col] || '') eq '-' ? '{' : '|';
Branch: 1 branch
BranchTrueFalseTotal
($cells[$_][$col] || '') eq '-' ? : 0 0 0
Condition0%0/2
$cells[$_][$col] || ''
l!l
00
MC/DC0%0/1
$cells[$_][$col] || ''
$cells[$_][$col]
Truth table0%0/2
A
A= $cells[$_][$col]
Aresult
11
00
242 }
243 0 _draw_cells(\@cells) if debug;
244 }
245  
246 # pad the rows with spaces
247 0 my $maxcol = max map { scalar @$_ } @cells;
248 0 for my $row (@cells) {
249 0 ▶ $row->[$_] ||= ' ' for (0..$maxcol-1);
Condition0%0/2
$row->[$_] ||= " "
l!l
00
MC/DC0%0/1
$row->[$_] ||= " "
$row->[$_]
Truth table0%0/2
A
A= $row->[$_]
Aresult
11
00
250 }
251  
252 0 return @cells;
253 }
254  
255 =head2 extract_time( $container )
256  
257 Extracts the time from a Mail::Thread::Container, returned as epoch
258 seconds used to decide the order of adding messages to the rows.
259  
260 =cut
261  
262 0 ▶sub extract_time {
Subroutine extract_time: not called CC=1 SCAR=6.9
Pod documented
263 0 my $self = shift;
264 0 my $container = shift;
265  
266 0 my $date = Mail::Thread->_get_hdr( $container->message, 'date' );
267 0 return str2time( $date );
268 }
269  
270 1 1;
271 __END__
↑ Summary