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

filter: Statementstmt 98.4% 63 / 64 Branchbran 93.7% 45 / 48 Conditioncond 76.4% 39 / 51 MC/DCmcdc 62.0% 18 / 29 Subroutinesub 100% 16 / 16 Podpod 100% 6 / 6 totaltotal 87.3% 187 / 214 CC 44 SCAR 22.0
CC
44
Cov
90%
date_interval
18.0
natural_compare
8.6
_names_a_real_day
6.8
CRAP 9.0
« blib/lib/Term/Fabulous/Scrolling.pm ↑ Summary tools/lib/Term/Fabulous/Screenshot/Render/SVG.pm »
1 package Term::Fabulous::Widget::Table::Value;
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(is_blank number_of date_epoch date_interval compare_values natural_compare);
12  
13 1 ▶use Feature::Compat::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Scalar::Util qw(blessed looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Time::Local 1.30 qw(timelocal_posix timegm_posix);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # The column types: how a raw value is turned into something that orders.
18 1 my %IS_TYPE = map { $_ => 1 } qw(string number date);
19  
20 52 ▶sub is_blank ($value) {
Subroutine is_blank: called CC=4 SCAR=13.9
Pod documented
21 52 ▶ return !defined $value || ( !ref $value && $value eq '' ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
!defined($value) || !ref($value) && $value eq '' ? : 13 39 52
Condition100%6/6
not defined $value or not ref $value and $value eq ""
l!l&&r!l&&!r
11239
not ref $value and $value eq ""
!ll&&!rl&&r
2372
MC/DC100%3/3
not defined $value or not ref $value and $value eq ""
not defined $value not ref $value $value eq ""
Truth table100%4/4
not(A) or not(B) and C
A= not defined $valueB= not ref $valueC= $value eq ""
ABCresult
1XX1
0111
00X0
0100
22 }
23  
24 # A finite number, or undef.
25 31 ▶sub number_of ($value) {
Subroutine number_of: called CC=6 SCAR=17.9
Pod documented
26 31 ▶ return undef if is_blank($value) || ref $value || !looks_like_number($value);
Branch: 1 branch
BranchTrueFalseTotal
if is_blank($value) or ref $value or not &looks_like_number($value) 12 19 31
Condition100%6/6
is_blank($value) or ref $value or not &looks_like_number($value)
l!l&&r!l&&!r
6619
is_blank($value) or ref $value
l!l&&r!l&&!r
5125
MC/DC100%3/3
is_blank($value) or ref $value or not &looks_like_number($value)
is_blank($value) ref $value not &looks_like_number($value)
Truth table100%4/4
A or B or not(C)
A= is_blank($value)B= ref $valueC= not &looks_like_number($value)
ABCresult
1XX1
01X1
0011
0000
27 19 my $number = $value + 0;
28 19 ▶ return $number == $number && $number - $number == 0 ? $number : undef;
Branch: 1 branch
BranchTrueFalseTotal
$number == $number && $number - $number == 0 ? : 18 1 19
Condition66%2/3
$number == $number and $number - $number == 0
!ll&&!rl&&r
0118
MC/DC50%1/2
$number == $number and $number - $number == 0
$number == $number $number - $number == 0
Truth table66%2/3
A and B
A= $number == $numberB= $number - $number == 0
ABresult
0X0
100
111
29 }
30  
31 # The span of time a date string names: [ first second, first second
32 # after it ). Its granularity is the last part given: "2024" is a year,
33 # "2024-05" a month, "2024-05-03" a day, "2024-05-03 14:30" a minute.
34 # Local time, or UTC with a trailing Z. Undef for anything else.
35 17 ▶sub date_interval ($text) { ## no critic (Subroutines::RequireFinalReturn) PPI does not parse try/catch
Subroutine date_interval: called CC=18 SCAR=28.9
Pod documented
36 17 ▶ return undef unless defined $text && !ref $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and not ref $text 1 16 17
Condition66%2/3
defined $text and not ref $text
!ll&&!rl&&r
1016
MC/DC50%1/2
defined $text and not ref $text
defined $text not ref $text
Truth table66%2/3
A and not(B)
A= defined $textB= not ref $text
ABresult
0X0
100
111
37 16 ▶ my ( $year, $month, $day, $hour, $minute, $second, $utc ) = $text =~ m{
Branch: 1 branch
BranchTrueFalseTotal
() = $text =~ / \A \s* ([0-9]{4}) (?: - ([0-9]{1,2}) (?: - ([0-9]{1,2}) (?: [ T] ([0-9]{1,2}) : ([0-9]{2}) (?: : ([0-9]{2}) )? )? )? )? \s* (Z)? \s* \z /ux or return undef 14 2 16
38 \A \s* ([0-9]{4})
39 (?: - ([0-9]{1,2})
40 (?: - ([0-9]{1,2})
41 (?: [ T] ([0-9]{1,2}) : ([0-9]{2}) (?: : ([0-9]{2}) )? )?
42 )?
43 )?
44 \s* (Z)? \s* \z
45 }x or return undef;
46 14 ▶ return undef if defined $utc && !defined $hour;
Branch: 1 branch
BranchTrueFalseTotal
if defined $utc and not defined $hour 1 13 14
Condition100%3/3
defined $utc and not defined $hour
!ll&&!rl&&r
1211
MC/DC100%2/2
defined $utc and not defined $hour
defined $utc not defined $hour
Truth table100%3/3
A and not(B)
A= defined $utcB= not defined $hour
ABresult
0X0
100
111
47  
48 13 ▶ my @start = ( $second // 0, $minute // 0, $hour // 0, $day // 1, ( $month // 1 ) - 1, $year - 1900 );
Condition100%10/10
$second // 0
l!l
112
$minute // 0
l!l
310
$hour // 0
l!l
310
$day // 1
l!l
94
$month // 1
l!l
121
MC/DC100%1/1
$second // 0
$second
MC/DC100%1/1
$minute // 0
$minute
MC/DC100%1/1
$hour // 0
$hour
MC/DC100%1/1
$day // 1
$day
MC/DC100%1/1
$month // 1
$month
Truth table100%2/2
A
A= $second
Aresult
11
00
Truth table100%2/2
A
A= $minute
Aresult
11
00
Truth table100%2/2
A
A= $hour
Aresult
11
00
Truth table100%2/2
A
A= $day
Aresult
11
00
Truth table100%2/2
A
A= $month
Aresult
11
00
49 13 ▶ my $to_epoch = defined $utc ? \&timegm_posix : \&timelocal_posix;
Branch: 1 branch
BranchTrueFalseTotal
defined $utc ? : 1 12 13
50 13 try {
51 13 my $first = $to_epoch->(@start);
52 11 ▶ return undef unless _names_a_real_day( $first, \@start, defined $utc );
Branch: 1 branch
BranchTrueFalseTotal
unless _names_a_real_day($first, \@start, defined $utc) 0 11 11
53 11 ▶ my $after
Branch: 5 branches
BranchTrueFalseTotal
defined $second ? : 1 10 11
defined $minute ? : 2 8 10
defined $day ? : 5 3 8
defined $month ? : 2 1 3
$start[4] == 11 ? : 1 1 2
54 = defined $second ? $first + 1
55 : defined $minute ? $first + 60
56 : defined $day ? $to_epoch->( 0, 0, 0, _next_day( @start[ 3, 4, 5 ] ) )
57 : defined $month ? $to_epoch->( 0, 0, 0, 1, ( $start[4] + 1 ) % 12, $start[5] + ( $start[4] == 11 ? 1 : 0 ) )
58 : $to_epoch->( 0, 0, 0, 1, 0, $start[5] + 1 );
59 11 return [ $first, $after ];
60 }
61 catch ($error) {
62 2 return undef;
63 }
64 }
65  
66 # Time::Local accepts 31 April as 1 May; a date that does not exist is no
67 # date.
68 11 ▶sub _names_a_real_day ( $epoch, $parts, $utc ) {
Subroutine _names_a_real_day: called CC=5 SCAR=19.2
69 11 ▶ my @back = $utc ? gmtime $epoch : localtime $epoch;
Branch: 1 branch
BranchTrueFalseTotal
$utc ? : 1 10 11
70 11 ▶ return $back[3] == $parts->[3] && $back[4] == $parts->[4] && $back[5] == $parts->[5] ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$back[3] == $parts->[3] && $back[4] == $parts->[4] && $back[5] == $parts->[5] ? : 11 0 11
Condition33%2/6
$back[3] == $parts->[3] and $back[4] == $parts->[4] and $back[5] == $parts->[5]
!ll&&!rl&&r
0011
$back[3] == $parts->[3] and $back[4] == $parts->[4]
!ll&&!rl&&r
0011
MC/DC0%0/3
$back[3] == $parts->[3] and $back[4] == $parts->[4] and $back[5] == $parts->[5]
$back[3] == $parts->[3] $back[4] == $parts->[4] $back[5] == $parts->[5]
Truth table25%1/4
A and B and C
A= $back[3] == $parts->[3]B= $back[4] == $parts->[4]C= $back[5] == $parts->[5]
ABCresult
0XX0
10X0
1100
1111
71 }
72  
73 # ( day, month, year ) of the day after a calendar day, in Time::Local's
74 # numbering; counted in UTC at noon, so no clock change gets in the way.
75 5 ▶sub _next_day ( $day, $month, $year ) {
Subroutine _next_day: called CC=1 SCAR=0.0
76 5 my @next = gmtime( timegm_posix( 0, 0, 12, $day, $month, $year ) + 86400 );
77 5 return @next[ 3, 4, 5 ];
78 }
79  
80 # The epoch seconds of a date value: a number (already epoch seconds), an
81 # object with an epoch method (DateTime, Time::Piece, Time::Moment), or a
82 # date string as date_interval reads it (its first second). Undef for
83 # anything else.
84 10 ▶sub date_epoch ($value) {
Subroutine date_epoch: called CC=6 SCAR=17.9
Pod documented
85 10 ▶ return undef if is_blank($value);
Branch: 1 branch
BranchTrueFalseTotal
if is_blank($value) 1 9 10
86 9 ▶ return $value->epoch + 0 if blessed $value && $value->can('epoch');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($value) and $value->can("epoch") 1 8 9
Condition66%2/3
builtin::blessed($value) and $value->can("epoch")
!ll&&!rl&&r
801
MC/DC50%1/2
builtin::blessed($value) and $value->can("epoch")
builtin::blessed($value) $value->can("epoch")
Truth table66%2/3
A and B
A= builtin::blessed($value)B= $value->can("epoch")
ABresult
0X0
100
111
87 8 my $number = number_of($value);
88 8 ▶ return $number if defined $number;
Branch: 1 branch
BranchTrueFalseTotal
if defined $number 4 4 8
89 4 ▶ my $interval = date_interval($value) // return undef;
Condition100%2/2
date_interval($value) // (return undef)
l!l
31
MC/DC100%1/1
date_interval($value) // (return undef)
date_interval($value)
Truth table100%2/2
A
A= date_interval($value)
Aresult
11
00
90 3 return $interval->[0];
91 }
92  
93 # Orders text the way people count: digit runs compare as numbers, so
94 # "file9" comes before "file10". Case-insensitive.
95 2 ▶sub natural_compare ( $left, $right ) {
Subroutine natural_compare: called CC=7 SCAR=21.5
Pod documented
96 2 my @left = split /([0-9]+)/, fc "$left";
97 2 my @right = split /([0-9]+)/, fc "$right";
98 2 ▶ while ( @left && @right ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@left and @right) 4 0 4
Condition33%1/3
@left and @right
!ll&&!rl&&r
004
MC/DC0%0/2
@left and @right
@left @right
Truth table33%1/3
A and B
A= @leftB= @right
ABresult
0X0
100
111
99 4 my ( $mine, $theirs ) = ( shift @left, shift @right );
100 4 ▶ my $order
Branch: 1 branch
BranchTrueFalseTotal
$mine =~ /\A[0-9]+\z/u && $theirs =~ /\A[0-9]+\z/u ? : 2 2 4
Condition50%3/6
$mine =~ /\A[0-9]+\z/u and $theirs =~ /\A[0-9]+\z/u
!ll&&!rl&&r
202
$mine <=> $theirs or length $mine <=> length $theirs
l!l&&r!l&&!r
200
MC/DC50%1/2
$mine =~ /\A[0-9]+\z/u and $theirs =~ /\A[0-9]+\z/u
$mine =~ /\A[0-9]+\z/u $theirs =~ /\A[0-9]+\z/u
MC/DC0%0/2
$mine <=> $theirs or length $mine <=> length $theirs
$mine <=> $theirs length $mine <=> length $theirs
Truth table66%2/3
A and B
A= $mine =~ /\A[0-9]+\z/uB= $theirs =~ /\A[0-9]+\z/u
ABresult
0X0
100
111
Truth table33%1/3
A or B
A= $mine <=> $theirsB= length $mine <=> length $theirs
ABresult
1X1
011
000
101 = ( $mine =~ /\A[0-9]+\z/ && $theirs =~ /\A[0-9]+\z/ )
102 ? ( $mine <=> $theirs || length $mine <=> length $theirs )
103 : $mine cmp $theirs;
104 4 ▶ return $order if $order;
Branch: 1 branch
BranchTrueFalseTotal
if $order 2 2 4
105 }
106 0 return @left <=> @right;
107 }
108  
109 # Orders two raw values of a column type; blank values and values that
110 # are not of the type come last, in either direction of the sort (the
111 # caller reverses only the result for known values).
112 4 ▶sub compare_values ( $type, $left, $right ) {
Subroutine compare_values: called CC=4 SCAR=13.9
Pod documented
113 4 ▶ die "Term::Fabulous::Widget::Table::Value: unknown column type '$type'" unless $IS_TYPE{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless $IS_TYPE{$type} 1 3 4
114 3 ▶ return fc("$left") cmp fc("$right") if $type eq 'string';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "string" 1 2 3
115 2 ▶ my $of = $type eq 'number' ? \&number_of : \&date_epoch;
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'number' ? : 1 1 2
116 2 return $of->($left) <=> $of->($right);
117 }
118  
119 1 1;
120  
121 __END__
« blib/lib/Term/Fabulous/Scrolling.pm ↑ Summary tools/lib/Term/Fabulous/Screenshot/Render/SVG.pm »