App-LJ
view release on metacpan or search on metacpan
lib/App/LJ.pm view on Meta::CPAN
package App::LJ;
use 5.008001;
use strict;
use warnings;
our $VERSION = "0.02";
use JSON::XS ();
use JSON::Color ();
my $_coder;
sub _coder {
$_coder ||= JSON::XS->new->pretty(1);
}
sub new_with_options {
my ($class, @argv) = @_;
my ($opt) = $class->parse_options(@argv);
$class->new($opt);
}
sub parse_options {
my ($class, @argv) = @_;
if (grep /^--?h(?:elp)?$/, @argv) {
_print_usage();
}
my $opt = {};
my @rest;
for my $v (@argv) {
if ($v eq '--no-color') {
$opt->{color} = undef;
next;
}
push @rest, $v;
}
($opt, \@rest)
}
sub new {
my $class = shift;
my $opt = @_ == 1 ? $_[0] : {@_};
unless (exists $opt->{color}) {
$opt->{color} = 1;
}
bless $opt, $class;
}
sub _pretty_print {
my ($self, $json) = @_;
($self->{printer} ||= do {
!$self->{color} ? sub { chomp(my $l = $_coder->encode(shift)); $l} : sub { JSON::Color::encode_json(shift, {pretty => 1}) }
})->($json);
}
sub run {
my $self = shift;
print $self->_process_line($_) . "\n" while <STDIN>;
}
sub _process_line {
my ($self, $line) = @_;
chomp $line;
if ($line =~ /\s*\[?\{.*\}\]?\s*/) {
my $pre = $`;
my $maybe_json = $&;
my $post = $';
my $json;
( run in 1.405 second using v1.01-cache-2.11-cpan-39bf76dae61 )