File Coverage

blib/lib/Getopt/Yath/Term.pm
Criterion Covered Total %
statement 34 39 87.1
branch 9 16 56.2
condition 3 6 50.0
subroutine 6 6 100.0
pod 1 1 100.0
total 53 68 77.9


line stmt bran cond sub pod time code
1             package Getopt::Yath::Term;
2 1     1   4 use strict;
  1         2  
  1         29  
3 1     1   3 use warnings;
  1         1  
  1         69  
4              
5             our $VERSION = '2.000007';
6              
7             our @EXPORT = qw/color USE_COLOR term_size fit_to_width/;
8 1     1   8 use Importer Importer => 'import';
  1         2  
  1         5  
9              
10 1     1   56 use Term::Table::Util qw/term_size/;
  1         1  
  1         149  
11              
12             BEGIN {
13 1 50   1   2 if (eval { require Term::ANSIColor; 1 }) {
  1         723  
  1         8080  
14 1         4 *USE_COLOR = sub() { 1 };
15 1         320 *color = \&Term::ANSIColor::color;
16             }
17             else {
18 0         0 *USE_COLOR = sub() { 0 };
19 0         0 *color = sub { '' };
  0         0  
20             }
21             }
22              
23             sub fit_to_width {
24 31     31 1 55 my ($join, $text, %params) = @_;
25              
26 31         40 my $prefix = $params{prefix};
27 31         53 my $width = $params{width};
28 31 50       63 unless (defined $width) {
29 31         55 $width = term_size() - 20;
30 31 50 33     132 $width = 80 unless $width && $width >= 80;
31             }
32              
33 31 50       76 my @parts = ref($text) ? @$text : split /\s+/, $text;
34              
35 31         34 my @out;
36              
37 31         30 my $line = "";
38 31         34 for my $part (@parts) {
39 161 100       238 my $new = $line ? "$line$join$part" : $part;
40              
41 161 50 66     266 if ($line && length($new) > $width) {
42 0         0 push @out => $line;
43 0         0 $line = $part;
44             }
45             else {
46 161         186 $line = $new;
47             }
48             }
49 31 50       56 push @out => $line if $line;
50              
51 31 50       40 if(defined $prefix) {
52 31         210 $_ =~ s/^/ /gm for @out;
53             }
54              
55 31         105 return join "\n" => @out;
56             }
57              
58             1;
59              
60             __END__