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