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