| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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'
MC/DC0%0/1
$App::UnANSI::VERSION || 'DEV'
$App::UnANSI::VERSION
Truth table0%0/2
A
A= $App::UnANSI::VERSION
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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"} ||= []
MC/DC0%0/1
$opts{"files"} ||= []
$opts{"files"}
Truth table50%1/2
A
A= $opts{"files"}
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 2 | foreach my $file ( @{ $opts{'files'} } ) { | |||||||||||||||||||||||||||
| 57 | 2 | ▶ | -e $file && -r $file | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
-e $file and -r $file
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 74 | # Work on STDIN | ||||||||||||||||||||||||||||
| 75 | 0 | ▶ | while ( my $line = <STDIN> ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 83 | or die "Cannot open $file: $!\n"; | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 2 | ▶ | while ( my $line = <$fh> ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 2 | $self->remove_ansi_colors($line); | |||||||||||||||||||||||||||
| 87 | 2 | print $line; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 2 | ▶ | close $fh | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||