blib/lib/Term/Fabulous/Widget/Table/Mutator.pm

filter: Statementstmt 100% 126 / 126 Branchbran 70.2% 52 / 74 Conditioncond 61.5% 24 / 39 MC/DCmcdc 34.7% 8 / 23 Subroutinesub 100% 33 / 33 Podpod 100% 11 / 11 totaltotal 83.0% 254 / 306 CC 70 SCAR 16.3
CC
70
Cov
85%
__ANON__
11.1
_non_negative_integer
10.0
__ANON__
8.2
CRAP 5.1
« blib/lib/Term/Fabulous/Color.pm ↑ Summary blib/lib/Term/Fabulous/Widget/Sixel.pm »
1 package Term::Fabulous::Widget::Table::Mutator;
2  
3 1 ▶use v5.32;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '0.05';
9  
10 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 our @EXPORT_OK = qw(datetime date number percent bytes duration boolean lookup truncate sprintf_format chain);
12 1 our %EXPORT_TAGS = ( all => \@EXPORT_OK );
13  
14 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Term::Fabulous::Widget::Table::Value qw(is_blank number_of date_epoch);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Term::Fabulous::Unicode qw(ELLIPSIS fit_columns string_columns);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 26 ▶sub _options ( $function, $known, %options ) {
Subroutine _options: called CC=2 SCAR=6.9
20 26 my @unknown = grep { !exists $known->{$_} } sort keys %options;
21 26 ▶ croak "Term::Fabulous::Widget::Table::Mutator: $function does not accept @unknown (known: " . join( ', ', sort keys %$known ) . ")" if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 1 25 26
22 25 return { %$known, %options };
23 }
24  
25 16 ▶sub _non_negative_integer ( $function, $name, $value ) {
Subroutine _non_negative_integer: called CC=5 SCAR=23.0
26 16 ▶ croak "Term::Fabulous::Widget::Table::Mutator: $function $name must be a non-negative integer, got " . ( defined $value ? "'$value'" : 'undef' )
Branch: 2 branches
BranchTrueFalseTotal
unless defined $value and not ref $value and $value =~ /\A[0-9]+\z/u 0 16 16
defined $value ? : 0 0 0
Condition33%2/6
defined $value and not ref $value and $value =~ /\A[0-9]+\z/u
!ll&&!rl&&r
0016
defined $value and not ref $value
!ll&&!rl&&r
0016
MC/DC0%0/3
defined $value and not ref $value and $value =~ /\A[0-9]+\z/u
defined $value not ref $value $value =~ /\A[0-9]+\z/u
Truth table25%1/4
A and not(B) and C
A= defined $valueB= not ref $valueC= $value =~ /\A[0-9]+\z/u
ABCresult
0XX0
10X0
1100
1111
27 unless defined $value && !ref $value && $value =~ /\A[0-9]+\z/;
28 16 return $value + 0;
29 }
30  
31 # A number with a fixed number of decimals and a separator between groups
32 # of three digits.
33 5 ▶sub _format_number ( $number, $decimals, $separator, $point ) {
Subroutine _format_number: called CC=5 SCAR=16.2
34 5 my $text = sprintf "%.${decimals}f", $number;
35 5 my ( $sign, $whole, $fraction ) = $text =~ /\A(-?)([0-9]+)(?:\.([0-9]+))?\z/;
36 5 ▶ $sign = '' if $text =~ /\A-0(?:\.0*)?\z/; # no "-0.00"
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\A-0(?:\.0*)?\z/u 1 4 5
37 5 ▶ 1 while length $separator && $whole =~ s/\A([0-9]+)([0-9]{3})/$1$separator$2/;
Branch: 1 branch
BranchTrueFalseTotal
length $separator and $whole =~ s/\A([0-9]+)([0-9]{3})/$1$separator$2/u and do { '???' } 3 5 8
Condition66%2/3
length $separator and $whole =~ s/\A([0-9]+)([0-9]{3})/$1$separator$2/u
!ll&&!rl&&r
053
MC/DC50%1/2
length $separator and $whole =~ s/\A([0-9]+)([0-9]{3})/$1$separator$2/u
length $separator $whole =~ s/\A([0-9]+)([0-9]{3})/$1$separator$2/u
Truth table66%2/3
A and B
A= length $separatorB= $whole =~ s/\A([0-9]+)([0-9]{3})/$1$separator$2/u
ABresult
0X0
100
111
38 5 ▶ return $sign . $whole . ( defined $fraction ? $point . $fraction : '' );
Branch: 1 branch
BranchTrueFalseTotal
defined $fraction ? : 5 0 5
39 }
40  
41 4 ▶sub datetime ( $format = '%Y-%m-%d %H:%M', %options ) {
Subroutine datetime: called CC=4 SCAR=14.3
Pod documented
42 4 my $settings = _options( datetime => { utc => 0 }, %options );
43 4 ▶ croak "Term::Fabulous::Widget::Table::Mutator: datetime needs a strftime format string" if ref $format || !length $format;
Branch: 1 branch
BranchTrueFalseTotal
if ref $format or not length $format 0 4 4
Condition33%1/3
ref $format or not length $format
l!l&&r!l&&!r
004
MC/DC0%0/2
ref $format or not length $format
ref $format not length $format
Truth table33%1/3
A or not(B)
A= ref $formatB= not length $format
ABresult
1X1
011
000
44 4 ▶ return sub ( $value, $row = undef ) {
Subroutine __ANON__: called CC=5 SCAR=16.2
45 4 ▶ my $epoch = date_epoch($value) // return is_blank($value) ? '' : "$value";
Branch: 1 branch
BranchTrueFalseTotal
is_blank($value) ? : 1 0 1
Condition100%2/2
date_epoch($value) // (return is_blank($value) ? "" : "$value")
l!l
31
MC/DC100%1/1
date_epoch($value) // (return is_blank($value) ? "" : "$value")
date_epoch($value)
Truth table100%2/2
A
A= date_epoch($value)
Aresult
11
00
46 3 ▶ return POSIX::strftime( $format, $settings->{utc} ? gmtime $epoch : localtime $epoch );
Branch: 1 branch
BranchTrueFalseTotal
$settings->{'utc'} ? : 1 2 3
47 4 };
48 }
49  
50 1 ▶sub date ( $format = '%Y-%m-%d', %options ) {
Subroutine date: called CC=2 SCAR=6.9
Pod documented
51 1 return datetime( $format, %options );
52 }
53  
54 7 ▶sub number (%options) {
Subroutine number: called CC=2 SCAR=7.0
Pod documented
55 7 my $settings = _options( number => { decimals => undef, separator => ',', point => '.', prefix => '', suffix => '' }, %options );
56 6 ▶ $settings->{decimals} = _non_negative_integer( number => decimals => $settings->{decimals} ) if defined $settings->{decimals};
Branch: 1 branch
BranchTrueFalseTotal
if defined $settings->{"decimals"} 3 3 6
57 6 ▶ return sub ( $value, $row = undef ) {
Subroutine __ANON__: called CC=6 SCAR=18.1
58 6 ▶ my $number = number_of($value) // return is_blank($value) ? '' : "$value";
Branch: 1 branch
BranchTrueFalseTotal
is_blank($value) ? : 1 1 2
Condition100%2/2
number_of($value) // (return is_blank($value) ? "" : "$value")
l!l
42
MC/DC100%1/1
number_of($value) // (return is_blank($value) ? "" : "$value")
number_of($value)
Truth table100%2/2
A
A= number_of($value)
Aresult
11
00
59 4 ▶ my $decimals = $settings->{decimals} // ( "$number" =~ /\.([0-9]+)\z/ ? length $1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
"$number" =~ /\.([0-9]+)\z/u ? : 1 0 1
Condition66%2/3
$settings->{"decimals"} // ("$number" =~ /\.([0-9]+)\z/u ? length $1 : 0)
l!l&&r!l&&!r
310
MC/DC0%0/2
$settings->{"decimals"} // ("$number" =~ /\.([0-9]+)\z/u ? length $1 : 0)
$settings->{"decimals"} ("$number" =~ /\.([0-9]+)\z/u ? length $1 : 0)
Truth table66%2/3
A // B
A= $settings->{"decimals"}B= ("$number" =~ /\.([0-9]+)\z/u ? length $1 : 0)
ABresult
1X1
011
000
60 4 return $settings->{prefix} . _format_number( $number, $decimals, $settings->{separator}, $settings->{point} ) . $settings->{suffix};
61 6 };
62 }
63  
64 1 ▶sub percent (%options) {
Subroutine percent: called CC=2 SCAR=7.5
Pod documented
65 1 my $settings = _options( percent => { decimals => 0, scale => 100, separator => ',', point => '.' }, %options );
66 1 $settings->{decimals} = _non_negative_integer( percent => decimals => $settings->{decimals} );
67 1 ▶ croak "Term::Fabulous::Widget::Table::Mutator: percent scale must be a number" unless defined number_of( $settings->{scale} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined number_of($settings->{"scale"}) 0 1 1
68 1 ▶ return sub ( $value, $row = undef ) {
Subroutine __ANON__: called CC=4 SCAR=16.6
69 1 ▶ my $number = number_of($value) // return is_blank($value) ? '' : "$value";
Branch: 1 branch
BranchTrueFalseTotal
is_blank($value) ? : 0 0 0
Condition50%1/2
number_of($value) // (return is_blank($value) ? "" : "$value")
l!l
10
MC/DC0%0/1
number_of($value) // (return is_blank($value) ? "" : "$value")
number_of($value)
Truth table50%1/2
A
A= number_of($value)
Aresult
11
00
70 1 return _format_number( $number * $settings->{scale}, $settings->{decimals}, $settings->{separator}, $settings->{point} ) . '%';
71 1 };
72 }
73  
74 4 ▶sub bytes (%options) {
Subroutine bytes: called CC=2 SCAR=7.0
Pod documented
75 4 my $settings = _options( bytes => { decimals => 1, binary => 1 }, %options );
76 4 $settings->{decimals} = _non_negative_integer( bytes => decimals => $settings->{decimals} );
77 4 ▶ my ( $step, @units ) = $settings->{binary} ? ( 1024, qw(B KiB MiB GiB TiB PiB EiB) ) : ( 1000, qw(B kB MB GB TB PB EB) );
Branch: 1 branch
BranchTrueFalseTotal
$settings->{'binary'} ? : 3 1 4
78 4 ▶ return sub ( $value, $row = undef ) {
Subroutine __ANON__: called CC=8 SCAR=21.0
79 4 ▶ my $number = number_of($value) // return is_blank($value) ? '' : "$value";
Branch: 1 branch
BranchTrueFalseTotal
is_blank($value) ? : 1 0 1
Condition100%2/2
number_of($value) // (return is_blank($value) ? "" : "$value")
l!l
31
MC/DC100%1/1
number_of($value) // (return is_blank($value) ? "" : "$value")
number_of($value)
Truth table100%2/2
A
A= number_of($value)
Aresult
11
00
80 3 my ( $size, $unit ) = ( abs $number, 0 );
81 3 ▶ while ( $size >= $step && $unit < $#units ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size >= $step and $unit < $#units) 3 3 6
Condition66%2/3
$size >= $step and $unit < $#units
!ll&&!rl&&r
303
MC/DC50%1/2
$size >= $step and $unit < $#units
$size >= $step $unit < $#units
Truth table66%2/3
A and B
A= $size >= $stepB= $unit < $#units
ABresult
0X0
100
111
82 3 $size /= $step;
83 3 $unit++;
84 }
85 3 ▶ my $sign = $number < 0 ? '-' : '';
Branch: 1 branch
BranchTrueFalseTotal
$number < 0 ? : 0 3 3
86 3 ▶ return $unit == 0 ? "$sign$size $units[0]" : sprintf( "%s%.$settings->{decimals}f %s", $sign, $size, $units[$unit] );
Branch: 1 branch
BranchTrueFalseTotal
$unit == 0 ? : 1 2 3
87 4 };
88 }
89  
90 3 ▶sub duration (%options) {
Subroutine duration: called CC=2 SCAR=7.0
Pod documented
91 3 my $settings = _options( duration => { parts => 2 }, %options );
92 3 my $parts = _non_negative_integer( duration => parts => $settings->{parts} );
93 3 ▶ croak "Term::Fabulous::Widget::Table::Mutator: duration parts must be at least 1" unless $parts >= 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $parts >= 1 0 3 3
94 3 my @units = ( [ d => 86400 ], [ h => 3600 ], [ m => 60 ], [ s => 1 ] );
95 3 ▶ return sub ( $value, $row = undef ) {
Subroutine __ANON__: called CC=11 SCAR=24.1
96 3 ▶ my $number = number_of($value) // return is_blank($value) ? '' : "$value";
Branch: 1 branch
BranchTrueFalseTotal
is_blank($value) ? : 1 0 1
Condition100%2/2
number_of($value) // (return is_blank($value) ? "" : "$value")
l!l
21
MC/DC100%1/1
number_of($value) // (return is_blank($value) ? "" : "$value")
number_of($value)
Truth table100%2/2
A
A= number_of($value)
Aresult
11
00
97 2 my $left = int abs $number;
98 2 my @shown;
99 2 foreach my $unit (@units) {
100 6 my ( $name, $seconds ) = @$unit;
101 6 my $count = int( $left / $seconds );
102 6 $left -= $count * $seconds;
103 6 ▶ push @shown, ( @shown ? sprintf( '%02d', $count ) : $count ) . $name if $count || @shown;
Branch: 2 branches
BranchTrueFalseTotal
if $count or @shown 5 1 6
@shown ? : 3 2 5
Condition66%2/3
$count or @shown
l!l&&r!l&&!r
501
MC/DC50%1/2
$count or @shown
$count @shown
Truth table66%2/3
A or B
A= $countB= @shown
ABresult
1X1
011
000
104 6 ▶ last if @shown == $parts;
Branch: 1 branch
BranchTrueFalseTotal
if @shown == $parts 2 4 6
105 }
106 2 ▶ @shown = ('0s') unless @shown;
Branch: 1 branch
BranchTrueFalseTotal
unless @shown 0 2 2
107 2 ▶ return ( $number < 0 ? '-' : '' ) . join ' ', @shown;
Branch: 1 branch
BranchTrueFalseTotal
$number < 0 ? : 1 1 2
108 3 };
109 }
110  
111 2 ▶sub boolean ( $true_text = 'yes', $false_text = 'no' ) {
Subroutine boolean: called CC=5 SCAR=16.8
Pod documented
112 2 ▶ croak "Term::Fabulous::Widget::Table::Mutator: boolean takes two strings" if ref $true_text || ref $false_text;
Branch: 1 branch
BranchTrueFalseTotal
if ref $true_text or ref $false_text 0 2 2
Condition33%1/3
ref $true_text or ref $false_text
l!l&&r!l&&!r
002
MC/DC0%0/2
ref $true_text or ref $false_text
ref $true_text ref $false_text
Truth table33%1/3
A or B
A= ref $true_textB= ref $false_text
ABresult
1X1
011
000
113 2 ▶ return sub ( $value, $row = undef ) {
Subroutine __ANON__: called CC=4 SCAR=14.0
114 2 ▶ return '' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 2 2
115 2 ▶ return $value ? $true_text : $false_text;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 1 1 2
116 2 };
117 }
118  
119 2 ▶sub lookup ( $table, %options ) {
Subroutine lookup: called CC=2 SCAR=7.1
Pod documented
120 2 ▶ croak "Term::Fabulous::Widget::Table::Mutator: lookup needs a hash reference" unless ref $table eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $table eq "HASH" 0 2 2
121 2 my $settings = _options( lookup => { default => undef }, %options );
122 2 my %text_of = %$table;
123 2 ▶ return sub ( $value, $row = undef ) {
Subroutine __ANON__: called CC=5 SCAR=16.5
124 2 ▶ return '' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 2 2
125 2 ▶ return $text_of{$value} if exists $text_of{$value};
Branch: 1 branch
BranchTrueFalseTotal
if exists $text_of{$value} 0 2 2
126 2 ▶ return $settings->{default} // "$value";
Condition100%2/2
$settings->{"default"} // "$value"
l!l
11
MC/DC100%1/1
$settings->{"default"} // "$value"
$settings->{"default"}
Truth table100%2/2
A
A= $settings->{"default"}
Aresult
11
00
127 2 };
128 }
129  
130 5 ▶sub truncate ( $columns, %options ) {
Subroutine truncate: called CC=2 SCAR=6.9
Pod documented
131 5 $columns = _non_negative_integer( truncate => columns => $columns );
132 5 my $settings = _options( truncate => { ellipsis => ELLIPSIS }, %options );
133 5 my $ellipsis = $settings->{ellipsis};
134 5 ▶ croak "Term::Fabulous::Widget::Table::Mutator: truncate needs more columns than the ellipsis takes" if string_columns($ellipsis) > $columns;
Branch: 1 branch
BranchTrueFalseTotal
if string_columns($ellipsis) > $columns 1 4 5
135 4 ▶ return sub ( $value, $row = undef ) {
Subroutine __ANON__: called CC=3 SCAR=11.0
136 4 ▶ return '' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 3 4
137 3 return fit_columns( "$value", $columns, mark => $ellipsis );
138 4 };
139 }
140  
141 2 ▶sub sprintf_format ($format) {
Subroutine sprintf_format: called CC=3 SCAR=11.6
Pod documented
142 2 ▶ croak "Term::Fabulous::Widget::Table::Mutator: sprintf_format needs a format string" if ref $format || !defined $format;
Branch: 1 branch
BranchTrueFalseTotal
if ref $format or not defined $format 0 2 2
Condition33%1/3
ref $format or not defined $format
l!l&&r!l&&!r
002
MC/DC0%0/2
ref $format or not defined $format
ref $format not defined $format
Truth table33%1/3
A or not(B)
A= ref $formatB= not defined $format
ABresult
1X1
011
000
143 2 ▶ return sub ( $value, $row = undef ) {
Subroutine __ANON__: called CC=3 SCAR=11.0
144 2 ▶ return is_blank($value) ? '' : sprintf( $format, $value );
Branch: 1 branch
BranchTrueFalseTotal
is_blank($value) ? : 1 1 2
145 2 };
146 }
147  
148 1 ▶sub chain (@mutators) {
Subroutine chain: called CC=3 SCAR=11.0
Pod documented
149 1 foreach my $mutator (@mutators) {
150 2 ▶ croak "Term::Fabulous::Widget::Table::Mutator: chain takes code references" unless ref $mutator eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $mutator eq "CODE" 0 2 2
151 }
152 1 ▶ return sub ( $value, $row = undef ) {
Subroutine __ANON__: called CC=3 SCAR=11.0
153 1 $value = $_->( $value, $row ) foreach @mutators;
154 1 return $value;
155 1 };
156 }
157  
158 1 1;
159  
160 __END__
« blib/lib/Term/Fabulous/Color.pm ↑ Summary blib/lib/Term/Fabulous/Widget/Sixel.pm »