| 1 | package colorize::handle; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||
| 4 | 2 | our $DATE = '2020-03-21'; # DATE | |||||||||||||||||||||||||||
| 5 | 2 | our $DIST = 'colorize-stderr'; # DIST | |||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = '0.002'; # VERSION | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 0 | ▶ | BEGIN { if ($^O =~ /^(MSWin32)$/) { require Win32::Console::ANSI } } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=9.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use PerlIO::via::ANSIColor; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 2 | my %colorized; # key = handle | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 0 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 17 | 0 | my ($pkg, $handle, $color) = @_; | |||||||||||||||||||||||||||
| 18 | 0 | ▶ | die "Please specify handle and color" unless $handle && $color; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$handle and $color
MC/DC0%0/2
$handle and $color
$handle
$color
Truth table0%0/3
A and B
A= $handleB= $color
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 0 | ▶ | return if $colorized{$handle}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | my @layers = PerlIO::get_layers($handle); | |||||||||||||||||||||||||||
| 23 | 0 | PerlIO::via::ANSIColor->color($color); | |||||||||||||||||||||||||||
| 24 | 0 | binmode($handle, ":via(ANSIColor)"); | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | $colorized{$handle} = [scalar(@layers)]; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 0 | ▶ | sub unimport { | ||||||||||||||||||||||||||
|
Subroutine
unimport: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 30 | 0 | my ($pkg, $handle) = @_; | |||||||||||||||||||||||||||
| 31 | 0 | ▶ | die "Please specify handle" unless $handle; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | return unless $colorized{$handle}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 0 | my $pos = $colorized{$handle}[0]; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | my @layers = PerlIO::get_layers($handle); | |||||||||||||||||||||||||||
| 37 | 0 | ▶ | if ($pos == $#layers && $layers[$pos] eq 'via') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$pos == $#layers and $layers[$pos] eq "via"
MC/DC0%0/2
$pos == $#layers and $layers[$pos] eq "via"
$pos == $#layers
$layers[$pos] eq "via"
Truth table0%0/3
A and B
A= $pos == $#layersB= $layers[$pos] eq "via"
| |||||||||||||||||||||||||||||
| 38 | # get_layers() only return 'via' so we assume that the last 'via' layer | ||||||||||||||||||||||||||||
| 39 | # is us. | ||||||||||||||||||||||||||||
| 40 | 0 | binmode($handle, ':pop'); | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | undef $colorized{$handle}; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 2 | 1; | |||||||||||||||||||||||||||
| 48 | # ABSTRACT: Colorize a filehandle | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | __END__ | ||||||||||||||||||||||||||||