File Coverage

blib/lib/Term/Table/LineBreak.pm
Criterion Covered Total %
statement 56 57 98.2
branch 17 20 85.0
condition 8 9 88.8
subroutine 9 10 90.0
pod 0 4 0.0
total 90 100 90.0


line stmt bran cond sub pod time code
1             package Term::Table::LineBreak;
2 6     6   5861 use strict;
  6         11  
  6         236  
3 6     6   30 use warnings;
  6         15  
  6         516  
4              
5             our $VERSION = '0.028';
6              
7 6     6   58 use Carp qw/croak/;
  6         30  
  6         458  
8 6     6   68 use Scalar::Util qw/blessed/;
  6         11  
  6         586  
9 6     6   3012 use Term::Table::Util qw/uni_length/;
  6         54  
  6         574  
10              
11 6     6   3821 use Term::Table::HashBase qw/string gcstring _len _parts idx/;
  6         18  
  6         44  
12              
13             sub init {
14 140     140 0 239 my $self = shift;
15              
16             croak "string is a required attribute"
17 140 50       318 unless defined $self->{+STRING};
18             }
19              
20 0     0 0 0 sub columns { uni_length($_[0]->{+STRING}) }
21              
22             sub break {
23 140     140 0 157 my $self = shift;
24 140         165 my ($len) = @_;
25              
26 140         235 $self->{+_LEN} = $len;
27              
28 140         283 $self->{+IDX} = 0;
29 140         234 my $str = $self->{+STRING} . ""; # Force stringification
30              
31 140         153 my @parts;
32 140         616 my @chars = split //, $str;
33 140         222 while (@chars) {
34 264         346 my $size = 0;
35 264         562 my $part = '';
36 264         416 until ($size == $len) {
37 1826         2286 my $char = shift @chars;
38 1826 100       2618 $char = '' unless defined $char;
39              
40 1826         3134 my $l = uni_length("$char");
41 1826 100       17346 last unless $l;
42              
43 1757 50       2620 last if $char eq "\n";
44              
45 1757 100       2557 if ($size + $l > $len) {
46 1         2 unshift @chars => $char;
47 1         15 last;
48             }
49              
50 1756         1776 $size += $l;
51 1756         2725 $part .= $char;
52             }
53              
54             # If we stopped just before a newline, grab it
55 264 100 100     919 shift @chars if $size == $len && @chars && $chars[0] eq "\n";
      100        
56              
57 264         418 until ($size == $len) {
58 429         531 $part .= ' ';
59 429         582 $size += 1;
60             }
61 264         639 push @parts => $part;
62             }
63              
64 140         354 $self->{+_PARTS} = \@parts;
65             }
66              
67             sub next {
68 495     495 0 671 my $self = shift;
69              
70 495 100       712 if (@_) {
71 476         712 my ($len) = @_;
72 476 100 66     1527 $self->break($len) if !$self->{+_LEN} || $self->{+_LEN} != $len;
73             }
74             else {
75             croak "String has not yet been broken"
76 19 50       27 unless $self->{+_PARTS};
77             }
78              
79 495         676 my $idx = $self->{+IDX}++;
80 495         635 my $parts = $self->{+_PARTS};
81              
82 495 100       1044 return undef if $idx >= @$parts;
83 264         651 return $parts->[$idx];
84             }
85              
86             1;
87              
88             __END__