blib/lib/App/UnANSI.pm

filter: Statementstmt 64.7% 33 / 51 Branchbran 33.3% 6 / 18 Conditioncond 28.5% 2 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 58.3% 7 / 12 Podpod 100% 5 / 5 totaltotal 54.6% 53 / 97 CC 15 SCAR 18.2
CC
15
Cov
54%
_help
12.0
run
9.1
new_with_options
6.0
CRAP 6.2
↑ Summary
1 package App::UnANSI;
2 3 our $AUTHORITY = 'cpan:XSAWYERX';
3 # ABSTRACT: Remove ANSI coloring from output or files
4 3 $App::UnANSI::VERSION = '0.003';
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Getopt::Long qw<:config no_ignore_case>;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # Helper function
10 0 ▶sub _help {
Subroutine _help: not called CC=3 SCAR=24.8
11 0 my $msg = shift;
12  
13 0 ▶ $msg
Branch: 1 branch
BranchTrueFalseTotal
$msg and print "Error: $msg\n\n" 0 0 0
14 and print "Error: $msg\n\n";
15  
16 0 print << "_HELP";
17 command_that_creates_colored_output | $0 [OPTIONS] FILE1 FILE2...FILEN
18  
19 Options:
20  
21 -h | --help Print this help menu and exit
22 -v | --version Print version number and exit
23 _HELP
24  
25 0 ▶ if ($msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg) { } 0 0 0
26 0 exit 2;
27 } else {
28 0 exit 0;
29 }
30 }
31  
32 0 ▶sub new_with_options {
Subroutine new_with_options: not called CC=2 SCAR=17.9
Pod documented
33 0 my ( $class, %opts ) = @_;
34  
35 0 my @files;
36 GetOptions(
37 0 ▶ 'help|h' => sub { _help(); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
38 0 ▶ 'version|v' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
39 0 ▶ my $version = $App::UnANSI::VERSION || 'DEV';
Condition0%0/2
$App::UnANSI::VERSION || 'DEV'
l!l
00
MC/DC0%0/1
$App::UnANSI::VERSION || 'DEV'
$App::UnANSI::VERSION
Truth table0%0/2
A
A= $App::UnANSI::VERSION
Aresult
11
00
40 0 print "$0 $version\n";
41 0 exit 0;
42 },
43 0 ▶ '<>' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
44 0 push @files, @_;
45 },
46 0 ▶ ) or _help();
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("help|h", sub { _help(); } , "version|v", sub { my $version = $App::UnANSI::VERSION || "DEV"; print "$0 $version\n"; exit 0; } , "<>", sub { push @files, @_; } ) or _help() 0 0 0
47  
48 0 return $class->new( 'files' => \@files );
49 }
50  
51 2 ▶sub new {
Subroutine new: called CC=5 SCAR=17.5
Pod documented
52 2 my ( $class, %opts ) = @_;
53  
54 2 ▶ $opts{'files'} ||= [];
Condition50%1/2
$opts{"files"} ||= []
l!l
20
MC/DC0%0/1
$opts{"files"} ||= []
$opts{"files"}
Truth table50%1/2
A
A= $opts{"files"}
Aresult
11
00
55  
56 2 foreach my $file ( @{ $opts{'files'} } ) {
57 2 ▶ -e $file && -r $file
Branch: 1 branch
BranchTrueFalseTotal
-e $file and -r $file or _help("$file is not a readable file") 2 0 2
Condition33%1/3
-e $file and -r $file
!ll&&!rl&&r
002
MC/DC0%0/2
-e $file and -r $file
-e $file -r $file
Truth table33%1/3
A and B
A= -e $fileB= -r $file
ABresult
0X0
100
111
58 or _help("$file is not a readable file")
59 }
60  
61 2 return bless {%opts}, $class;
62 }
63  
64 2 ▶sub files {
Subroutine files: called CC=1 SCAR=0.0
Pod documented
65 2 my $self = shift;
66 2 return @{ $self->{'files'} };
67 }
68  
69 2 ▶sub run {
Subroutine run: called CC=7 SCAR=22.0
Pod documented
70 2 my $self = shift;
71 2 my @files = $self->files;
72  
73 2 ▶ if ( !@files ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @files) { } 0 2 2
74 # Work on STDIN
75 0 ▶ while ( my $line = <STDIN> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline STDIN)) 0 0 0
76 0 $self->remove_ansi_colors($line);
77 0 print $line;
78 }
79 } else {
80 # Work on files
81 2 foreach my $file (@files) {
82 2 ▶ open my $fh, '<', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "Cannot open ${file}: $!\n" 2 0 2
83 or die "Cannot open $file: $!\n";
84  
85 2 ▶ while ( my $line = <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 2 2 4
86 2 $self->remove_ansi_colors($line);
87 2 print $line;
88 }
89  
90 2 ▶ close $fh
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Cannot close ${file}: $!\n" 2 0 2
91 or die "Cannot close $file: $!\n";
92 }
93 }
94  
95 2 return 1;
96 }
97  
98 2 ▶sub remove_ansi_colors {
Subroutine remove_ansi_colors: called CC=1 SCAR=0.0
Pod documented
99 2 $_[1] =~ s{\x1b\[[^m]*m}{}sgmx;
100 2 return 1;
101 }
102  
103 3 1;
104  
105 __END__
↑ Summary