blib/lib/Term/Fabulous/Unicode.pm

filter: Statementstmt 98.7% 80 / 81 Branchbran 89.1% 41 / 46 Conditioncond 94.7% 18 / 19 MC/DCmcdc 90.9% 10 / 11 Subroutinesub 95.0% 19 / 20 Podpod 100% 6 / 6 totaltotal 95.0% 174 / 183 CC 33 SCAR 19.2
CC
33
Cov
95%
fit_columns
16.0
terminal_is_utf8
6.0
string_columns
4.0
CRAP 6.8
« blib/lib/Term/Fabulous/Chart/Easing.pm ↑ Summary blib/lib/Term/Fabulous/Enum/BorderStyle.pm »
1 package Term::Fabulous::Unicode;
2  
3 4 ▶use v5.32;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 our $VERSION = '0.05';
9  
10 4 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 our @EXPORT_OK = qw(sanitize_text grapheme_clusters cluster_columns string_columns fit_columns terminal_is_utf8 ELLIPSIS);
12  
13 4 ▶use I18N::Langinfo qw(langinfo CODESET);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Term::Fabulous::Termbox qw(tb_cluster_width);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Unicode::GCString;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 4 ▶use constant CACHE_LIMIT => 4096;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use constant ELLIPSIS => "\x{2026}";
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # The same texts are segmented and measured on every frame (layout
21 # callbacks, text and canvas drawing), so each result is remembered per
22 # input string. A cache is emptied once it reaches CACHE_LIMIT entries.
23 4 my ( %clusters_by_text, %columns_by_cluster, %columns_by_text );
24  
25 30 ▶sub sanitize_text ($text) {
Subroutine sanitize_text: called CC=2 SCAR=6.9
Pod documented
26 30 ▶ die "Term::Fabulous::Unicode: sanitize_text needs well-formed UTF-8" unless utf8::valid($text);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::valid($text) 2 28 30
27 28 return $text =~ tr/\t\x00-\x08\x0A-\x1F\x7F-\x9F/ \x{FFFD}/r;
28 }
29  
30 44 ▶sub grapheme_clusters ($text) {
Subroutine grapheme_clusters: called CC=3 SCAR=11.0
Pod documented
31 44 my $clusters = $clusters_by_text{$text};
32 44 ▶ return @$clusters if defined $clusters;
Branch: 1 branch
BranchTrueFalseTotal
if defined $clusters 19 25 44
33  
34 25 ▶ %clusters_by_text = () if keys(%clusters_by_text) >= CACHE_LIMIT;
Branch: 1 branch
BranchTrueFalseTotal
if keys %clusters_by_text >= 4096 0 25 25
35 25 $clusters = $clusters_by_text{$text} = [ map { $_->as_string } @{ Unicode::GCString->new( sanitize_text($text) )->as_arrayref } ];
36 24 return @$clusters;
37 }
38  
39 160 ▶sub cluster_columns ($cluster) {
Subroutine cluster_columns: called CC=4 SCAR=13.9
Pod documented
40 160 my $columns = $columns_by_cluster{$cluster};
41 160 ▶ return $columns if defined $columns;
Branch: 1 branch
BranchTrueFalseTotal
if defined $columns 103 57 160
42  
43 57 ▶ die "Term::Fabulous::Unicode: cluster_columns needs a non-empty cluster" unless length $cluster;
Branch: 1 branch
BranchTrueFalseTotal
unless length $cluster 1 56 57
44 56 ▶ %columns_by_cluster = () if keys(%columns_by_cluster) >= CACHE_LIMIT;
Branch: 1 branch
BranchTrueFalseTotal
if keys %columns_by_cluster >= 4096 0 56 56
45 56 return $columns_by_cluster{$cluster} = _terminal_columns($cluster);
46 }
47  
48 46 ▶sub string_columns ($text) {
Subroutine string_columns: called CC=4 SCAR=13.9
Pod documented
49 46 my $columns = $columns_by_text{$text};
50 46 ▶ return $columns if defined $columns;
Branch: 1 branch
BranchTrueFalseTotal
if defined $columns 24 22 46
51  
52 22 ▶ %columns_by_text = () if keys(%columns_by_text) >= CACHE_LIMIT;
Branch: 1 branch
BranchTrueFalseTotal
if keys %columns_by_text >= 4096 0 22 22
53 22 $columns = 0;
54 22 $columns += cluster_columns($_) foreach grapheme_clusters($text);
55 22 return $columns_by_text{$text} = $columns;
56 }
57  
58 27 ▶sub fit_columns ( $text, $columns, %options ) {
Subroutine fit_columns: called CC=16 SCAR=27.7
Pod documented
59 27 ▶ my @unknown = grep { $_ ne 'mark' && $_ ne 'cut' } sort keys %options;
Condition100%3/3
$_ ne 'mark' && $_ ne 'cut'
!ll&&!rl&&r
1051
MC/DC100%2/2
$_ ne 'mark' && $_ ne 'cut'
$_ ne 'mark' $_ ne 'cut'
Truth table100%3/3
A && B
A= $_ ne 'mark'B= $_ ne 'cut'
ABresult
0X0
100
111
60 27 ▶ die "Term::Fabulous::Unicode: fit_columns does not take @unknown (known: cut mark)" if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 1 26 27
61 26 ▶ die "Term::Fabulous::Unicode: fit_columns needs a text, got " . _named($text) unless _is_text($text);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_text($text) 1 25 26
62 25 ▶ die "Term::Fabulous::Unicode: fit_columns needs a whole number of columns, got " . _named($columns) unless _is_text($columns) && $columns =~ /\A-?[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless _is_text($columns) and $columns =~ /\A-?[0-9]+\z/u 2 23 25
Condition100%3/3
_is_text($columns) and $columns =~ /\A-?[0-9]+\z/u
!ll&&!rl&&r
1123
MC/DC100%2/2
_is_text($columns) and $columns =~ /\A-?[0-9]+\z/u
_is_text($columns) $columns =~ /\A-?[0-9]+\z/u
Truth table100%3/3
A and B
A= _is_text($columns)B= $columns =~ /\A-?[0-9]+\z/u
ABresult
0X0
100
111
63 23 ▶ my $mark = $options{mark} // ELLIPSIS;
Condition100%2/2
$options{"mark"} // "\x{2026}"
l!l
1013
MC/DC100%1/1
$options{"mark"} // "\x{2026}"
$options{"mark"}
Truth table100%2/2
A
A= $options{"mark"}
Aresult
11
00
64 23 ▶ my $cut = $options{cut} // 'end';
Condition100%2/2
$options{"cut"} // "end"
l!l
518
MC/DC100%1/1
$options{"cut"} // "end"
$options{"cut"}
Truth table100%2/2
A
A= $options{"cut"}
Aresult
11
00
65 23 ▶ die "Term::Fabulous::Unicode: fit_columns mark must be a text, got " . _named($mark) unless _is_text($mark);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_text($mark) 1 22 23
66 22 ▶ die "Term::Fabulous::Unicode: fit_columns cut must be 'end' or 'start', got " . _named($cut) unless _is_text($cut) && ( $cut eq 'end' || $cut eq 'start' );
Branch: 1 branch
BranchTrueFalseTotal
unless _is_text($cut) and $cut eq "end" || $cut eq "start" 1 21 22
Condition83%5/6
_is_text($cut) and $cut eq "end" || $cut eq "start"
!ll&&!rl&&r
0121
$cut eq "end" or $cut eq "start"
l!l&&r!l&&!r
1741
MC/DC66%2/3
_is_text($cut) and $cut eq "end" || $cut eq "start"
_is_text($cut) $cut eq "end" $cut eq "start"
Truth table75%3/4
A and B or C
A= _is_text($cut)B= $cut eq "end"C= $cut eq "start"
ABCresult
0XX0
1000
11X1
1011
67  
68 21 ▶ return '' if $columns <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $columns <= 0 2 19 21
69 19 ▶ return $text if string_columns($text) <= $columns;
Branch: 1 branch
BranchTrueFalseTotal
if string_columns($text) <= $columns 2 17 19
70 17 my $room = $columns - string_columns($mark);
71 17 ▶ return join '', _clusters_within( $columns, grapheme_clusters($mark) ) if $room < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $room < 0 3 14 17
72 14 ▶ return join( '', _clusters_within( $room, grapheme_clusters($text) ) ) . $mark if $cut eq 'end';
Branch: 1 branch
BranchTrueFalseTotal
if $cut eq "end" 11 3 14
73 3 return $mark . join '', reverse _clusters_within( $room, reverse grapheme_clusters($text) );
74 }
75  
76 0 ▶sub terminal_is_utf8 () {
Subroutine terminal_is_utf8: not called CC=2 SCAR=17.9
Pod documented
77 0 ▶ return langinfo(CODESET) =~ /\Autf-?8\z/i ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
&langinfo(14) =~ /\Autf-?8\z/iu ? : 0 0 0
78 }
79  
80 # The leading clusters that fit into $room columns together.
81 17 ▶sub _clusters_within ( $room, @clusters ) {
Subroutine _clusters_within: called CC=3 SCAR=11.0
82 17 my @kept;
83 17 foreach my $cluster (@clusters) {
84 53 $room -= cluster_columns($cluster);
85 53 ▶ last if $room < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $room < 0 17 36 53
86 36 push @kept, $cluster;
87 }
88 17 return @kept;
89 }
90  
91 96 ▶sub _is_text ($value) {
Subroutine _is_text: called CC=2 SCAR=6.9
92 96 ▶ return defined $value && !ref $value;
Condition100%3/3
defined $value && !ref($value)
!ll&&!rl&&r
1293
MC/DC100%2/2
defined $value && !ref($value)
defined $value !ref($value)
Truth table100%3/3
A && not(B)
A= defined $valueB= !ref($value)
ABresult
0X0
100
111
93 }
94  
95 5 ▶sub _named ($value) {
Subroutine _named: called CC=4 SCAR=13.9
96 5 ▶ return 'undef' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 4 5
97 4 ▶ return ( ref($value) =~ /\A[AEIOU]/ ? 'an ' : 'a ' ) . ref($value) . ' reference' if ref $value;
Branch: 2 branches
BranchTrueFalseTotal
if ref $value 2 2 4
ref($value) =~ /\A[AEIOU]/u ? : 1 1 2
98 2 return "'$value'";
99 }
100  
101 # Mirrors termbox2's tb_present(): a cluster advances by tb_cluster_width()
102 # (its widest codepoint, forced to 1 by a text presentation selector and
103 # to 2 by an emoji presentation selector, a zero-width joiner or a pair of
104 # regional indicators), and any result below 1 advances by one column.
105 56 ▶sub _terminal_columns ($cluster) {
Subroutine _terminal_columns: called CC=2 SCAR=6.9
106 56 my $width = tb_cluster_width($cluster);
107 56 ▶ return $width < 1 ? 1 : $width;
Branch: 1 branch
BranchTrueFalseTotal
$width < 1 ? : 1 55 56
108 }
109  
110 4 1;
111  
112 __END__
« blib/lib/Term/Fabulous/Chart/Easing.pm ↑ Summary blib/lib/Term/Fabulous/Enum/BorderStyle.pm »