Text/Termcap.pm

filter: Statementstmt 96.1% 99 / 103 Branchbran 92.3% 24 / 26 Conditioncond 64.0% 16 / 25 MC/DCmcdc 38.4% 5 / 13 Subroutinesub 93.3% 14 / 15 Podpod 10.0% 1 / 10 totaltotal 82.8% 159 / 192 CC 25 SCAR 20.4
CC
25
Cov
90%
wrap
13.3
new
10.3
end_format
3.0
CRAP 7.7
« Text.pm ↑ Summary ParseLink.pm »
1 # Convert POD data to ASCII text with format escapes.
2 #
3 # This is a simple subclass of Pod::Text that overrides a few key methods to
4 # output the right termcap escape sequences for formatted text on the current
5 # terminal type.
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::Termcap 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 POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Term::Cap;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 ##############################################################################
23 # Overrides
24 ##############################################################################
25  
26 # In the initialization method, grab our terminal characteristics as well as
27 # do all the stuff we normally do.
28 11 ▶sub new {
Subroutine new: called CC=10 SCAR=23.3
Pod documented
29 11 my ($self, %args) = @_;
30 11 my ($ospeed, $term, $termios);
31  
32 # Fall back on a hard-coded terminal speed if POSIX::Termios isn't
33 # available (such as on VMS).
34 11 eval { $termios = POSIX::Termios->new };
35 11 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 11 11
36 0 $ospeed = 9600;
37 } else {
38 11 $termios->getattr;
39 11 ▶ $ospeed = $termios->getospeed || 9600;
Condition50%1/2
$termios->getospeed || 9600
l!l
011
MC/DC0%0/1
$termios->getospeed || 9600
$termios->getospeed
Truth table50%1/2
A
A= $termios->getospeed
Aresult
11
00
40 }
41  
42 # Get data from Term::Cap if possible.
43 11 my ($bold, $undl, $norm, $width);
44 11 eval {
45 11 my $term = Tgetent Term::Cap { TERM => undef, OSPEED => $ospeed };
46 11 $bold = $term->Tputs('md');
47 11 $undl = $term->Tputs('us');
48 11 $norm = $term->Tputs('me');
49 11 ▶ if (defined $$term{_co}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $term->{"_co"}) 10 1 11
50 10 $width = $$term{_co};
51 10 $width =~ s/^\#//;
52 }
53 };
54  
55 # Figure out the terminal width before calling the Pod::Text constructor,
56 # since it will otherwise force 76 characters. Pod::Text::Termcap has
57 # historically used 2 characters less than the width of the screen, while
58 # the other Pod::Text classes have used 76. This is weirdly inconsistent,
59 # but there's probably no good reason to change it now.
60 11 ▶ unless (defined $args{width}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"width"}) 9 2 11
61 9 ▶ $args{width} = $ENV{COLUMNS} || $width || 80;
Condition40%2/5
$ENV{'COLUMNS'} || $width || 80
l!l
90
$ENV{"COLUMNS"} or $width
l!l&&r!l&&!r
900
MC/DC0%0/2
$ENV{'COLUMNS'} || $width || 80
$ENV{"COLUMNS"} $width
Truth table33%1/3
A or B
A= $ENV{"COLUMNS"}B= $width
ABresult
1X1
011
000
62 9 $args{width} -= 2;
63 }
64  
65 # Initialize Pod::Text.
66 11 $self = $self->SUPER::new (%args);
67  
68 # If we were unable to get any of the formatting sequences, don't attempt
69 # that type of formatting. This will do weird things if bold or underline
70 # were available but normal wasn't, but hopefully that will never happen.
71 11 ▶ $$self{BOLD} = $bold || q{};
Condition100%2/2
$bold || ''
l!l
101
MC/DC100%1/1
$bold || ''
$bold
Truth table100%2/2
A
A= $bold
Aresult
11
00
72 11 ▶ $$self{UNDL} = $undl || q{};
Condition100%2/2
$undl || ''
l!l
92
MC/DC100%1/1
$undl || ''
$undl
Truth table100%2/2
A
A= $undl
Aresult
11
00
73 11 ▶ $$self{NORM} = $norm || q{};
Condition100%2/2
$norm || ''
l!l
101
MC/DC100%1/1
$norm || ''
$norm
Truth table100%2/2
A
A= $norm
Aresult
11
00
74  
75 11 return $self;
76 }
77  
78 # Make level one headings bold.
79 17 ▶sub cmd_head1 {
Subroutine cmd_head1: called CC=1 SCAR=0.0
Pod undocumented
80 17 my ($self, $attrs, $text) = @_;
81 17 $text =~ s/\s+$//;
82 17 $self->SUPER::cmd_head1 ($attrs, "$$self{BOLD}$text$$self{NORM}");
83 }
84  
85 # Make level two headings bold.
86 2 ▶sub cmd_head2 {
Subroutine cmd_head2: called CC=1 SCAR=0.0
Pod undocumented
87 2 my ($self, $attrs, $text) = @_;
88 2 $text =~ s/\s+$//;
89 2 $self->SUPER::cmd_head2 ($attrs, "$$self{BOLD}$text$$self{NORM}");
90 }
91  
92 # Fix up B<> and I<>. Note that we intentionally don't do F<>.
93 25 ▶sub cmd_b { my $self = shift; return "$$self{BOLD}$_[1]$$self{NORM}" }
Subroutine cmd_b: called CC=1 SCAR=0.0
Pod undocumented
94 19 ▶sub cmd_i { my $self = shift; return "$$self{UNDL}$_[1]$$self{NORM}" }
Subroutine cmd_i: called CC=1 SCAR=0.0
Pod undocumented
95  
96 # Return a regex that matches a formatting sequence. This will only be valid
97 # if we were able to get at least some termcap information.
98 190 ▶sub format_regex {
Subroutine format_regex: called CC=2 SCAR=6.9
Pod undocumented
99 190 my ($self) = @_;
100 190 my @codes = ($self->{BOLD}, $self->{UNDL}, $self->{NORM});
101 190 ▶ return join(q{|}, map { $_ eq q{} ? () : "\Q$_\E" } @codes);
Branch: 1 branch
BranchTrueFalseTotal
$_ eq '' ? : 1 569 570
102 }
103  
104 # Analyze a single line and return any formatting codes in effect at the end
105 # of that line.
106 74 ▶sub end_format {
Subroutine end_format: called CC=3 SCAR=11.0
Pod undocumented
107 74 my ($self, $line) = @_;
108 74 my $pattern = "(" . $self->format_regex() . ")";
109 74 my $current;
110 74 ▶ while ($line =~ /$pattern/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /$pattern/gu) 55 74 129
111 55 my $code = $1;
112 55 ▶ if ($code eq $$self{NORM}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code eq $self->{'NORM'}) { } 27 28 55
113 27 undef $current;
114 } else {
115 28 $current .= $code;
116 }
117 }
118 74 return $current;
119 }
120  
121 # Output any included code in bold.
122 0 ▶sub output_code {
Subroutine output_code: not called CC=1 SCAR=6.9
Pod undocumented
123 0 my ($self, $code) = @_;
124 0 $self->output ($$self{BOLD} . $code . $$self{NORM});
125 }
126  
127 # Strip all of the formatting from a provided string, returning the stripped
128 # version.
129 33 ▶sub strip_format {
Subroutine strip_format: called CC=1 SCAR=0.0
Pod undocumented
130 33 my ($self, $text) = @_;
131 33 $text =~ s/\Q$$self{BOLD}//g;
132 33 $text =~ s/\Q$$self{UNDL}//g;
133 33 $text =~ s/\Q$$self{NORM}//g;
134 33 return $text;
135 }
136  
137 # Override the wrapping code to ignore the special sequences.
138 115 ▶sub wrap {
Subroutine wrap: called CC=13 SCAR=25.9
Pod undocumented
139 115 my $self = shift;
140 115 local $_ = shift;
141 115 my $output = '';
142 115 my $spaces = ' ' x $$self{MARGIN};
143 115 my $width = $$self{opt_width} - $$self{MARGIN};
144  
145 # If we were unable to find any termcap sequences, use Pod::Text wrapping.
146 115 ▶ if ($self->{BOLD} eq q{} && $self->{UNDL} eq q{} && $self->{NORM} eq q{}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"BOLD"} eq "" and $self->{"UNDL"} eq "" and $self->{"NORM"} eq "") 1 114 115
Condition66%4/6
$self->{"BOLD"} eq "" and $self->{"UNDL"} eq "" and $self->{"NORM"} eq ""
!ll&&!rl&&r
11401
$self->{"BOLD"} eq "" and $self->{"UNDL"} eq ""
!ll&&!rl&&r
11401
MC/DC33%1/3
$self->{"BOLD"} eq "" and $self->{"UNDL"} eq "" and $self->{"NORM"} eq ""
$self->{"BOLD"} eq "" $self->{"UNDL"} eq "" $self->{"NORM"} eq ""
Truth table50%2/4
A and B and C
A= $self->{"BOLD"} eq ""B= $self->{"UNDL"} eq ""C= $self->{"NORM"} eq ""
ABCresult
0XX0
10X0
1100
1111
147 1 return $self->SUPER::wrap($_);
148 }
149  
150 # Pathological margins make wrapping impossible. In this case, complain to
151 # the user and reset the margin to 0.
152 114 ▶ if ($width <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($width <= 0) 1 113 114
153 1 my $error = 'Margin is wider than the output width';
154 1 $self->whine($self->line_count(), $error);
155 1 $spaces = q{};
156 1 $width = $self->{opt_width};
157 }
158  
159 # $code matches a single special sequence. $char matches any number of
160 # special sequences preceding a single character other than a newline.
161 # $shortchar matches some sequence of $char ending in codes followed by
162 # whitespace or the end of the string. $longchar matches exactly $width
163 # $chars, used when we have to truncate and hard wrap.
164 114 my $code = "(?:" . $self->format_regex() . ")";
165 114 my $char = "(?>$code*[^\\n])";
166 114 my $shortchar = '^(' . $char . "{0,$width}(?>$code*)" . ')(?:[ \t\n]+|\z)';
167 114 my $longchar = '^(' . $char . "{$width})";
168 114 ▶ while (length > $width) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ > $width) 45 114 159
169 45 ▶ if (s/$shortchar// || s/$longchar//) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/$shortchar//u or s/$longchar//u) { } 45 0 45
Condition33%1/3
s/$shortchar//u or s/$longchar//u
l!l&&r!l&&!r
4500
MC/DC0%0/2
s/$shortchar//u or s/$longchar//u
s/$shortchar//u s/$longchar//u
Truth table33%1/3
A or B
A= s/$shortchar//uB= s/$longchar//u
ABresult
1X1
011
000
170 45 $output .= $spaces . $1 . "\n";
171 } else {
172 0 last;
173 }
174 }
175 114 $output .= $spaces . $_;
176  
177 # less -R always resets terminal attributes at the end of each line, so we
178 # need to clear attributes at the end of lines and then set them again at
179 # the start of the next line. This requires a second pass through the
180 # wrapped string, accumulating any attributes we see, remembering them,
181 # and then inserting the appropriate sequences at the newline.
182 114 ▶ if ($output =~ /\n/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output =~ /\n/u) 29 85 114
183 29 my @lines = split (/\n/, $output);
184 29 my $start_format;
185 29 for my $line (@lines) {
186 74 ▶ if ($start_format && $line =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start_format and $line =~ /\S/u) 1 73 74
Condition66%2/3
$start_format and $line =~ /\S/u
!ll&&!rl&&r
7301
MC/DC50%1/2
$start_format and $line =~ /\S/u
$start_format $line =~ /\S/u
Truth table66%2/3
A and B
A= $start_formatB= $line =~ /\S/u
ABresult
0X0
100
111
187 1 $line =~ s/^(\s*)(\S)/$1$start_format$2/;
188 }
189 74 $start_format = $self->end_format ($line);
190 74 ▶ if ($start_format) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start_format) 1 73 74
191 1 $line .= $$self{NORM};
192 }
193 }
194 29 $output = join ("\n", @lines);
195 }
196  
197 # Fix up trailing whitespace and return the results.
198 114 $output =~ s/\s+$/\n\n/;
199 114 return $output;
200 }
201  
202 ##############################################################################
203 # Module return value and documentation
204 ##############################################################################
205  
206 2 1;
207 __END__
« Text.pm ↑ Summary ParseLink.pm »