line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::ANSI::Fold::Util; |
2
|
|
|
|
|
|
|
our $VERSION = "1.01"; |
3
|
|
|
|
|
|
|
|
4
|
3
|
|
|
3
|
|
110939
|
use v5.14; |
|
3
|
|
|
|
|
21
|
|
5
|
3
|
|
|
3
|
|
1002
|
use utf8; |
|
3
|
|
|
|
|
26
|
|
|
3
|
|
|
|
|
12
|
|
6
|
3
|
|
|
3
|
|
69
|
use warnings; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
59
|
|
7
|
3
|
|
|
3
|
|
1473
|
use Data::Dumper; |
|
3
|
|
|
|
|
17070
|
|
|
3
|
|
|
|
|
156
|
|
8
|
|
|
|
|
|
|
|
9
|
3
|
|
|
3
|
|
18
|
use Exporter qw(import); |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
176
|
|
10
|
|
|
|
|
|
|
our @EXPORT_OK; |
11
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( all => [ @EXPORT_OK ] ); |
12
|
|
|
|
|
|
|
|
13
|
3
|
|
|
3
|
|
14
|
use List::Util qw(max); |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
549
|
|
14
|
3
|
|
|
3
|
|
1234
|
use Text::ANSI::Fold qw(ansi_fold); |
|
3
|
|
|
|
|
146434
|
|
|
3
|
|
|
|
|
267
|
|
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.01 |
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 prefix, and |
45
|
|
|
|
|
|
|
unexportable functions without them. |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=over 7 |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=cut |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=item B(I) |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=item B(I) |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
Returns visual width of given text. |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=cut |
59
|
|
|
|
|
|
|
|
60
|
3
|
|
|
3
|
|
255
|
BEGIN { push @EXPORT_OK, qw(&ansi_width) } |
61
|
6
|
|
|
6
|
1
|
14422
|
sub ansi_width { goto &width } |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub width { |
64
|
6
|
|
|
6
|
1
|
21
|
(ansi_fold($_[0], -1))[2]; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=item B(I, I, I [, I]) |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=item B(I, I, I [, I]) |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
Returns substring just like Perl's B function, but string |
73
|
|
|
|
|
|
|
position is calculated by the visible width on the screen instead of |
74
|
|
|
|
|
|
|
number of characters. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
If an optional I parameter is given, replace the substring |
77
|
|
|
|
|
|
|
by the replacement and return the entire string. |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
It does not cut the text in the middle of multi-byte character, of |
80
|
|
|
|
|
|
|
course. Its behavior depends on the implementation of lower module. |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=cut |
83
|
|
|
|
|
|
|
|
84
|
3
|
|
|
3
|
|
405
|
BEGIN { push @EXPORT_OK, qw(&ansi_substr) } |
85
|
1
|
|
|
1
|
1
|
71
|
sub ansi_substr { goto &substr } |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub substr { |
88
|
12
|
|
|
12
|
1
|
4951
|
my($text, $offset, $length, $replacement) = @_; |
89
|
12
|
100
|
|
|
|
30
|
if ($offset < 0) { |
90
|
4
|
|
|
|
|
11
|
$offset = max(0, $offset + ansi_width($text)); |
91
|
|
|
|
|
|
|
} |
92
|
12
|
|
100
|
|
|
580
|
my @s = Text::ANSI::Fold |
93
|
|
|
|
|
|
|
->new(text => $text, width => [ $offset, $length // -1, -1 ]) |
94
|
|
|
|
|
|
|
->chops; |
95
|
12
|
100
|
|
|
|
19459
|
if (defined $replacement) { |
96
|
2
|
|
50
|
|
|
10
|
$s[0] . $replacement . ($s[2] // ''); |
97
|
|
|
|
|
|
|
} else { |
98
|
10
|
|
|
|
|
31
|
$s[1]; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=back |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=cut |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
1; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
__END__ |