blib/lib/Text/ANSI/Fold/Util.pm

filter: Statementstmt 97.4% 38 / 39 Branchbran 87.5% 7 / 8 Conditioncond 83.3% 5 / 6 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 13 / 13 Podpod 100% 4 / 4 totaltotal 94.5% 69 / 73 CC 8 SCAR 10.4
CC
8
Cov
94%
substr
6.0
width
3.0
BEGIN
1.0
CRAP 2.8
↑ Summary
1 package Text::ANSI::Fold::Util;
2 3 our $VERSION = "1.06";
3  
4 3 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 our @EXPORT_OK;
11 3 our %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
12  
13 3 ▶use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Text::ANSI::Fold qw(ansi_fold);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =encoding utf-8
17  
18 =head1 NAME
19  
20 Text::ANSI::Fold::Util - Text::ANSI::Fold utilities (width, substr)
21  
22 =head1 SYNOPSIS
23  
24 use Text::ANSI::Fold::Util qw(:all);
25 use Text::ANSI::Fold::Util qw(ansi_width ansi_substr);
26 ansi_width($text);
27 ansi_substr($text, $offset, $width [, $replacement]);
28  
29 use Text::ANSI::Fold::Util;
30 Text::ANSI::Fold::Util::width($text);
31 Text::ANSI::Fold::Util::substr($text, ...);
32  
33 =head1 VERSION
34  
35 Version 1.06
36  
37 =head1 DESCRIPTION
38  
39 This is a collection of utilities using Text::ANSI::Fold module. All
40 functions are aware of ANSI terminal sequence.
41  
42 =head1 FUNCTION
43  
44 There are exportable functions start with B<ansi_> prefix, and
45 unexportable functions without them.
46  
47 Unless otherwise noted, these functions are executed in the same
48 context as C<ansi_fold> exported by C<Text::ANSI::Fold> module. That
49 is, the parameters set by C<< Text::ANSI::Fold->configure >> are
50 effective.
51  
52 =over 7
53  
54 =cut
55  
56  
57 =item B<width>(I<text>)
58  
59 =item B<ansi_width>(I<text>)
60  
61 Returns visual width of given text.
62  
63 =cut
64  
65 3 ▶BEGIN { push @EXPORT_OK, qw(&ansi_width) }
Subroutine BEGIN: called CC=1 SCAR=0.0
66 6 ▶sub ansi_width { goto &width }
Subroutine ansi_width: called CC=1 SCAR=0.0
Pod documented
67  
68 6 ▶sub width {
Subroutine width: called CC=3 SCAR=11.1
Pod documented
69 6 ▶ my $text = $_[0] // '';
Condition50%1/2
$_[0] // ""
l!l
60
MC/DC0%0/1
$_[0] // ""
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
70 # an empty or printable ASCII string is as wide as its length
71 6 ▶ $text !~ /[^\x20-\x7e]/ ? length $text : (ansi_fold($text, -1))[2];
Branch: 1 branch
BranchTrueFalseTotal
!($text =~ /[^\x20-\x7e]/u) ? : 3 3 6
72 }
73  
74  
75 =item B<substr>(I<text>, I<offset>, I<width> [, I<replacement>])
76  
77 =item B<ansi_substr>(I<text>, I<offset>, I<width> [, I<replacement>])
78  
79 Returns substring just like Perl's B<substr> function, but string
80 position is calculated by the visible width on the screen instead of
81 number of characters. If there is no corresponding substring, undef
82 is returned always.
83  
84 If the C<padding> option is specified, then if there is a
85 corresponding substring, it is padded to the specified width and
86 returned.
87  
88 It does not cut the text in the middle of multi-byte character. If
89 you want to split the text in the middle of a wide character, specify
90 the C<splitwide> option.
91  
92 Text::ANSI::Fold->configure(splitwide => 1);
93  
94 If an optional I<replacement> parameter is given, replace the
95 substring by the replacement and return the entire string. If 0 is
96 specified for the width, there may be no error even if the
97 corresponding substring does not exist.
98  
99 =cut
100  
101 3 ▶BEGIN { push @EXPORT_OK, qw(&ansi_substr) }
Subroutine BEGIN: called CC=1 SCAR=0.0
102 2 ▶sub ansi_substr { goto &substr }
Subroutine ansi_substr: called CC=1 SCAR=0.0
Pod documented
103  
104 18 ▶sub substr {
Subroutine substr: called CC=6 SCAR=18.0
Pod documented
105 18 my($text, $offset, $length, $replacement) = @_;
106 18 ▶ if ($offset < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < 0) 4 14 18
107 4 $offset = max(0, $offset + ansi_width($text));
108 }
109 18 ▶ my @s = (state $fold = Text::ANSI::Fold->configure)
Condition100%2/2
$length // -1
l!l
153
MC/DC100%1/1
$length // -1
$length
Truth table100%2/2
A
A= $length
Aresult
11
00
110 ->text($text)
111 ->chops(width => [ $offset, $length // -1, -1 ]);
112 18 ▶ if (defined $replacement) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $replacement) { } 4 14 18
113 4 ▶ if (defined $s[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s[1]) { } 4 0 4
114 4 ▶ $s[0] . $replacement . ($s[2] // '');
Condition100%2/2
$s[2] // ""
l!l
22
MC/DC100%1/1
$s[2] // ""
$s[2]
Truth table100%2/2
A
A= $s[2]
Aresult
11
00
115 } else {
116 0 undef;
117 }
118 } else {
119 14 $s[1];
120 }
121 }
122  
123  
124 =back
125  
126 =cut
127  
128 3 1;
129  
130 __END__
↑ Summary