| 1 | # ABSTRACT: Shared colour decision for the renderers that colour their output | ||
| 2 | |||
| 3 | package App::karr::Role::Color; | ||
| 4 | our $VERSION = '0.602'; | ||
| 5 | use Moo::Role; | ||
| 6 | use MooX::Options; | ||
| 7 | |||
| 8 | |||
| 9 | option color => ( | ||
| 10 | is => 'ro', | ||
| 11 | negatable => 1, | ||
| 12 | doc => 'Colour output; --no-color disables it for this invocation', | ||
| 13 | ); | ||
| 14 | |||
| 15 | sub _effective_color { | ||
| 16 | my ($self) = @_; | ||
| 17 | return 0 if defined $self->color && !$self->color; | ||
| 18 | if ( $self->can('command_chain') && ( my $chain = $self->command_chain ) ) { | ||
| 19 | for my $cmd (@$chain) { | ||
| 20 | next if $cmd == $self; | ||
| 21 | return 0 if $cmd->can('color') && defined $cmd->color && !$cmd->color; | ||
| 22 | } | ||
| 23 | } | ||
| 24 | return 1; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub _want_color { | ||
| 28 | my ($self) = @_; | ||
| 29 | return 0 unless $self->_effective_color; | ||
| 30 | return 0 if $ENV{NO_COLOR}; | ||
| 31 | return -t STDOUT ? 1 : 0; | ||
| 32 | } | ||
| 33 | |||
| 34 | 1; | ||
| 35 | |||
| 36 | __END__ |