refactor formatting/printing shit, introduce error object for error_return
[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
e22aa835 17around format_error => sub {
653a2194 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
e22aa835 26around format_result => sub {
653a2194 27 my $orig = shift;
28 my $self = shift;
e22aa835 29 return join "", (
30 color($self->normal_color),
31 $orig->($self, @_),
32 color('reset'),
33 );
653a2194 34};
35
36# make arbitrary warns colored -- somewhat difficult because warn doesn't
37# get $self, so we localize $SIG{__WARN__} during eval so it can get
38# error_color
39
81d2f8e6 40sub _wrap_warn {
653a2194 41 my $orig = shift;
42 my $self = shift;
43
44 local $SIG{__WARN__} = sub {
45 my $warning = shift;
46 chomp $warning;
47 warn color($self->error_color || 'bold red')
48 . $warning
49 . color('reset')
50 . "\n";
51 };
52
53 $orig->($self, @_);
54};
55
81d2f8e6 56around compile => \&_wrap_warn;
57around execute => \&_wrap_warn;
58
653a2194 591;
60
61__END__
62
63=head1 NAME
64
65Devel::REPL::Plugin::Colors - add color to return values, warnings, and errors
66
67=head1 SYNOPSIS
68
69 #!/usr/bin/perl
70
71 use lib './lib';
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