App-Adenosine
view release on metacpan or search on metacpan
lib/App/Adenosine/Plugin/Rainbow.pm view on Meta::CPAN
my ($self, $pre, $post) = @_;
if (my @m = $pre =~ $timestamp_re) {
$pre = $self->filter_timestamp(@m)
}
return $pre .
$self->colorize($self->info_star_color, '* ') .
$post
}
sub filter_response {
my ($self, $pre, $post) = @_;
if (my @m = $pre =~ $timestamp_re) {
$pre = $self->filter_timestamp(@m)
}
if (my @match = $post =~ $header_re ){
$post = $self->filter_header(@match, {
name => $self->response_header_name_color,
colon => $self->response_header_colon_color,
value => $self->response_header_value_color,
})
} elsif (my @match2 = $post =~ $response_re) {
$post = $self->filter_response_init(@match2, {
protocol => $self->response_protocol_color,
protocol_version => $self->response_protocol_version_color,
status_code => $self->response_status_code_color,
status_text => $self->response_status_text_color,
})
}
return $pre .
$self->colorize($self->response_bracket_color, '< ') .
$post
}
sub filter_request {
my ($self, $pre, $post) = @_;
if (my @m = $pre =~ $timestamp_re) {
$pre = $self->filter_timestamp(@m)
}
if (my @match = ( $post =~ $header_re ) ) {
$post = $self->filter_header(@match, {
name => $self->request_header_name_color,
colon => $self->request_header_colon_color,
value => $self->request_header_value_color,
})
} elsif (my @match2 = ( $post =~ $request_re ) ) {
$post = $self->filter_request_init(@match2, {
method => $self->request_method_color,
uri => $self->request_uri_color,
protocol => $self->request_protocol_color,
protocol_version => $self->request_protocol_version_color,
})
}
return $pre .
$self->colorize($self->request_bracket_color, '> ') .
$post
}
sub filter_stderr {
my ($self, $err) = @_;
my @out;
for my $line (map { s/\r$//; $_ } split /\n/, $err) {
if ($line =~ /^(.*)\* (.*)$/) {
$line = $self->filter_info($1, $2)
} elsif ($line =~ /^(.*)< (.*)$/) {
$line = $self->filter_response($1, $2)
} elsif ($line =~ /^(.*)> (.*)$/) {
$line = $self->filter_request($1, $2)
} elsif ($line =~ /^(.*){ (.*)$/) {
$line = $self->filter_response_ellided_body($1, $2)
} elsif ($line =~ /^(.*)} (.*)$/) {
$line = $self->filter_request_ellided_body($1, $2)
}
push @out, $line
}
return join "\n", @out, ''
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
App::Adenosine::Plugin::Rainbow
=head1 VERSION
version 2.002000
=head1 DESCRIPTION
Color codes standard error (diagnostics) from curl. Highly customizable.
=head1 METHODS
=head2 colorize
$p->colorize('red1', 'Christmas') . ' ' . $p->colorize('green1', 'tree!');
C<colorize> is the method used to highlight all the pieces that come from the
curl output. It takes two arguments, first a color specification, and next the
string to be colorized. The complete color specification is defined as:
{
fg => $color,
bg => $color,
bold => $is_bold,
italic => $is_italic,
underline => $is_underline,
}
All of the keys in the hash are optional. The values for $color can be found
at L<Term::ExtendedColor/Standard color map>. Additionally I've added a form
lib/App/Adenosine/Plugin/Rainbow.pm view on Meta::CPAN
bright_cyan
bright_white
As a shortcut, if you pass a simple string instead of a hashref it wil be
explanded to C<< { fg => $str } >>.
Note that unfortunately support for all the attributes are spotty.
For example on my computer I use tmux 1.6 running within terminator 0.96.
In this situation I can't use any of the non-color attributes. Outside of
tmux C<underline> works, but the others do not. Similarly, C<bold> only
seems to work with some colors. It's pretty frustrating, and experimentation
seems necesary.
=head2 Overriding colors at runtime
To change a color when you run C<adenosine> instantiate it as follows:
#!/usr/bin/env perl
use lib 'path/to/adenosine/lib';
use App::Adenosine;
use App::Adenosine::Plugin::Rainbow;
App::Adenosine->new({
argv => \@ARGV,
plugins => [
App::Adenosine::Plugin::Rainbow->new(
response_header_name_color => 'orange4',
response_header_value_color => 'orange2',
response_ellided_body_color => {
fg => 'blue12',
bg => 'blue16',
},
),
],
});
=head2 Creating custom themes
To create a custom theme just subclass C<Rainbow> as follows:
package App::Adennosine::Plugin::Rainbow::Valentine;
use Moo;
extends 'App::Adenosine::Plugin::Rainbow';
has '+response_header_name_color' => ( default => sub { 'magenta1' } );
has '+response_header_value_color' => ( default => sub { 'magenta19' } );
has '+request_header_name_color' => ( default => sub { 'magenta7' } );
has '+request_header_value_color' => ( default => sub { 'magenta25' } );
1;
Then use it the same way you use C<Rainbow>:
...
App::Adenosine->new({ argv => \@ARGV, plugins => ['::Rainbow::Valentine'] })
=head1 COLORABLE SECTIONS
C<Rainbow> splits apart the stderr string from curl and hilights the various
sections respectively. The values of the sections are what is passed as
the first argument to L</colorize>. The names of the sections are:
=over 2
=item * C<response_header_colon_color>
=item * C<response_header_name_color>
=item * C<response_header_value_color>
=item * C<request_header_colon_color>
=item * C<request_header_name_color>
=item * C<request_header_value_color>
=item * C<info_star_color>
=item * C<response_bracket_color>
=item * C<request_bracket_color>
=item * C<request_method_color>
=item * C<request_uri_color>
=item * C<request_protocol_color>
=item * C<request_protocol_version_color>
=item * C<response_protocol_color>
=item * C<response_protocol_version_color>
=item * C<response_status_code_color>
=item * C<response_status_text_color>
=item * C<response_ellided_bracket_color>
=item * C<response_ellided_body_color>
=item * C<request_ellided_bracket_color>
=item * C<request_ellided_body_color>
=back
=head1 AUTHOR
Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2018 by Arthur Axel "fREW" Schmidt.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
( run in 2.849 seconds using v1.01-cache-2.11-cpan-fe3c2283af0 )