| 1 | # ============================================================================ | ||
| 2 | package MooseX::App::Message::BlockColor; | ||
| 3 | # ============================================================================ | ||
| 4 | |||
| 5 | use 5.010; | ||
| 6 | use utf8; | ||
| 7 | |||
| 8 | use namespace::autoclean; | ||
| 9 | use Moose; | ||
| 10 | extends qw(MooseX::App::Message::Block); | ||
| 11 | |||
| 12 | use Term::ANSIColor qw(); | ||
| 13 | use IO::Interactive qw(is_interactive); | ||
| 14 | |||
| 15 | BEGIN { | ||
| 16 | if ($^O eq 'MSWin32') { | ||
| 17 | Class::Load::try_load_class('Win32::Console::ANSI'); | ||
| 18 | } | ||
| 19 | }; | ||
| 20 | |||
| 21 | sub stringify { | ||
| 22 | my ($self) = @_; | ||
| 23 | |||
| 24 | my $header_color; | ||
| 25 | my $body_color; | ||
| 26 | my $type = $self->type; | ||
| 27 | if($type eq 'error') { | ||
| 28 | $header_color = 'bright_red bold'; | ||
| 29 | $body_color = 'bright_red'; | ||
| 30 | } | ||
| 31 | elsif($type eq 'default') { | ||
| 32 | $header_color = 'bold'; | ||
| 33 | } | ||
| 34 | else { | ||
| 35 | $header_color = $type; | ||
| 36 | } | ||
| 37 | |||
| 38 | my $message = ''; | ||
| 39 | if ($self->has_header) { | ||
| 40 | $message .= $self->_wrap_color($header_color,$self->header)."\n"; | ||
| 41 | } | ||
| 42 | |||
| 43 | if ($self->has_body) { | ||
| 44 | $message .= $self->_wrap_color($body_color,$self->body)."\n\n"; | ||
| 45 | } | ||
| 46 | |||
| 47 | return $message; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub _wrap_color { | ||
| 51 | my ($self,$color,$string) = @_; | ||
| 52 | |||
| 53 | return $string | ||
| 54 | unless is_interactive() | ||
| 55 | && defined $color; | ||
| 56 | |||
| 57 | return Term::ANSIColor::color($color) | ||
| 58 | .$string | ||
| 59 | .Term::ANSIColor::color('reset'); | ||
| 60 | } | ||
| 61 | |||
| 62 | __PACKAGE__->meta->make_immutable; | ||
| 63 | 1; |