add use strict; use warnings to modules, just to be sure
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / Colors.pm
CommitLineData
1716b200 1use strict;
2use warnings;
653a2194 3package Devel::REPL::Plugin::Colors;
4
6a5409bc 5use Devel::REPL::Plugin;
653a2194 6use Term::ANSIColor;
aa8b7647 7use namespace::autoclean;
653a2194 8
9has normal_color => (
10 is => 'rw', lazy => 1,
11 default => 'green',
12);
13
14has error_color => (
15 is => 'rw', lazy => 1,
16 default => 'bold red',
17);
18
e22aa835 19around format_error => sub {
653a2194 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
e22aa835 28around format_result => sub {
653a2194 29 my $orig = shift;
30 my $self = shift;
807af0f7 31 no warnings 'uninitialized';
e22aa835 32 return join "", (
33 color($self->normal_color),
34 $orig->($self, @_),
35 color('reset'),
36 );
653a2194 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
81d2f8e6 43sub _wrap_warn {
653a2194 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
81d2f8e6 59around compile => \&_wrap_warn;
60around execute => \&_wrap_warn;
61
653a2194 621;
63
64__END__
65
66=head1 NAME
67
68Devel::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
85Colors are very pretty.
86
87This plugin causes certain prints, warns, and errors to be colored. Generally
88the return value(s) of each line will be colored green (you can override this
89by setting C<< $_REPL->normal_color >> in your rcfile). Warnings and
90compile/runtime errors will be colored with C<< $_REPL->error_color >>. This
91plugin uses L<Term::ANSIColor>, so consult that module for valid colors. The
92defaults are actually 'green' and 'bold red'.
93
94=head1 SEE ALSO
95
96C<Devel::REPL>
97
98=head1 AUTHOR
99
100Shawn M Moore, C<< <sartak at gmail dot com> >>
101
102=head1 COPYRIGHT AND LICENSE
103
104Copyright (C) 2007 by Shawn M Moore
105
106This library is free software; you can redistribute it and/or modify
107it under the same terms as Perl itself.
108
109=cut