Text/Color.pm

filter: Statementstmt 91.7% 67 / 73 Branchbran 93.7% 15 / 16 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 92.3% 12 / 13 Podpod 0.0% 0 / 9 totaltotal 80.9% 98 / 121 CC 12 SCAR 16.3
CC
12
Cov
89%
wrap
10.1
end_format
3.0
output_code
2.0
CRAP 5.1
« ParseLink.pm ↑ Summary Text/Overstrike.pm »
1 # Convert POD data to formatted color ASCII text
2 #
3 # This is just a basic proof of concept. It should later be modified to make
4 # better use of color, take options changing what colors are used for what
5 # text, and the like.
6 #
7 # SPDX-License-Identifier: GPL-1.0-or-later OR Artistic-1.0-Perl
8  
9 ##############################################################################
10 # Modules and declarations
11 ##############################################################################
12  
13 package Pod::Text::Color v6.1.1;
14  
15 2 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use parent qw(Pod::Text);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 ▶use Term::ANSIColor qw(color colored colorstrip);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 ##############################################################################
22 # Overrides
23 ##############################################################################
24  
25 # Make level one headings bold.
26 16 ▶sub cmd_head1 {
Subroutine cmd_head1: called CC=1 SCAR=0.0
Pod undocumented
27 16 my ($self, $attrs, $text) = @_;
28 16 $text =~ s{ \s+ \z }{}xms;
29 16 local $Term::ANSIColor::EACHLINE = "\n";
30 16 $self->SUPER::cmd_head1($attrs, colored($text, 'bold'));
31 16 return;
32 }
33  
34 # Make level two headings bold.
35 2 ▶sub cmd_head2 {
Subroutine cmd_head2: called CC=1 SCAR=0.0
Pod undocumented
36 2 my ($self, $attrs, $text) = @_;
37 2 $text =~ s{ \s+ \z }{}xms;
38 2 $self->SUPER::cmd_head2($attrs, colored($text, 'bold'));
39 2 return;
40 }
41  
42 # Fix the various formatting codes.
43 24 ▶sub cmd_b { my (undef, undef, $text) = @_; return colored($text, 'bold') }
Subroutine cmd_b: called CC=1 SCAR=0.0
Pod undocumented
44 3 ▶sub cmd_f { my (undef, undef, $text) = @_; return colored($text, 'cyan') }
Subroutine cmd_f: called CC=1 SCAR=0.0
Pod undocumented
45 18 ▶sub cmd_i { my (undef, undef, $text) = @_; return colored($text, 'yellow') }
Subroutine cmd_i: called CC=1 SCAR=0.0
Pod undocumented
46  
47 # Analyze a single line and return any formatting codes in effect at the end
48 # of that line.
49 73 ▶sub end_format {
Subroutine end_format: called CC=3 SCAR=11.0
Pod undocumented
50 73 my ($self, $line) = @_;
51 73 my $reset = color('reset');
52 73 my $current;
53 73 ▶ while ($line =~ m{ ( \e\[ [\d;]+ m ) }xmsg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ / ( \e\[ [\d;]+ m ) /gmsux) 56 73 129
54 56 my $code = $1;
55 56 ▶ if ($code eq $reset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code eq $reset) { } 27 29 56
56 27 undef $current;
57 } else {
58 29 $current .= $code;
59 }
60 }
61 73 return $current;
62 }
63  
64 # Output any included code in green.
65 0 ▶sub output_code {
Subroutine output_code: not called CC=1 SCAR=6.9
Pod undocumented
66 0 my ($self, $code) = @_;
67 0 local $Term::ANSIColor::EACHLINE = "\n";
68 0 $code = colored($code, 'green');
69 0 $self->output($code);
70 0 return;
71 }
72  
73 # Strip all of the formatting from a provided string, returning the stripped
74 # version.
75 33 ▶sub strip_format {
Subroutine strip_format: called CC=1 SCAR=0.0
Pod undocumented
76 33 my ($self, $text) = @_;
77 33 return colorstrip($text);
78 }
79  
80 # We unfortunately have to override the wrapping code here, since the normal
81 # wrapping code gets really confused by all the escape sequences.
82 114 ▶sub wrap {
Subroutine wrap: called CC=10 SCAR=23.1
Pod undocumented
83 114 my ($self, $text) = @_;
84 114 my $output = q{};
85 114 my $spaces = q{ } x $self->{MARGIN};
86 114 my $width = $self->{opt_width} - $self->{MARGIN};
87  
88 # Pathological margins make wrapping impossible. In this case, complain to
89 # the user and reset the margin to 0.
90 114 ▶ if ($width <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($width <= 0) 1 113 114
91 1 my $error = 'Margin is wider than the output width';
92 1 $self->whine($self->line_count(), $error);
93 1 $spaces = q{};
94 1 $width = $self->{opt_width};
95 }
96  
97 # Matches a single escape sequence.
98 114 my $code = qr{ (?: \e\[ [\d;]+ m ) }xms;
99  
100 # Matches any number of escape sequences preceding a single character
101 # other than a newline. Prevent backtracking to optimize the final
102 # regular expression matching since $code is complex.
103 114 my $char = qr{ (?> $code* [^\n] ) }xms;
104  
105 # Matches some sequence of $char up to $width characters, ending in codes
106 # followed by whitespace or the end of the string. This detects a valid
107 # break point. The extracted text is placed in $1.
108 114 my $shortchar = qr{
109 \A
110 ( ${char}{0,$width} (?> $code* ) )
111 (?: [ \t\n]+ | \z )
112 }xms;
113  
114 # Matches exactly $width $chars, used when we have to hard wrap in the
115 # middle of an unbroken string. The extracted text is placed in $1.
116 114 my $longchar = qr{ \A ( ${char}{$width} ) }xms;
117  
118 # Extract one line at a time from $text and wrap it.
119 114 ▶ while (length($text) > $width) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $text > $width) 44 114 158
120 44 ▶ if ($text =~ s{$shortchar}{}xms || $text =~ s{$longchar}{}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/$shortchar//msux or $text =~ s/$longchar//msux) { } 44 0 44
Condition33%1/3
$text =~ s/$shortchar//msux or $text =~ s/$longchar//msux
l!l&&r!l&&!r
4400
MC/DC0%0/2
$text =~ s/$shortchar//msux or $text =~ s/$longchar//msux
$text =~ s/$shortchar//msux $text =~ s/$longchar//msux
Truth table33%1/3
A or B
A= $text =~ s/$shortchar//msuxB= $text =~ s/$longchar//msux
ABresult
1X1
011
000
121 44 $output .= $spaces . $1 . "\n";
122 } else {
123 0 last;
124 }
125 }
126 114 $output .= $spaces . $text;
127  
128 # less -R always resets terminal attributes at the end of each line, so we
129 # need to clear attributes at the end of lines and then set them again at
130 # the start of the next line. This requires a second pass through the
131 # wrapped string, accumulating any attributes we see, remembering them,
132 # and then inserting the appropriate sequences at the newline.
133 114 ▶ if ($output =~ m{\n}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output =~ /\n/msux) 29 85 114
134 29 my @lines = split(m{\n}xms, $output);
135 29 my $start_format;
136 29 for my $line (@lines) {
137 73 ▶ if ($start_format && $line =~ m{\S}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start_format and $line =~ /\S/msux) 2 71 73
Condition66%2/3
$start_format and $line =~ /\S/msux
!ll&&!rl&&r
7102
MC/DC50%1/2
$start_format and $line =~ /\S/msux
$start_format $line =~ /\S/msux
Truth table66%2/3
A and B
A= $start_formatB= $line =~ /\S/msux
ABresult
0X0
100
111
138 2 $line =~ s{ \A (\s*) (\S) }{$1$start_format$2}xms;
139 }
140 73 $start_format = $self->end_format($line);
141 73 ▶ if ($start_format) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start_format) 2 71 73
142 2 $line .= color('reset');
143 }
144 }
145 29 $output = join("\n", @lines);
146 }
147  
148 # Fix up trailing whitespace and return the results.
149 114 $output =~ s{ \s+ \z }{\n\n}xms;
150 114 return $output;
151 }
152  
153 ##############################################################################
154 # Module return value and documentation
155 ##############################################################################
156  
157 2 1;
158 __END__
« ParseLink.pm ↑ Summary Text/Overstrike.pm »