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