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