keep $VERSION right in the repo
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / Colors.pm
CommitLineData
1716b200 1use strict;
2use warnings;
653a2194 3package Devel::REPL::Plugin::Colors;
4
54beb05d 5our $VERSION = '1.003027';
6
6a5409bc 7use Devel::REPL::Plugin;
653a2194 8use Term::ANSIColor;
aa8b7647 9use namespace::autoclean;
653a2194 10
11has normal_color => (
12 is => 'rw', lazy => 1,
13 default => 'green',
14);
15
16has error_color => (
17 is => 'rw', lazy => 1,
18 default => 'bold red',
19);
20
e22aa835 21around format_error => sub {
653a2194 22 my $orig = shift;
23 my $self = shift;
24 return color($self->error_color)
25 . $orig->($self, @_)
26 . color('reset');
27};
28
29# we can't just munge @_ because that screws up DDS
e22aa835 30around format_result => sub {
653a2194 31 my $orig = shift;
32 my $self = shift;
807af0f7 33 no warnings 'uninitialized';
e22aa835 34 return join "", (
35 color($self->normal_color),
36 $orig->($self, @_),
37 color('reset'),
38 );
653a2194 39};
40
41# make arbitrary warns colored -- somewhat difficult because warn doesn't
42# get $self, so we localize $SIG{__WARN__} during eval so it can get
43# error_color
44
81d2f8e6 45sub _wrap_warn {
653a2194 46 my $orig = shift;
47 my $self = shift;
48
49 local $SIG{__WARN__} = sub {
50 my $warning = shift;
51 chomp $warning;
52 warn color($self->error_color || 'bold red')
53 . $warning
54 . color('reset')
55 . "\n";
56 };
57
58 $orig->($self, @_);
59};
60
81d2f8e6 61around compile => \&_wrap_warn;
62around execute => \&_wrap_warn;
63
653a2194 641;
65
66__END__
67
68=head1 NAME
69
70Devel::REPL::Plugin::Colors - add color to return values, warnings, and errors
71
72=head1 SYNOPSIS
73
653a2194 74 use Devel::REPL;
75
76 my $repl = Devel::REPL->new;
77 $repl->load_plugin('LexEnv');
78 $repl->load_plugin('History');
79 $repl->load_plugin('Colors');
80 $repl->run;
81
82=head1 DESCRIPTION
83
84Colors are very pretty.
85
86This plugin causes certain prints, warns, and errors to be colored. Generally
87the return value(s) of each line will be colored green (you can override this
88by setting C<< $_REPL->normal_color >> in your rcfile). Warnings and
89compile/runtime errors will be colored with C<< $_REPL->error_color >>. This
90plugin uses L<Term::ANSIColor>, so consult that module for valid colors. The
91defaults are actually 'green' and 'bold red'.
92
93=head1 SEE ALSO
94
95C<Devel::REPL>
96
97=head1 AUTHOR
98
99Shawn M Moore, C<< <sartak at gmail dot com> >>
100
101=head1 COPYRIGHT AND LICENSE
102
103Copyright (C) 2007 by Shawn M Moore
104
105This library is free software; you can redistribute it and/or modify
106it under the same terms as Perl itself.
107
108=cut