| 1 | package Zed::Output; | ||||||||||
| 2 | |||||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | Zed::Output - export some output method used by zed. | ||||||||||
| 6 | |||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||
| 8 | |||||||||||
| 9 | use Zed::Output; | ||||||||||
| 10 | |||||||||||
| 11 | info "info~~"; | ||||||||||
| 12 | error "error~~"; | ||||||||||
| 13 | result 'first' => 0, "bla bla fail~~"; | ||||||||||
| 14 | result 'second' => 1, "bla bla suc~~"; | ||||||||||
| 15 | text "text~~"; | ||||||||||
| 16 | |||||||||||
| 17 | Zed::Output::_debug("PackageA"); | ||||||||||
| 18 | Zed::Output::_debug_off("PackageB"); | ||||||||||
| 19 | |||||||||||
| 20 | package PackageA; | ||||||||||
| 21 | use Zed::Output; | ||||||||||
| 22 | |||||||||||
| 23 | debug("debug msg!"); | ||||||||||
| 24 | |||||||||||
| 25 | package PackageB; | ||||||||||
| 26 | use Zed::Output; | ||||||||||
| 27 | |||||||||||
| 28 | debug("debug msg! you can't see!"); | ||||||||||
| 29 | |||||||||||
| 30 | =cut | ||||||||||
| 31 | |||||||||||
| 32 | 1 | ▶ | use base Exporter; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | 1 | our @EXPORT = qw( debug info error result text); | |||||||||
| 34 | |||||||||||
| 35 | 1 | ▶ | use Term::ANSIColor qw(:constants colored); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 36 | 1 | $Term::ANSIColor::AUTORESET = 1; | |||||||||
| 37 | 1 | ▶ | use Data::Printer output => 'stdout'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | |||||||||||
| 39 | 1 | my %switch; | |||||||||
| 40 | 0 | ▶ | sub _debug { my $pack = shift; $switch{ $pack } = 1;}; | ||||||||
|
Subroutine
_debug: not called CC=1 SCAR=6.9
| |||||||||||
| 41 | 0 | ▶ | sub _debug_off { my $pack = shift; delete $switch{ $pack }; }; | ||||||||
|
Subroutine
_debug_off: not called CC=1 SCAR=6.9
| |||||||||||
| 42 | |||||||||||
| 43 | sub debug | ||||||||||
| 44 | { | ||||||||||
| 45 | 0 | ▶ | my($package, $filename, $line) = caller; | ||||||||
|
Subroutine
debug: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||
| 46 | 0 | ▶ | return unless $switch{ $package }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 0 | print "[$package:$line] "; | |||||||||
| 48 | 0 | ▶ | ref $_ ? p $_ : print $_ for @_; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 0 | ▶ | print "\n" unless ref $_[-1] | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | } | ||||||||||
| 51 | sub result | ||||||||||
| 52 | { | ||||||||||
| 53 | 0 | ▶ | my( $prefix, $suc, $str ) = @_; | ||||||||
|
Subroutine
result: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 54 | 0 | ▶ | print "[$prefix] ", $suc ? colored(['green'], $str) : colored(['red'], $str), "\n"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | } | ||||||||||
| 56 | 0 | ▶ | sub info { _out('yellow', @_) } | ||||||||
|
Subroutine
info: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 57 | 0 | ▶ | sub error { _out('red', @_) } | ||||||||
|
Subroutine
error: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 58 | 0 | ▶ | sub text { _out('white', @_) } | ||||||||
|
Subroutine
text: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 59 | |||||||||||
| 60 | sub _out | ||||||||||
| 61 | { | ||||||||||
| 62 | 0 | ▶ | my ( $color, $str, $out ) = shift; | ||||||||
|
Subroutine
_out: not called CC=6 SCAR=37.4
| |||||||||||
| 63 | 0 | for(@_) | |||||||||
| 64 | { | ||||||||||
| 65 | 0 | ▶ | if( ref $_) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | { | ||||||||||
| 67 | |||||||||||
| 68 | 0 | ▶ | $out .= np(%$_, colored => 1) if ref $_ eq 'HASH'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 0 | ▶ | $out .= np(@$_, colored => 1) if ref $_ eq 'ARRAY'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 0 | next; | |||||||||
| 71 | } | ||||||||||
| 72 | 0 | $str = $_; | |||||||||
| 73 | 0 | ▶ | while($str =~ /\[.+?\]/) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | { | ||||||||||
| 75 | 0 | debug "prefix: |$`|"; | |||||||||
| 76 | 0 | debug "match: |$&|"; | |||||||||
| 77 | 0 | debug "suffix: |$'|"; | |||||||||
| 78 | 0 | $out .= colored([$color], $`); | |||||||||
| 79 | 0 | $out .= colored([$color,'bold'], $&); | |||||||||
| 80 | 0 | $str = $'; | |||||||||
| 81 | |||||||||||
| 82 | } | ||||||||||
| 83 | 0 | $out .= colored([$color], $str); | |||||||||
| 84 | 0 | debug "out:|$out|"; | |||||||||
| 85 | } | ||||||||||
| 86 | 0 | $out =~ s/(\n*)?$/\n/; | |||||||||
| 87 | 0 | $out =~ s/(suc|success)/colored(['green'], $1)/ge; | |||||||||
| 88 | 0 | $out =~ s/(fail|failed)/colored(['red'], $1)/ge; | |||||||||
| 89 | 0 | print $out; | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | 1 | 1; | |||||||||