blib/lib/Term/Fabulous/Unicode.pm

filter: Statementstmt 98.1% 52 / 53 Branchbran 75.0% 15 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 93.3% 14 / 15 Podpod 100% 5 / 5 totaltotal 92.4% 86 / 93 CC 12 SCAR 9.9
CC
12
Cov
92%
terminal_is_utf8
6.0
string_columns
4.0
cluster_columns
4.0
CRAP 2.7
« blib/lib/Term/Fabulous/Render/Target/Mask.pm ↑ Summary blib/lib/Term/Fabulous/Widget/ScrollBox.pm »
1 package Term::Fabulous::Unicode;
2  
3 3 ▶use v5.32;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 our $VERSION = '0.03';
9  
10 3 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 our @EXPORT_OK = qw(sanitize_text grapheme_clusters cluster_columns string_columns terminal_is_utf8);
12  
13 3 ▶use I18N::Langinfo qw(langinfo CODESET);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Term::Fabulous::Termbox qw(tb_cluster_width);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Unicode::GCString;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 ▶use constant CACHE_LIMIT => 4096;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # The same texts are segmented and measured on every frame (layout
20 # callbacks, text and canvas drawing), so each result is remembered per
21 # input string. A cache is emptied once it reaches CACHE_LIMIT entries.
22 3 my ( %clusters_by_text, %columns_by_cluster, %columns_by_text );
23  
24 15 ▶sub sanitize_text ($text) {
Subroutine sanitize_text: called CC=2 SCAR=6.9
Pod documented
25 15 ▶ die "Term::Fabulous::Unicode: sanitize_text needs well-formed UTF-8" unless utf8::valid($text);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::valid($text) 2 13 15
26 13 return $text =~ tr/\t\x00-\x08\x0A-\x1F\x7F-\x9F/ \x{FFFD}/r;
27 }
28  
29 14 ▶sub grapheme_clusters ($text) {
Subroutine grapheme_clusters: called CC=3 SCAR=11.0
Pod documented
30 14 my $clusters = $clusters_by_text{$text};
31 14 ▶ return @$clusters if defined $clusters;
Branch: 1 branch
BranchTrueFalseTotal
if defined $clusters 4 10 14
32  
33 10 ▶ %clusters_by_text = () if keys(%clusters_by_text) >= CACHE_LIMIT;
Branch: 1 branch
BranchTrueFalseTotal
if keys %clusters_by_text >= 4096 0 10 10
34 10 $clusters = $clusters_by_text{$text} = [ map { $_->as_string } @{ Unicode::GCString->new( sanitize_text($text) )->as_arrayref } ];
35 9 return @$clusters;
36 }
37  
38 50 ▶sub cluster_columns ($cluster) {
Subroutine cluster_columns: called CC=4 SCAR=13.9
Pod documented
39 50 my $columns = $columns_by_cluster{$cluster};
40 50 ▶ return $columns if defined $columns;
Branch: 1 branch
BranchTrueFalseTotal
if defined $columns 18 32 50
41  
42 32 ▶ die "Term::Fabulous::Unicode: cluster_columns needs a non-empty cluster" unless length $cluster;
Branch: 1 branch
BranchTrueFalseTotal
unless length $cluster 1 31 32
43 31 ▶ %columns_by_cluster = () if keys(%columns_by_cluster) >= CACHE_LIMIT;
Branch: 1 branch
BranchTrueFalseTotal
if keys %columns_by_cluster >= 4096 0 31 31
44 31 return $columns_by_cluster{$cluster} = _terminal_columns($cluster);
45 }
46  
47 12 ▶sub string_columns ($text) {
Subroutine string_columns: called CC=4 SCAR=13.9
Pod documented
48 12 my $columns = $columns_by_text{$text};
49 12 ▶ return $columns if defined $columns;
Branch: 1 branch
BranchTrueFalseTotal
if defined $columns 4 8 12
50  
51 8 ▶ %columns_by_text = () if keys(%columns_by_text) >= CACHE_LIMIT;
Branch: 1 branch
BranchTrueFalseTotal
if keys %columns_by_text >= 4096 0 8 8
52 8 $columns = 0;
53 8 $columns += cluster_columns($_) foreach grapheme_clusters($text);
54 8 return $columns_by_text{$text} = $columns;
55 }
56  
57 0 ▶sub terminal_is_utf8 () {
Subroutine terminal_is_utf8: not called CC=2 SCAR=17.9
Pod documented
58 0 ▶ return langinfo(CODESET) =~ /\Autf-?8\z/i ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
&langinfo(14) =~ /\Autf-?8\z/iu ? : 0 0 0
59 }
60  
61 # Mirrors termbox2's tb_present(): a cluster advances by tb_cluster_width()
62 # (its widest codepoint, forced to 1 by a text presentation selector and
63 # to 2 by an emoji presentation selector, a zero-width joiner or a pair of
64 # regional indicators), and any result below 1 advances by one column.
65 31 ▶sub _terminal_columns ($cluster) {
Subroutine _terminal_columns: called CC=2 SCAR=6.9
66 31 my $width = tb_cluster_width($cluster);
67 31 ▶ return $width < 1 ? 1 : $width;
Branch: 1 branch
BranchTrueFalseTotal
$width < 1 ? : 1 30 31
68 }
69  
70 3 1;
71  
72 __END__
« blib/lib/Term/Fabulous/Render/Target/Mask.pm ↑ Summary blib/lib/Term/Fabulous/Widget/ScrollBox.pm »