| 1 | package IPC::PrettyPipe::Render::Template::Tiny; | ||||||||||
| 2 | |||||||||||
| 3 | # ABSTRACT: rendering backend using B<Template::Tiny> | ||||||||||
| 4 | |||||||||||
| 5 | 5 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 5 | ▶ | use Template::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 5 | ▶ | use Safe::Isa; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 5 | ▶ | use Text::Tabs qw[ expand ]; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 5 | ▶ | use Types::Standard -all; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 5 | ▶ | use Type::Params qw[ validate ]; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 5 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | |||||||||||
| 16 | 5 | our $VERSION = '0.13'; | |||||||||
| 17 | |||||||||||
| 18 | 5 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=9.2
| |||||||||||
| 19 | 5 | ▶ | if ( $^O =~ /Win32/i ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | 0 | require Win32::Console::ANSI; | |||||||||
| 21 | } | ||||||||||
| 22 | } | ||||||||||
| 23 | 5 | ▶ | use Term::ANSIColor (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | |||||||||||
| 25 | 5 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | |||||||||||
| 27 | |||||||||||
| 28 | |||||||||||
| 29 | |||||||||||
| 30 | |||||||||||
| 31 | |||||||||||
| 32 | |||||||||||
| 33 | |||||||||||
| 34 | |||||||||||
| 35 | |||||||||||
| 36 | |||||||||||
| 37 | |||||||||||
| 38 | |||||||||||
| 39 | |||||||||||
| 40 | |||||||||||
| 41 | |||||||||||
| 42 | |||||||||||
| 43 | |||||||||||
| 44 | |||||||||||
| 45 | |||||||||||
| 46 | |||||||||||
| 47 | |||||||||||
| 48 | |||||||||||
| 49 | |||||||||||
| 50 | |||||||||||
| 51 | has colors => ( | ||||||||||
| 52 | is => 'rw', | ||||||||||
| 53 | isa => HashRef, | ||||||||||
| 54 | 8 | ▶ | default => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 55 | { | ||||||||||
| 56 | 8 | cmd => { | |||||||||
| 57 | cmd => 'blue', | ||||||||||
| 58 | stream => { | ||||||||||
| 59 | spec => 'red', | ||||||||||
| 60 | file => 'green', | ||||||||||
| 61 | }, | ||||||||||
| 62 | arg => { | ||||||||||
| 63 | name => 'red', | ||||||||||
| 64 | sep => 'yellow', | ||||||||||
| 65 | value => 'green', | ||||||||||
| 66 | }, | ||||||||||
| 67 | }, | ||||||||||
| 68 | pipe => { | ||||||||||
| 69 | stream => { | ||||||||||
| 70 | spec => 'red', | ||||||||||
| 71 | file => 'green', | ||||||||||
| 72 | }, | ||||||||||
| 73 | }, | ||||||||||
| 74 | }; | ||||||||||
| 75 | }, | ||||||||||
| 76 | 5 | ); | |||||||||
| 77 | |||||||||||
| 78 | |||||||||||
| 79 | |||||||||||
| 80 | |||||||||||
| 81 | |||||||||||
| 82 | |||||||||||
| 83 | |||||||||||
| 84 | |||||||||||
| 85 | |||||||||||
| 86 | |||||||||||
| 87 | |||||||||||
| 88 | |||||||||||
| 89 | |||||||||||
| 90 | |||||||||||
| 91 | |||||||||||
| 92 | 5 | has colorize => ( | |||||||||
| 93 | is => 'rw', | ||||||||||
| 94 | isa => Bool, | ||||||||||
| 95 | default => 1, | ||||||||||
| 96 | ); | ||||||||||
| 97 | |||||||||||
| 98 | |||||||||||
| 99 | |||||||||||
| 100 | |||||||||||
| 101 | |||||||||||
| 102 | |||||||||||
| 103 | |||||||||||
| 104 | |||||||||||
| 105 | |||||||||||
| 106 | |||||||||||
| 107 | |||||||||||
| 108 | |||||||||||
| 109 | |||||||||||
| 110 | |||||||||||
| 111 | |||||||||||
| 112 | |||||||||||
| 113 | |||||||||||
| 114 | |||||||||||
| 115 | |||||||||||
| 116 | 5 | has cmd_template => ( | |||||||||
| 117 | is => 'rw', | ||||||||||
| 118 | default => <<"EOT", | ||||||||||
| 119 | [%- indent %][% color.cmd.cmd %][% cmd.quoted_cmd %][% color.reset %] | ||||||||||
| 120 | [%- FOREACH arg IN cmd.args.elements %]\\ | ||||||||||
| 121 | [% indent %][% indent %][% color.cmd.arg.pfx %][% arg.pfx %][% color.cmd.arg.name %][% arg.quoted_name %] | ||||||||||
| 122 | [%- IF arg.has_value %][%- IF arg.sep %][% color.cmd.arg.sep %][% arg.sep %][% ELSE %] [% END %] | ||||||||||
| 123 | [%- color.cmd.arg.value %][% arg.quoted_value %][% END %][% color.reset %] | ||||||||||
| 124 | [%- END %] | ||||||||||
| 125 | [%- FOREACH stream IN cmd.streams.elements %]\\ | ||||||||||
| 126 | [% indent %][% indent %][% color.cmd.stream.spec %][% stream.spec -%] | ||||||||||
| 127 | [% IF stream.has_file %] [% color.cmd.stream.file %][% stream.quoted_file %][% color.reset %][% END %] | ||||||||||
| 128 | [%- END -%] | ||||||||||
| 129 | EOT | ||||||||||
| 130 | ); | ||||||||||
| 131 | |||||||||||
| 132 | |||||||||||
| 133 | |||||||||||
| 134 | |||||||||||
| 135 | |||||||||||
| 136 | |||||||||||
| 137 | |||||||||||
| 138 | |||||||||||
| 139 | |||||||||||
| 140 | |||||||||||
| 141 | |||||||||||
| 142 | |||||||||||
| 143 | |||||||||||
| 144 | |||||||||||
| 145 | |||||||||||
| 146 | |||||||||||
| 147 | |||||||||||
| 148 | |||||||||||
| 149 | 5 | has pipe_template => ( | |||||||||
| 150 | |||||||||||
| 151 | is => 'rw', | ||||||||||
| 152 | |||||||||||
| 153 | default => <<"EOT", | ||||||||||
| 154 | [% indent %][% IF pipe.streams.empty %][% ELSE %](\\ | ||||||||||
| 155 | [% END %][% cmds %] | ||||||||||
| 156 | [%- IF pipe.streams.empty %][% ELSE %]\\ | ||||||||||
| 157 | [% indent %])[% FOREACH stream IN pipe.streams.elements -%] | ||||||||||
| 158 | [% IF stream.first %]\t[% ELSE %]\\ | ||||||||||
| 159 | \t[% indent %][% END -%] | ||||||||||
| 160 | [% color.pipe.stream.spec %][% stream.spec -%] | ||||||||||
| 161 | [% IF stream.has_file %] [% color.pipe.stream.file %][% stream.quoted_file %][% color.reset %][% END %] | ||||||||||
| 162 | [%- END -%] | ||||||||||
| 163 | [%- END -%] | ||||||||||
| 164 | EOT | ||||||||||
| 165 | ); | ||||||||||
| 166 | |||||||||||
| 167 | 126 | ▶ | sub _colorize { | ||||||||
|
Subroutine
_colorize: called CC=3 SCAR=11.0
| |||||||||||
| 168 | |||||||||||
| 169 | 126 | my ( $tmpl, $colors ) = @_; | |||||||||
| 170 | |||||||||||
| 171 | ## no critic (ProhibitAccessOfPrivateData) | ||||||||||
| 172 | |||||||||||
| 173 | 126 | ▶ | while ( my ( $node, $value ) = each %$tmpl ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 174 | |||||||||||
| 175 | 273 | ▶ | if ( ref $value ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 176 | |||||||||||
| 177 | 105 | $colors->{$node} = {}; | |||||||||
| 178 | 105 | _colorize( $value, $colors->{$node} ); | |||||||||
| 179 | |||||||||||
| 180 | } | ||||||||||
| 181 | |||||||||||
| 182 | else { | ||||||||||
| 183 | 168 | $colors->{$node} = Term::ANSIColor::color( $value ); | |||||||||
| 184 | } | ||||||||||
| 185 | |||||||||||
| 186 | } | ||||||||||
| 187 | |||||||||||
| 188 | } | ||||||||||
| 189 | |||||||||||
| 190 | |||||||||||
| 191 | |||||||||||
| 192 | |||||||||||
| 193 | |||||||||||
| 194 | |||||||||||
| 195 | |||||||||||
| 196 | |||||||||||
| 197 | |||||||||||
| 198 | |||||||||||
| 199 | |||||||||||
| 200 | |||||||||||
| 201 | |||||||||||
| 202 | |||||||||||
| 203 | |||||||||||
| 204 | |||||||||||
| 205 | |||||||||||
| 206 | |||||||||||
| 207 | |||||||||||
| 208 | 21 | ▶ | sub render { | ||||||||
|
Subroutine
render: called CC=5 SCAR=16.9
Pod
documented
| |||||||||||
| 209 | |||||||||||
| 210 | 21 | my $self = shift; | |||||||||
| 211 | |||||||||||
| 212 | 21 | my ( $pipe, $args ) = validate( | |||||||||
| 213 | \@_, | ||||||||||
| 214 | InstanceOf[ 'IPC::PrettyPipe' ], | ||||||||||
| 215 | slurpy Dict [ | ||||||||||
| 216 | colorize => Optional [Bool], | ||||||||||
| 217 | ] ); | ||||||||||
| 218 | |||||||||||
| 219 | $args->{colorize} = $self->colorize ## no critic (ProhibitAccessOfPrivateData) | ||||||||||
| 220 | 21 | ▶ | unless exists $args->{colorize}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 221 | |||||||||||
| 222 | 21 | my %color; | |||||||||
| 223 | 21 | _colorize( $self->colors, \%color ); | |||||||||
| 224 | |||||||||||
| 225 | 21 | ▶ | $color{reset} = Term::ANSIColor::color( 'reset' ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 226 | if keys %color; | ||||||||||
| 227 | |||||||||||
| 228 | 21 | local $Text::Tabs::tabstop = 2; | |||||||||
| 229 | |||||||||||
| 230 | # generate non-colorized version so can get length of records to | ||||||||||
| 231 | # pad out any continuation lines | ||||||||||
| 232 | 21 | my @output; | |||||||||
| 233 | 21 | $self->_render_pipe( $pipe, { indent => '' }, \@output ); | |||||||||
| 234 | |||||||||||
| 235 | 21 | my @records = map { expand( $_ ) } map { split( /\n/, $_ ) } @output; | |||||||||
| 236 | 21 | my @lengths = map { length } @records; | |||||||||
| 237 | 21 | my $max = List::Util::max( @lengths ) + 4; | |||||||||
| 238 | |||||||||||
| 239 | 21 | ▶ | if ( $args->{colorize} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 240 | 0 | @output = (); | |||||||||
| 241 | 0 | $self->_render_pipe( $pipe, { indent => '', color => \%color }, | |||||||||
| 242 | \@output ); | ||||||||||
| 243 | 0 | @records = map { expand( $_ ) } map { split( /\n/, $_ ) } @output; | |||||||||
| 244 | } | ||||||||||
| 245 | |||||||||||
| 246 | 21 | foreach ( @records ) { | |||||||||
| 247 | 85 | my $pad = ' ' x ( $max - shift @lengths ); | |||||||||
| 248 | 85 | s/\\$/$pad \\/; | |||||||||
| 249 | } | ||||||||||
| 250 | |||||||||||
| 251 | 21 | return join( "\n", @records ) . "\n"; | |||||||||
| 252 | } | ||||||||||
| 253 | |||||||||||
| 254 | 23 | ▶ | sub _render_pipe { | ||||||||
|
Subroutine
_render_pipe: called CC=3 SCAR=11.0
| |||||||||||
| 255 | |||||||||||
| 256 | 23 | my $self = shift; | |||||||||
| 257 | |||||||||||
| 258 | 23 | my ( $pipe, $process_args, $output ) = @_; | |||||||||
| 259 | |||||||||||
| 260 | 23 | my %process_args = %{$process_args}; | |||||||||
| 261 | |||||||||||
| 262 | 23 | my @output; | |||||||||
| 263 | |||||||||||
| 264 | 23 | for my $cmd ( @{ $pipe->cmds->elements } ) { | |||||||||
| 265 | 32 | ▶ | if ( $cmd->isa( 'IPC::PrettyPipe' ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 266 | 2 | local $process_args{indent} = $process_args{indent} . "\t"; | |||||||||
| 267 | 2 | $self->_render_pipe( $cmd, \%process_args, \@output ); | |||||||||
| 268 | } | ||||||||||
| 269 | else { | ||||||||||
| 270 | 30 | local $process_args{indent} = $process_args{indent} . "\t"; | |||||||||
| 271 | 30 | push @output, ''; | |||||||||
| 272 | 30 | local $process_args{cmd} = $cmd; | |||||||||
| 273 | 30 | Template::Tiny->new->process( \$self->cmd_template, \%process_args, | |||||||||
| 274 | \( $output[-1] ) ); | ||||||||||
| 275 | } | ||||||||||
| 276 | } | ||||||||||
| 277 | |||||||||||
| 278 | 23 | $process_args{cmds} = join( "\\\n|", @output ); | |||||||||
| 279 | 23 | $process_args{pipe} = $pipe; | |||||||||
| 280 | 23 | push @{$output}, ''; | |||||||||
| 281 | 23 | Template::Tiny->new->process( \$self->pipe_template, \%process_args, | |||||||||
| 282 | \( $output->[-1] ) ); | ||||||||||
| 283 | 23 | return; | |||||||||
| 284 | } | ||||||||||
| 285 | |||||||||||
| 286 | |||||||||||
| 287 | 5 | with 'IPC::PrettyPipe::Renderer'; | |||||||||
| 288 | |||||||||||
| 289 | 5 | 1; | |||||||||
| 290 | |||||||||||
| 291 | # | ||||||||||
| 292 | # This file is part of IPC-PrettyPipe | ||||||||||
| 293 | # | ||||||||||
| 294 | # This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory. | ||||||||||
| 295 | # | ||||||||||
| 296 | # This is free software, licensed under: | ||||||||||
| 297 | # | ||||||||||
| 298 | # The GNU General Public License, Version 3, June 2007 | ||||||||||
| 299 | # | ||||||||||
| 300 | |||||||||||
| 301 | __END__ | ||||||||||