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