remove unneeded shebangs and "use lib" directives
[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
653a2194 72 use Devel::REPL;
73
74 my $repl = Devel::REPL->new;
75 $repl->load_plugin('LexEnv');
76 $repl->load_plugin('History');
77 $repl->load_plugin('Colors');
78 $repl->run;
79
80=head1 DESCRIPTION
81
82Colors are very pretty.
83
84This plugin causes certain prints, warns, and errors to be colored. Generally
85the return value(s) of each line will be colored green (you can override this
86by setting C<< $_REPL->normal_color >> in your rcfile). Warnings and
87compile/runtime errors will be colored with C<< $_REPL->error_color >>. This
88plugin uses L<Term::ANSIColor>, so consult that module for valid colors. The
89defaults are actually 'green' and 'bold red'.
90
91=head1 SEE ALSO
92
93C<Devel::REPL>
94
95=head1 AUTHOR
96
97Shawn M Moore, C<< <sartak at gmail dot com> >>
98
99=head1 COPYRIGHT AND LICENSE
100
101Copyright (C) 2007 by Shawn M Moore
102
103This library is free software; you can redistribute it and/or modify
104it under the same terms as Perl itself.
105
106=cut