| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::yajg::Output; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 355 | use 5.014000; | 
|  | 1 |  |  |  |  | 2 |  | 
| 4 | 1 |  |  | 1 |  | 3 | use strict; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 14 |  | 
| 5 | 1 |  |  | 1 |  | 3 | use warnings; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 16 |  | 
| 6 | 1 |  |  | 1 |  | 3 | use utf8; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 3 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 1 |  |  | 1 |  | 377 | use App::yajg; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 38 |  | 
| 9 | 1 |  |  | 1 |  | 19 | use Data::Dumper qw(); | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 222 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | # Class methods | 
| 12 | 2 |  |  | 2 | 0 | 648 | sub new { bless {}, shift } | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | { | 
| 15 |  |  |  |  |  |  | my $can_hl = eval { | 
| 16 |  |  |  |  |  |  | # FIXME: probably will not work if not unix os | 
| 17 |  |  |  |  |  |  | qx(which highlight 2>/dev/null) and $? == 0 | 
| 18 |  |  |  |  |  |  | }; | 
| 19 | 0 |  |  | 0 | 0 | 0 | sub can_highlight {$can_hl} | 
| 20 |  |  |  |  |  |  | } | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 0 |  |  | 0 | 0 | 0 | sub lang              {...}    # lang for highlight | 
| 23 | 0 |  |  | 0 | 0 | 0 | sub need_change_depth {1}      # need to change max depth via Data::Dumper | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | sub highlight { | 
| 26 | 0 |  |  | 0 | 0 | 0 | my ($class, $string) = @_; | 
| 27 | 0 | 0 |  |  |  | 0 | $class = ref $class if ref $class; | 
| 28 |  |  |  |  |  |  |  | 
| 29 | 0 | 0 | 0 |  |  | 0 | return $string unless $class->can_highlight and length $string; | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | # IPC::Open2 hangs on big data so we will do like this | 
| 32 | 0 |  |  |  |  | 0 | my $pid = open(my $hl_out, '-|'); | 
| 33 | 0 | 0 |  |  |  | 0 | if (not defined $pid) { | 
| 34 | 0 |  |  |  |  | 0 | warn "highlight failed: $!\n"; | 
| 35 | 0 |  |  |  |  | 0 | return $string; | 
| 36 |  |  |  |  |  |  | } | 
| 37 | 0 |  |  |  |  | 0 | my $utf8 = utf8::is_utf8($string); | 
| 38 |  |  |  |  |  |  |  | 
| 39 | 0 | 0 |  |  |  | 0 | unless ($pid) { | 
| 40 | 0 | 0 |  |  |  | 0 | open(my $hl_in, '|-', 'highlight', '-O', 'ansi', '-S', $class->lang) | 
| 41 |  |  |  |  |  |  | or die "$!\n"; | 
| 42 | 0 | 0 |  |  |  | 0 | utf8::encode($string) if utf8::is_utf8($string); | 
| 43 | 0 |  |  |  |  | 0 | print $hl_in $string; | 
| 44 | 0 |  |  |  |  | 0 | close $hl_in; | 
| 45 | 0 |  |  |  |  | 0 | exit 0; | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 0 |  |  |  |  | 0 | local $/; | 
| 49 | 0 |  |  |  |  | 0 | my $highlighted = <$hl_out>; | 
| 50 | 0 |  |  |  |  | 0 | close $hl_out;    # may be waitpid($pid, 0); ?? | 
| 51 | 0 | 0 |  |  |  | 0 | return $string unless $? == 0; | 
| 52 | 0 | 0 | 0 |  |  | 0 | utf8::decode($highlighted) if $utf8 and not utf8::is_utf8($highlighted); | 
| 53 |  |  |  |  |  |  |  | 
| 54 | 0 |  | 0 |  |  | 0 | return $highlighted || $string; | 
| 55 |  |  |  |  |  |  | } | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | # Object methods | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | # Getters/Setters | 
| 60 |  |  |  |  |  |  | for my $method (qw(data color minimal max_depth)) { | 
| 61 | 1 |  |  | 1 |  | 4 | no strict 'refs'; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 242 |  | 
| 62 |  |  |  |  |  |  | *{ __PACKAGE__ . "::$method" } = sub { | 
| 63 | 31 |  |  | 31 |  | 3934 | my $self = shift; | 
| 64 | 31 | 100 |  |  |  | 148 | return $self->{ '_' . $method } unless @_; | 
| 65 | 12 |  |  |  |  | 22 | $self->{ '_' . $method } = shift; | 
| 66 | 12 |  |  |  |  | 25 | return $self; | 
| 67 |  |  |  |  |  |  | }; | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | sub change_depth { | 
| 71 | 1 |  |  | 1 | 0 | 6 | my $self  = shift; | 
| 72 | 1 |  |  |  |  | 1 | my $class = ref $self; | 
| 73 | 1 | 50 | 33 |  |  | 4 | return $self unless $class->need_change_depth and $self->max_depth; | 
| 74 | 1 |  |  |  |  | 5 | local $SIG{__WARN__} = \&App::yajg::warn_without_line; | 
| 75 | 1 |  |  |  |  | 2 | my $VAR1; | 
| 76 | 1 |  |  |  |  | 2 | eval Data::Dumper->new([$self->data])->Maxdepth($self->max_depth)->Dump(); | 
| 77 | 1 | 50 | 33 |  |  | 14 | if ($@ or not defined $VAR1) { | 
| 78 | 0 |  |  |  |  | 0 | warn "max_depth failed: $@"; | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  | else { | 
| 81 | 1 |  |  |  |  | 3 | $self->data($VAR1); | 
| 82 |  |  |  |  |  |  | } | 
| 83 | 1 |  |  |  |  | 5 | return $self; | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | sub as_string { | 
| 87 |  |  |  |  |  |  | ... | 
| 88 | 0 |  |  | 0 | 0 |  | } | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | sub print { | 
| 91 | 0 |  |  | 0 | 0 |  | my $self  = shift; | 
| 92 | 0 |  |  |  |  |  | my $class = ref $self; | 
| 93 | 0 |  |  |  |  |  | my $out   = $self->change_depth->as_string; | 
| 94 | 0 | 0 | 0 |  |  |  | $out = $class->highlight($out) if $self->color and $class->can_highlight; | 
| 95 | 0 | 0 |  |  |  |  | utf8::encode($out) if utf8::is_utf8($out); | 
| 96 | 0 |  |  |  |  |  | print $out; | 
| 97 | 0 |  |  |  |  |  | return $self; | 
| 98 |  |  |  |  |  |  | } | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | 1; |