blib/lib/Term/Fabulous/Chart/Format.pm

filter: Statementstmt 89.3% 84 / 94 Branchbran 89.6% 52 / 58 Conditioncond 75.0% 39 / 52 MC/DCmcdc 63.3% 19 / 30 Subroutinesub 96.0% 24 / 25 Podpod 100% 7 / 7 totaltotal 84.5% 225 / 266 CC 54 SCAR 25.3
CC
54
Cov
86%
format_values
56.0
format_value
19.0
number_formatter
9.0
CRAP 12.6
« blib/lib/Term/Fabulous/Chart/Scale/Time.pm ↑ Summary blib/lib/Term/Fabulous/Widget/Table/Frame.pm »
1 package Term::Fabulous::Chart::Format;
2  
3 2 ▶use v5.32;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '0.05';
9  
10 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 our @EXPORT_OK = qw(number_formatter time_formatter format_value format_values decimals_of check_number_format check_time_format);
12  
13 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use POSIX qw(floor strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 my @SI_PREFIXES = ( [ 1e12, 'T' ], [ 1e9, 'G' ], [ 1e6, 'M' ], [ 1e3, 'k' ] );
18 2 my %NUMBER_FORMATS = map { $_ => 1 } qw(auto si integer percent);
19  
20 # The decimals a number needs to be written to six significant digits,
21 # up to 10.
22 166 ▶sub decimals_of ($number) {
Subroutine decimals_of: called CC=4 SCAR=13.9
Pod documented
23 166 foreach my $decimals ( 0 .. 10 ) {
24 239 my $scaled = abs($number) * 10**$decimals;
25 239 ▶ return $decimals if abs( $scaled - int( $scaled + 0.5 ) ) < 1e-6 * ( $scaled || 1 );
Branch: 1 branch
BranchTrueFalseTotal
if abs $scaled - int($scaled + "0.5") < "1e-06" * ($scaled || 1) 165 74 239
Condition100%2/2
$scaled || 1
l!l
2381
MC/DC100%1/1
$scaled || 1
$scaled
Truth table100%2/2
A
A= $scaled
Aresult
11
00
26 }
27 1 return 10;
28 }
29  
30 663 ▶sub _fixed ( $value, $decimals ) {
Subroutine _fixed: called CC=1 SCAR=0.0
31 663 my $text = sprintf '%.*f', $decimals, $value;
32 663 $text =~ s/\A-(?=0(?:\.0*)?\z)//; # no "-0"
33 663 return $text;
34 }
35  
36 # 12000 as "12k": the largest prefix the value reaches.
37 52 ▶sub _si ( $value, $decimals_of_unit ) {
Subroutine _si: called CC=3 SCAR=11.0
38 52 my $magnitude = abs $value;
39 52 foreach my $prefix (@SI_PREFIXES) {
40 199 my ( $unit, $symbol ) = @$prefix;
41 199 ▶ next if $magnitude < $unit;
Branch: 1 branch
BranchTrueFalseTotal
if $magnitude < $unit 155 44 199
42 44 return _fixed( $value / $unit, $decimals_of_unit->($unit) ) . $symbol;
43 }
44 8 return _fixed( $value, $decimals_of_unit->(1) );
45 }
46  
47 8 ▶sub check_number_format ( $owner, $name, $format ) {
Subroutine check_number_format: called CC=7 SCAR=19.5
Pod documented
48 8 ▶ return $format if !defined $format || ref $format eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $format or ref $format eq "CODE" 1 7 8
Condition66%2/3
not defined $format or ref $format eq "CODE"
l!l&&r!l&&!r
107
MC/DC50%1/2
not defined $format or ref $format eq "CODE"
not defined $format ref $format eq "CODE"
Truth table66%2/3
not(A) or B
A= not defined $formatB= ref $format eq "CODE"
ABresult
1X1
011
000
49 croak "$owner: $name must be auto, si, integer, percent, a sprintf format with one % conversion or a code reference, got " . ( ref $format ? ref($format) . ' reference' : "'$format'" )
50 7 ▶ if ref $format || !( $NUMBER_FORMATS{$format} || $format =~ /%/ );
Branch: 2 branches
BranchTrueFalseTotal
if ref $format or not $NUMBER_FORMATS{$format} || $format =~ /%/u 2 5 7
ref $format ? : 1 1 2
Condition100%6/6
ref $format or not $NUMBER_FORMATS{$format} || $format =~ /%/u
l!l&&r!l&&!r
115
$NUMBER_FORMATS{$format} || $format =~ /%/u
l!l&&r!l&&!r
411
MC/DC100%3/3
ref $format or not $NUMBER_FORMATS{$format} || $format =~ /%/u
ref $format $NUMBER_FORMATS{$format} $format =~ /%/u
Truth table100%4/4
A or not(B || C)
A= ref $formatB= $NUMBER_FORMATS{$format}C= $format =~ /%/u
ABCresult
1XX1
0001
01X0
0010
51 5 return $format;
52 }
53  
54 # A function that writes the tick labels of a numeric axis whose ticks are
55 # $step apart and reach $largest (in magnitude): the format of the axis
56 # (auto, si, integer, percent, a sprintf format or a code reference
57 # called with the value).
58 112 ▶sub number_formatter ( $format, $step, $largest ) {
Subroutine number_formatter: called CC=9 SCAR=22.0
Pod documented
59 112 ▶ $format //= 'auto';
Condition100%2/2
$format //= "auto"
l!l
1993
MC/DC100%1/1
$format //= "auto"
$format
Truth table100%2/2
A
A= $format
Aresult
11
00
60 112 ▶ return $format if ref $format eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $format eq "CODE" 1 111 112
61 5 ▶ return sub ($value) { sprintf $format, $value }
Subroutine __ANON__: called CC=1 SCAR=0.0
62 111 ▶ if $format =~ /%/;
Branch: 1 branch
BranchTrueFalseTotal
if $format =~ /%/u 5 106 111
63  
64 106 ▶ my $step_decimals = decimals_of( $step || 1 );
Condition50%1/2
$step || 1
l!l
1060
MC/DC0%0/1
$step || 1
$step
Truth table50%1/2
A
A= $step
Aresult
11
00
65 3 ▶ return sub ($value) { _fixed( $value, 0 ) }
Subroutine __ANON__: called CC=1 SCAR=0.0
66 106 ▶ if $format eq 'integer';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "integer" 1 105 106
67 20 ▶ return sub ($value) { _fixed( $value * 100, decimals_of( ( $step || 1 ) * 100 ) ) . '%' }
Subroutine __ANON__: called CC=2 SCAR=7.6
Condition50%1/2
$step || 1
l!l
200
MC/DC0%0/1
$step || 1
$step
Truth table50%1/2
A
A= $step
Aresult
11
00
68 105 ▶ if $format eq 'percent';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "percent" 6 99 105
69  
70 99 ▶ my $decimals_of_unit = sub ($unit) { decimals_of( ( $step || 1 ) / $unit ) };
Subroutine __ANON__: called CC=2 SCAR=7.2
Condition50%1/2
$step || 1
l!l
310
MC/DC0%0/1
$step || 1
$step
Truth table50%1/2
A
A= $step
Aresult
11
00
71 31 ▶ return sub ($value) { _si( $value, $decimals_of_unit ) }
Subroutine __ANON__: called CC=1 SCAR=0.0
72 99 ▶ if $format eq 'si' || abs($largest) >= 1e4;
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "si" or abs $largest >= 10000 10 89 99
Condition100%3/3
$format eq "si" or abs $largest >= 10000
l!l&&r!l&&!r
2889
MC/DC100%2/2
$format eq "si" or abs $largest >= 10000
$format eq "si" abs $largest >= 10000
Truth table100%3/3
A or B
A= $format eq "si"B= abs $largest >= 10000
ABresult
1X1
011
000
73 89 ▶ return sub ($value) { _fixed( $value, $step_decimals ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
74 }
75  
76 # A value shown on its own (a bar's label, a legend entry): as many
77 # decimals as it has, at most two (three significant digits below 1), and
78 # large values with an SI prefix.
79 55 ▶sub format_value ( $value, $format = undef ) {
Subroutine format_value: called CC=19 SCAR=29.4
Pod documented
80 55 ▶ return '' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 54 55
81 54 ▶ return $format->($value) if ref $format eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $format eq "CODE" 1 53 54
82 53 ▶ return sprintf $format, $value if defined $format && $format =~ /%/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $format and $format =~ /%/u 1 52 53
Condition100%3/3
defined $format and $format =~ /%/u
!ll&&!rl&&r
13391
MC/DC100%2/2
defined $format and $format =~ /%/u
defined $format $format =~ /%/u
Truth table100%3/3
A and B
A= defined $formatB= $format =~ /%/u
ABresult
0X0
100
111
83 52 ▶ return _fixed( $value * 100, abs( $value * 100 ) < 10 ? 1 : 0 ) . '%' if defined $format && $format eq 'percent';
Branch: 2 branches
BranchTrueFalseTotal
if defined $format and $format eq "percent" 2 50 52
abs $value * 100 < 10 ? : 1 1 2
Condition100%3/3
defined $format and $format eq "percent"
!ll&&!rl&&r
13372
MC/DC100%2/2
defined $format and $format eq "percent"
defined $format $format eq "percent"
Truth table100%3/3
A and B
A= defined $formatB= $format eq "percent"
ABresult
0X0
100
111
84 50 ▶ return _fixed( $value, 0 ) if defined $format && $format eq 'integer';
Branch: 1 branch
BranchTrueFalseTotal
if defined $format and $format eq "integer" 1 49 50
Condition100%3/3
defined $format and $format eq "integer"
!ll&&!rl&&r
13361
MC/DC100%2/2
defined $format and $format eq "integer"
defined $format $format eq "integer"
Truth table100%3/3
A and B
A= defined $formatB= $format eq "integer"
ABresult
0X0
100
111
85 49 my $magnitude = abs $value;
86 49 ▶ if ( $magnitude >= 1e6 || ( defined $format && $format eq 'si' && $magnitude >= 1e3 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($magnitude >= 1000000 or defined $format and $format eq "si" and $magnitude >= 1000) 21 28 49
Condition88%8/9
$magnitude >= 1000000 or defined $format and $format eq "si" and $magnitude >= 1000
l!l&&r!l&&!r
61528
defined $format and $format eq "si" and $magnitude >= 1000
!ll&&!rl&&r
91915
defined $format and $format eq "si"
!ll&&!rl&&r
9034
MC/DC75%3/4
$magnitude >= 1000000 or defined $format and $format eq "si" and $magnitude >= 1000
$magnitude >= 1000000 defined $format $format eq "si" $magnitude >= 1000
Truth table80%4/5
A or B and C and D
A= $magnitude >= 1000000B= defined $formatC= $format eq "si"D= $magnitude >= 1000
ABCDresult
1XXX1
01111
00XX0
010X0
01100
87 21 ▶ my $text = _si( $value, sub ($unit) { $magnitude / $unit >= 100 ? 0 : 1 } );
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$magnitude / $unit >= 100 ? : 6 15 21
88 21 $text =~ s/\.0(?=[kMGT]\z)//;
89 21 return $text;
90 }
91 28 ▶ my $decimals = $magnitude >= 100 ? 1 : $magnitude >= 1 ? 2 : $magnitude == 0 ? 0 : 2 - floor( log($magnitude) / log(10) );
Branch: 3 branches
BranchTrueFalseTotal
$magnitude >= 100 ? : 8 20 28
$magnitude >= 1 ? : 16 4 20
$magnitude == 0 ? : 1 3 4
92 28 ▶ $decimals = 6 if $decimals > 6;
Branch: 1 branch
BranchTrueFalseTotal
if $decimals > 6 1 27 28
93 28 my $text = _fixed( $value, $decimals );
94 28 $text =~ s/(\.\d*?)0+\z/$1/;
95 28 $text =~ s/\.\z//;
96 28 return $text;
97 }
98  
99 # Values shown together (the labels of bars): as format_value writes
100 # them, but all with the decimals the one with most decimals has.
101 0 ▶sub format_values ( $values, $format = undef ) {
Subroutine format_values: not called CC=7 SCAR=40.3
Pod documented
102 0 my @texts = map { format_value( $_, $format ) } @$values;
103 0 ▶ return @texts if defined $format || grep { /[^0-9.\-]/ } @texts;
Branch: 1 branch
BranchTrueFalseTotal
if defined $format or grep {/[^0-9.\-]/u;} @texts 0 0 0
Condition0%0/3
defined $format or grep {/[^0-9.\-]/u;} @texts
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $format or grep {/[^0-9.\-]/u;} @texts
defined $format grep {/[^0-9.\-]/u;} @texts
Truth table0%0/3
A or B
A= defined $formatB= grep {/[^0-9.\-]/u;} @texts
ABresult
1X1
011
000
104 0 my $decimals = 0;
105 0 foreach my $text (@texts) {
106 0 ▶ $decimals = length $1 if $text =~ /\.([0-9]+)\z/ && length $1 > $decimals;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\.([0-9]+)\z/u and length $1 > $decimals 0 0 0
Condition0%0/3
$text =~ /\.([0-9]+)\z/u and length $1 > $decimals
!ll&&!rl&&r
000
MC/DC0%0/2
$text =~ /\.([0-9]+)\z/u and length $1 > $decimals
$text =~ /\.([0-9]+)\z/u length $1 > $decimals
Truth table0%0/3
A and B
A= $text =~ /\.([0-9]+)\z/uB= length $1 > $decimals
ABresult
0X0
100
111
107 }
108 0 return map { _fixed( $_, $decimals ) } @$values;
109 }
110  
111 3 ▶sub check_time_format ( $owner, $name, $format ) {
Subroutine check_time_format: called CC=6 SCAR=18.4
Pod documented
112 3 ▶ return $format if !defined $format || ref $format eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $format or ref $format eq "CODE" 0 3 3
Condition33%1/3
not defined $format or ref $format eq "CODE"
l!l&&r!l&&!r
003
MC/DC0%0/2
not defined $format or ref $format eq "CODE"
not defined $format ref $format eq "CODE"
Truth table33%1/3
not(A) or B
A= not defined $formatB= ref $format eq "CODE"
ABresult
1X1
011
000
113 3 ▶ croak "$owner: $name must be a strftime format or a code reference, got " . ( ref $format ? ref($format) . ' reference' : "'$format'" )
Branch: 2 branches
BranchTrueFalseTotal
if ref $format or not $format =~ /%/u 2 1 3
ref $format ? : 1 1 2
Condition100%3/3
ref $format or not $format =~ /%/u
l!l&&r!l&&!r
111
MC/DC100%2/2
ref $format or not $format =~ /%/u
ref $format not $format =~ /%/u
Truth table100%3/3
A or not(B)
A= ref $formatB= not $format =~ /%/u
ABresult
1X1
011
000
114 if ref $format || $format !~ /%/;
115 1 return $format;
116 }
117  
118 # A function that writes epoch seconds with a strftime format (or calls a
119 # code reference with the epoch); local time unless $utc.
120 11 ▶sub time_formatter ( $format, $utc ) {
Subroutine time_formatter: called CC=2 SCAR=7.0
Pod documented
121 11 ▶ return $format if ref $format eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $format eq "CODE" 1 10 11
122 10 ▶ return sub ($epoch) { strftime( $format, $utc ? gmtime $epoch : localtime $epoch ) };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$utc ? : 686 0 686
123 }
124  
125 2 1;
126  
127 __END__
« blib/lib/Term/Fabulous/Chart/Scale/Time.pm ↑ Summary blib/lib/Term/Fabulous/Widget/Table/Frame.pm »