Skip to content

Commit

Permalink
Only color when output goes to terminal.
Browse files Browse the repository at this point in the history
Also make it much easier to configurate what colors to use.
  • Loading branch information
gisle committed Jan 12, 2009
1 parent 0177d4d commit d9e64fd
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions lib/Data/Dump/Trace.pm
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,18 @@ sub AUTOLOAD {

package Data::Dump::Trace::Call;

use Term::ANSIColor qw(YELLOW CYAN RESET);
use Term::ANSIColor ();
use Data::Dump ();

*_dump = \&Data::Dump::dump;

our %COLOR = (
name => "yellow",
output => "cyan",
);

%COLOR = () unless -t STDOUT;

sub _dumpav {
return "(" . _dump(@_) . ")" if @_ == 1;
return _dump(@_);
Expand All @@ -136,12 +143,18 @@ sub new {
return $self;
}

sub color {
my($self, $category, $text) = @_;
return $text unless $COLOR{$category};
return Term::ANSIColor::colored($text, $COLOR{$category});
}

sub print_call {
my $self = shift;
my $arg = shift;
my $input = $self->{input};
$input = "" if $input eq "()" && $self->{name} =~ /->/;
print YELLOW, "$self->{name}", RESET, $input;
print $self->color("name", "$self->{name}"), $self->color("input", $input);
}

sub return_void {
Expand All @@ -159,11 +172,11 @@ sub return_scalar {
my $s = shift;
if (my $name = $autowrap_class{ref($s)}) {
$name .= $name_count{$name} if $name_count{$name}++;
print " ==> ", CYAN, $name, RESET, "\n";
print " ==> ", $self->color("output", $name), "\n";
$s = Data::Dump::Trace::wrap(name => $name, obj => $s);
}
else {
print " ==> ", CYAN, _dump($s), RESET, "\n";
print " ==> ", $self->color("output", _dump($s)), "\n";
}
return $s;
}
Expand All @@ -172,7 +185,7 @@ sub return_list {
my $self = shift;
my $arg = shift;
$self->print_call($arg);
print " ==> ", CYAN, _dumpav(@_), RESET, "\n";
print " ==> ", $self->color("output", _dumpav(@_)), "\n";
return @_;
}

Expand Down

0 comments on commit d9e64fd

Please sign in to comment.