| 1 | package Shell::Amazon::S3::Plugin::Colors; | ||
| 2 | |||
| 3 | use Moose::Role; | ||
| 4 | use Term::ANSIColor; | ||
| 5 | use namespace::clean -except => ['meta']; | ||
| 6 | |||
| 7 | has normal_color => ( | ||
| 8 | is => 'rw', | ||
| 9 | lazy => 1, | ||
| 10 | default => 'green', | ||
| 11 | ); | ||
| 12 | |||
| 13 | has error_color => ( | ||
| 14 | is => 'rw', | ||
| 15 | lazy => 1, | ||
| 16 | default => 'bold red', | ||
| 17 | ); | ||
| 18 | |||
| 19 | around error_return => sub { | ||
| 20 | my $orig = shift; | ||
| 21 | my $self = shift; | ||
| 22 | return | ||
| 23 | color( $self->error_color ) | ||
| 24 | . $orig->( $self, @_ ) | ||
| 25 | . color('reset'); | ||
| 26 | }; | ||
| 27 | |||
| 28 | # we can't just munge @_ because that screws up DDS | ||
| 29 | around print => sub { | ||
| 30 | my $orig = shift; | ||
| 31 | my $self = shift; | ||
| 32 | print { $self->out_fh } color( $self->normal_color ); | ||
| 33 | $orig->( $self, @_ ); | ||
| 34 | print { $self->out_fh } color('reset'); | ||
| 35 | }; | ||
| 36 | |||
| 37 | 1; | ||
| 38 | |||
| 39 | __END__ | ||
| 40 | |||
| 41 | =head1 NAME | ||
| 42 | |||
| 43 | Shell::Amazon::S3::Plugin::Colors - add color to return values, warnings, and errors | ||
| 44 | |||
| 45 | =head1 SYNOPSIS | ||
| 46 | |||
| 47 | #!/usr/bin/perl | ||
| 48 | |||
| 49 | use lib './lib'; | ||
| 50 | use Shell::Amazon::S3; | ||
| 51 | |||
| 52 | my $repl = Shell::Amazon::S3->new; | ||
| 53 | $repl->load_plugin('History'); | ||
| 54 | $repl->load_plugin('Colors'); | ||
| 55 | $repl->run; | ||
| 56 | |||
| 57 | =head1 DESCRIPTION | ||
| 58 | |||
| 59 | |||
| 60 | =head1 SEE ALSO | ||
| 61 | |||
| 62 | C<Shell::Amazon::S3> | ||
| 63 | |||
| 64 | =head1 AUTHOR | ||
| 65 | |||
| 66 | Dann C<< <techmemo at gmail dot com> >> | ||
| 67 | |||
| 68 | =head1 COPYRIGHT AND LICENSE | ||
| 69 | |||
| 70 | Copyright (C) 2008 Dann | ||
| 71 | |||
| 72 | This library is free software; you can redistribute it and/or modify | ||
| 73 | it under the same terms as Perl itself. | ||
| 74 | |||
| 75 | =cut |