From: Sartak Date: Fri, 21 Sep 2007 03:48:48 +0000 (+0000) Subject: 'Colors' plugin to colorize return values and errors (dep on Term::ANSIColor) X-Git-Tag: v1.003015~151 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=653a2194b52cb95e0a6549a2b794384ba7b583b5;p=p5sagit%2FDevel-REPL.git 'Colors' plugin to colorize return values and errors (dep on Term::ANSIColor) git-svn-id: http://dev.catalyst.perl.org/repos/bast/trunk/Devel-REPL@3764 bd8105ee-0ff8-0310-8827-fb3f25b6796d --- diff --git a/Makefile.PL b/Makefile.PL index 43cccc0..0e5e2c1 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -18,6 +18,7 @@ requires 'Term::ReadLine'; requires 'Lexical::Persistence'; requires 'Data::Dump::Streamer'; requires 'PPI'; +requires 'Term::ANSIColor'; auto_install; WriteAll; diff --git a/lib/Devel/REPL/Plugin/Colors.pm b/lib/Devel/REPL/Plugin/Colors.pm new file mode 100644 index 0000000..aa87abf --- /dev/null +++ b/lib/Devel/REPL/Plugin/Colors.pm @@ -0,0 +1,101 @@ +package Devel::REPL::Plugin::Colors; + +use Moose::Role; +use Term::ANSIColor; +use namespace::clean -except => [ 'meta' ]; + +has normal_color => ( + is => 'rw', lazy => 1, + default => 'green', +); + +has error_color => ( + is => 'rw', lazy => 1, + default => 'bold red', +); + +around error_return => sub { + my $orig = shift; + my $self = shift; + return color($self->error_color) + . $orig->($self, @_) + . color('reset'); +}; + +# we can't just munge @_ because that screws up DDS +around print => sub { + my $orig = shift; + my $self = shift; + print {$self->out_fh} color($self->normal_color); + $orig->($self, @_); + print {$self->out_fh} color('reset'); +}; + +# make arbitrary warns colored -- somewhat difficult because warn doesn't +# get $self, so we localize $SIG{__WARN__} during eval so it can get +# error_color + +around execute => sub { + my $orig = shift; + my $self = shift; + + local $SIG{__WARN__} = sub { + my $warning = shift; + chomp $warning; + warn color($self->error_color || 'bold red') + . $warning + . color('reset') + . "\n"; + }; + + $orig->($self, @_); +}; + +1; + +__END__ + +=head1 NAME + +Devel::REPL::Plugin::Colors - add color to return values, warnings, and errors + +=head1 SYNOPSIS + + #!/usr/bin/perl + + use lib './lib'; + use Devel::REPL; + + my $repl = Devel::REPL->new; + $repl->load_plugin('LexEnv'); + $repl->load_plugin('History'); + $repl->load_plugin('Colors'); + $repl->run; + +=head1 DESCRIPTION + +Colors are very pretty. + +This plugin causes certain prints, warns, and errors to be colored. Generally +the return value(s) of each line will be colored green (you can override this +by setting C<< $_REPL->normal_color >> in your rcfile). Warnings and +compile/runtime errors will be colored with C<< $_REPL->error_color >>. This +plugin uses L, so consult that module for valid colors. The +defaults are actually 'green' and 'bold red'. + +=head1 SEE ALSO + +C + +=head1 AUTHOR + +Shawn M Moore, C<< >> + +=head1 COPYRIGHT AND LICENSE + +Copyright (C) 2007 by Shawn M Moore + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself. + +=cut