| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 25 | ▶ | %clusters_by_text = () if keys(%clusters_by_text) >= CACHE_LIMIT; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 57 | ▶ | die "Term::Fabulous::Unicode: cluster_columns needs a non-empty cluster" unless length $cluster; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 56 | ▶ | %columns_by_cluster = () if keys(%columns_by_cluster) >= CACHE_LIMIT; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | 22 | ▶ | %columns_by_text = () if keys(%columns_by_text) >= CACHE_LIMIT; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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'
MC/DC100%2/2
$_ ne 'mark' && $_ ne 'cut'
$_ ne 'mark'
$_ ne 'cut'
Truth table100%3/3
A && B
A= $_ ne 'mark'B= $_ ne 'cut'
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 27 | ▶ | die "Term::Fabulous::Unicode: fit_columns does not take @unknown (known: cut mark)" if @unknown; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 26 | ▶ | die "Term::Fabulous::Unicode: fit_columns needs a text, got " . _named($text) unless _is_text($text); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
_is_text($columns) and $columns =~ /\A-?[0-9]+\z/u
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
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 23 | ▶ | my $mark = $options{mark} // ELLIPSIS; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options{"mark"} // "\x{2026}"
MC/DC100%1/1
$options{"mark"} // "\x{2026}"
$options{"mark"}
Truth table100%2/2
A
A= $options{"mark"}
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 23 | ▶ | my $cut = $options{cut} // 'end'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options{"cut"} // "end"
MC/DC100%1/1
$options{"cut"} // "end"
$options{"cut"}
Truth table100%2/2
A
A= $options{"cut"}
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 23 | ▶ | die "Term::Fabulous::Unicode: fit_columns mark must be a text, got " . _named($mark) unless _is_text($mark); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition83%5/6
_is_text($cut) and $cut eq "end" || $cut eq "start"
$cut eq "end" or $cut eq "start"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 21 | ▶ | return '' if $columns <= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 19 | ▶ | return $text if string_columns($text) <= $columns; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 17 | my $room = $columns - string_columns($mark); | |||||||||||||||||||||||||||||||||||||||||
| 71 | 17 | ▶ | return join '', _clusters_within( $columns, grapheme_clusters($mark) ) if $room < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 14 | ▶ | return join( '', _clusters_within( $room, grapheme_clusters($text) ) ) . $mark if $cut eq 'end'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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)
MC/DC100%2/2
defined $value && !ref($value)
defined $value
!ref($value)
Truth table100%3/3
A && not(B)
A= defined $valueB= !ref($value)
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 4 | ▶ | return ( ref($value) =~ /\A[AEIOU]/ ? 'an ' : 'a ' ) . ref($value) . ' reference' if ref $value; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | __END__ | ||||||||||||||||||||||||||||||||||||||||||