line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::ASCIITable::Wrap; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
@ISA=qw(Exporter); |
4
|
|
|
|
|
|
|
@EXPORT = qw(); |
5
|
|
|
|
|
|
|
@EXPORT_OK = qw(wrap); |
6
|
|
|
|
|
|
|
$VERSION = '0.2'; |
7
|
13
|
|
|
13
|
|
66
|
use Exporter; |
|
13
|
|
|
|
|
27
|
|
|
13
|
|
|
|
|
520
|
|
8
|
13
|
|
|
13
|
|
67
|
use strict; |
|
13
|
|
|
|
|
27
|
|
|
13
|
|
|
|
|
365
|
|
9
|
13
|
|
|
13
|
|
100
|
use Carp; |
|
13
|
|
|
|
|
20
|
|
|
13
|
|
|
|
|
6650
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 NAME |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Text::ASCIITable::Wrap - Wrap text |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 SHORT DESCRIPTION |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Make sure a text never gets wider than the specified width using wordwrap. |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 SYNOPSIS |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
use Text::ASCIITable::Wrap qw{ wrap }; |
22
|
|
|
|
|
|
|
print wrap('This is a long line which will be cut down to several lines',10); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 FUNCTIONS |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head2 wrap($text,$width[,$nostrict]) (exportable) |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
Wraps text at the specified width. Unless the $nostrict parameter is set, it |
29
|
|
|
|
|
|
|
will cut down the word if a word is wider than $width. Also supports text with linebreaks. |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=cut |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub wrap { |
34
|
5
|
|
|
5
|
1
|
511
|
my ($text,$width,$nostrict) = @_; |
35
|
5
|
50
|
33
|
|
|
21
|
Carp::shortmess('Missing required text or width parameter.') if (!defined($text) || !defined($width)); |
36
|
5
|
|
|
|
|
7
|
my $result=''; |
37
|
5
|
|
|
|
|
19
|
for (split(/\n/,$text)) { |
38
|
5
|
|
|
|
|
12
|
$result .= _wrap($_,$width,$nostrict)."\n"; |
39
|
|
|
|
|
|
|
} |
40
|
5
|
|
|
|
|
12
|
chop($result); |
41
|
5
|
|
|
|
|
19
|
return $result; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub _wrap { |
45
|
5
|
|
|
5
|
|
7
|
my ($text,$width,$nostrict) = @_; |
46
|
5
|
|
|
|
|
7
|
my @result; |
47
|
5
|
|
|
|
|
7
|
my $line=''; |
48
|
5
|
50
|
66
|
|
|
22
|
$nostrict = defined($nostrict) && $nostrict == 1 ? 1 : 0; |
49
|
5
|
|
|
|
|
45
|
for (split(/ /,$text)) { |
50
|
100
|
100
|
|
|
|
188
|
my $spc = $line eq '' ? 0 : 1; |
51
|
100
|
|
|
|
|
110
|
my $len = length($line); |
52
|
100
|
|
|
|
|
154
|
my $newlen = $len + $spc + length($_); |
53
|
100
|
100
|
100
|
|
|
472
|
if ($len == 0 && $newlen > $width) { |
|
|
100
|
100
|
|
|
|
|
54
|
3
|
50
|
|
|
|
11
|
push @result, $nostrict == 1 ? $_ : substr($_,0,$width); # kutt ned bredden |
55
|
3
|
|
|
|
|
7
|
$line=''; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
elsif ($len != 0 && $newlen > $width) { |
58
|
81
|
50
|
|
|
|
201
|
push @result, $nostrict == 1 ? $line : substr($line,0,$width); |
59
|
81
|
|
|
|
|
131
|
$line = $_; |
60
|
|
|
|
|
|
|
} else { |
61
|
16
|
|
|
|
|
40
|
$line .= (' ' x $spc).$_; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} |
64
|
5
|
50
|
|
|
|
28
|
push @result,$nostrict == 1 ? $line : substr($line,0,$width) if $line ne ''; |
|
|
100
|
|
|
|
|
|
65
|
5
|
|
|
|
|
38
|
return join("\n",@result); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
1; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
__END__ |