blib/lib/IO/Factotum/skylighting.pm

filter: Statementstmt 35.2% 18 / 51 Branchbran 4.1% 1 / 24 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 55.5% 5 / 9 Podpod 0.0% 0 / 5 totaltotal 18.4% 24 / 130 CC 24 SCAR 38.8
CC
24
Cov
19%
interpreter
90.0
match
56.0
highlight
42.0
CRAP 48.2
↑ Summary blib/lib/IO/Factotum/sourcehighlight.pm »
1 package IO::Factotum::skylighting;
2  
3 1 ▶use v5.16;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use IO::Factotum;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our @COMMAND = qw(skylighting -f ansi -r -C 16 -S pygments);
9  
10 # A file larger than this is left as it is, 0 for no limit. The
11 # colored text is many times larger than the file, and takes time to
12 # fold: some seconds for a megabyte.
13 1 our $MAX_SIZE = 256 * 1024;
14  
15 # A file which has an escape character is left as it is: skylighting
16 # takes the ANSI sequences in it apart, and exits with status 0.
17 # So are an empty file, which would get a line, and a large one.
18 0 ▶sub match {
Subroutine match: not called CC=7 SCAR=40.3
Pod undocumented
19 0 my $file = shift;
20 0 ▶ return 0 unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
21 0 ▶ my $size = -s _ or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $size = -s _ or return 0 0 0 0
22 0 ▶ return 0 if $MAX_SIZE and $size > $MAX_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Factotum::skylighting::MAX_SIZE and $size > $IO::Factotum::skylighting::MAX_SIZE 0 0 0
Condition0%0/3
$IO::Factotum::skylighting::MAX_SIZE and $size > $IO::Factotum::skylighting::MAX_SIZE
!ll&&!rl&&r
000
MC/DC0%0/2
$IO::Factotum::skylighting::MAX_SIZE and $size > $IO::Factotum::skylighting::MAX_SIZE
$IO::Factotum::skylighting::MAX_SIZE $size > $IO::Factotum::skylighting::MAX_SIZE
Truth table0%0/3
A and B
A= $IO::Factotum::skylighting::MAX_SIZEB= $size > $IO::Factotum::skylighting::MAX_SIZE
ABresult
0X0
100
111
23 0 ▶ open my $fh, '<:raw', $file or return 0;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $file or return 0 0 0 0
24 0 local $/;
25 0 ▶ my $content = <$fh> // return 0;
Condition0%0/2
readline $fh // (return 0)
l!l
00
MC/DC0%0/1
readline $fh // (return 0)
readline $fh
Truth table0%0/2
A
A= readline $fh
Aresult
11
00
26 0 index($content, "\e") < 0;
27 }
28  
29 # The names of interpreters which are not the names of the syntax
30 1 my %SYNTAX = (
31 node => 'javascript',
32 tclsh => 'tcl',
33 ksh => 'bash',
34 dash => 'bash',
35 );
36  
37 # The syntax of a script, by the interpreter on its first line
38 # (#!/usr/bin/env perl, #!/usr/bin/python3): the name of the command,
39 # after env and its options and variables, without the version number.
40 0 ▶sub interpreter {
Subroutine interpreter: not called CC=9 SCAR=45.0
Pod undocumented
41 0 my $line = shift;
42 0 ▶ $line =~ /\A#!\s*(\S.*)/ or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /\A#!\s*(\S.*)/u or return undef 0 0 0
43 0 my @word = split ' ', $1;
44 0 ▶ my $command = shift @word // return undef;
Condition0%0/2
shift @word // (return undef)
l!l
00
MC/DC0%0/1
shift @word // (return undef)
shift @word
Truth table0%0/2
A
A= shift @word
Aresult
11
00
45 0 ▶ if ($command =~ m{(?:\A|/)env\z}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command =~ m[(?:\A|/)env\z]u) 0 0 0
46 0 ▶ shift @word while @word and $word[0] =~ /\A-|=/;
Branch: 1 branch
BranchTrueFalseTotal
@word and $word[0] =~ /\A-|=/u and do { shift @word } 0 0 0
Condition0%0/3
@word and $word[0] =~ /\A-|=/u
!ll&&!rl&&r
000
MC/DC0%0/2
@word and $word[0] =~ /\A-|=/u
@word $word[0] =~ /\A-|=/u
Truth table0%0/3
A and B
A= @wordB= $word[0] =~ /\A-|=/u
ABresult
0X0
100
111
47 0 ▶ $command = shift @word // return undef;
Condition0%0/2
shift @word // (return undef)
l!l
00
MC/DC0%0/1
shift @word // (return undef)
shift @word
Truth table0%0/2
A
A= shift @word
Aresult
11
00
48 }
49 0 my $name = $command =~ s{.*/}{}r =~ s/[\d.]+\z//r;
50 0 ▶ return undef if $name eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "" 0 0 0
51 0 ▶ $SYNTAX{$name} // $name;
Condition0%0/3
$SYNTAX{$name} // $name
l!l&&r!l&&!r
000
MC/DC0%0/2
$SYNTAX{$name} // $name
$SYNTAX{$name} $name
Truth table0%0/3
A // B
A= $SYNTAX{$name}B= $name
ABresult
1X1
011
000
52 }
53  
54 # Run skylighting with the arguments, and return what it writes, or
55 # undef when it fails. Its messages are thrown away by the caller.
56 0 ▶sub skylighting {
Subroutine skylighting: not called CC=4 SCAR=30.0
Pod undocumented
57 0 ▶ open my $fh, '-|', @COMMAND, @_ or return undef;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "-|", @IO::Factotum::skylighting::COMMAND, @_ or return undef 0 0 0
58 0 my $out = do { local $/; <$fh> };
59 0 ▶ close $fh or return undef;
Branch: 1 branch
BranchTrueFalseTotal
close $fh or return undef 0 0 0
60 0 ▶ $out // '';
Condition0%0/2
$out // ""
l!l
00
MC/DC0%0/1
$out // ""
$out
Truth table0%0/2
A
A= $out
Aresult
11
00
61 }
62  
63 # Run skylighting on the file given as the last argument, and print
64 # what it gives without the sequences which do nothing: skylighting
65 # puts \e[22;23;24m after every reset, and resets twice at the top.
66 # A line which ends with two sequences gets an empty line after it
67 # when it is folded by Text::ANSI::Fold and the text fills the width.
68 # This is called in a process of its own (IO::Factotum), and dies
69 # when skylighting fails: the file is then read as it is.
70 0 ▶sub highlight {
Subroutine highlight: not called CC=6 SCAR=37.4
Pod undocumented
71 0 my $file = $_[-1];
72 # skylighting tells the language from the name of the file. When
73 # it can not, the interpreter on the first line tells it.
74 0 ▶ my $out = skylighting($file) // do {
Condition0%0/3
skylighting($file) // do { open my $fh, "<", $file or die "${file}: $!\n"; my $syntax = interpreter(scalar readline $fh // "") // die("skylighting failed\n"); skylighting("-s", $syntax, $file) // die("skylighting failed\n") }
l!l&&r!l&&!r
000
MC/DC0%0/2
skylighting($file) // do { open my $fh, "<", $file or die "${file}: $!\n"; my $syntax = interpreter(scalar readline $fh // "") // die("skylighting failed\n"); skylighting("-s", $syntax, $file) // die("skylighting failed\n") }
skylighting($file) do { open my $fh, "<", $file or die "${file}: $!\n"; my $syntax = interpreter(scalar readline $fh // "") // die("skylighting failed\n"); skylighting("-s", $syntax, $file) // die("skylighting failed\n") }
Truth table0%0/3
A // B
A= skylighting($file)B= do { open my $fh, "<", $file or die "${file}: $!\n"; my $syntax = interpreter(scalar readline $fh // "") // die("skylighting failed\n"); skylighting("-s", $syntax, $file) // die("skylighting failed\n") }
ABresult
1X1
011
000
75 0 ▶ open my $fh, '<', $file or die "$file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "${file}: $!\n" 0 0 0
76 0 ▶ my $syntax = interpreter(scalar <$fh> // '')
Condition0%0/4
interpreter(scalar readline $fh // "") // die("skylighting failed\n")
l!l
00
scalar readline $fh // ""
l!l
00
MC/DC0%0/1
interpreter(scalar readline $fh // "") // die("skylighting failed\n")
interpreter(scalar readline $fh // "")
MC/DC0%0/1
scalar readline $fh // ""
scalar readline $fh
Truth table0%0/2
A
A= interpreter(scalar readline $fh // "")
Aresult
11
00
Truth table0%0/2
A
A= scalar readline $fh
Aresult
11
00
77 // die "skylighting failed\n";
78 0 ▶ skylighting('-s', $syntax, $file) // die "skylighting failed\n";
Condition0%0/2
skylighting("-s", $syntax, $file) // die("skylighting failed\n")
l!l
00
MC/DC0%0/1
skylighting("-s", $syntax, $file) // die("skylighting failed\n")
skylighting("-s", $syntax, $file)
Truth table0%0/2
A
A= skylighting("-s", $syntax, $file)
Aresult
11
00
79 };
80 0 $out =~ s/\e\[0m\K\e\[22;23;24m//g;
81 0 $out =~ s/\e\[0m\K(?:\e\[0m)+//g;
82 # CR LF stay together: the reset comes between them
83 0 $out =~ s/\r((?:\e\[[0-9;]*m)+)$/$1\r/mg;
84 0 print $out;
85 }
86  
87 1 my $done;
88 1 ▶sub setup {
Subroutine setup: called CC=2 SCAR=7.1
Pod undocumented
89 1 ▶ return if $done++;
Branch: 1 branch
BranchTrueFalseTotal
if $done++ 0 1 1
90 1 IO::Factotum::register(match => \&match, command => [ \&highlight ]);
91 1 return;
92 }
93  
94 1 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
95 1 setup();
96 1 IO::Factotum::tie_argv();
97 }
98  
99 1 1;
100  
101 __END__
↑ Summary blib/lib/IO/Factotum/sourcehighlight.pm »