From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 8C2E61FC0D for ; Sun, 19 Sep 2021 12:50:36 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 09/16] lei ls-mail-source: pretty JSON support Date: Sun, 19 Sep 2021 12:50:28 +0000 Message-Id: <20210919125035.6331-10-e@80x24.org> In-Reply-To: <20210919125035.6331-1-e@80x24.org> References: <20210919125035.6331-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: As with other commands, we enable pretty JSON by default if stdout is a terminal or if --pretty is specified. While the ->pretty JSON output has excessive vertical whitespace, too many lines is preferable to having everything on one line. --- lib/PublicInbox/LEI.pm | 2 +- lib/PublicInbox/LeiLsMailSource.pm | 19 ++++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm index def85ef1..b468a32c 100644 --- a/lib/PublicInbox/LEI.pm +++ b/lib/PublicInbox/LEI.pm @@ -214,7 +214,7 @@ our %CMD = ( # sorted in order of importance/use: 'ls-mail-sync' => [ '[FILTER]', 'list mail sync folders', qw(z|0 globoff|g invert-match|v local remote), @c_opt ], 'ls-mail-source' => [ 'URL', 'list IMAP or NNTP mail source folders', - qw(z|0 ascii l url), @c_opt ], + qw(z|0 ascii l pretty url), @c_opt ], 'forget-external' => [ 'LOCATION...|--prune', 'exclude further results from a publicinbox|extindex', qw(prune), @c_opt ], diff --git a/lib/PublicInbox/LeiLsMailSource.pm b/lib/PublicInbox/LeiLsMailSource.pm index a2e75e94..2265969a 100644 --- a/lib/PublicInbox/LeiLsMailSource.pm +++ b/lib/PublicInbox/LeiLsMailSource.pm @@ -12,15 +12,9 @@ use parent qw(PublicInbox::IPC PublicInbox::LeiInput); sub input_path_url { # overrides LeiInput version my ($self, $url) = @_; # TODO: support ndjson and other JSONs we support elsewhere - my $json; my $lei = $self->{lei}; - my $ORS = "\n"; - if ($self->{lei}->{opt}->{l}) { - $json = ref(PublicInbox::Config->json)->new->utf8->canonical; - $json->ascii(1) if $lei->{opt}->{ascii}; - } elsif ($self->{lei}->{opt}->{z}) { - $ORS = "\0"; - } + my $json = $lei->{json}; + my $ORS = $self->{lei}->{opt}->{z} ? "\0" : "\n"; my @f; if ($url =~ m!\Aimaps?://!i) { my $uri = PublicInbox::URIimap->new($url); @@ -93,7 +87,14 @@ sub lei_ls_mail_source { my $self = bless { pfx => $pfx, -ls_ok => 1 }, __PACKAGE__; $self->{cfg} = $lei->_lei_cfg; # may be undef $self->prepare_inputs($lei, [ $url ]) or return; - $lei->start_pager if -t $lei->{1}; + my $isatty = -t $lei->{1}; + if ($lei->{opt}->{l}) { + my $json = ref(PublicInbox::Config->json)->new->utf8->canonical; + $lei->{json} = $json; + $json->ascii(1) if $lei->{opt}->{ascii}; + $json->pretty(1)->indent(2) if $isatty || $lei->{opt}->{pretty}; + } + $lei->start_pager if $isatty; my $ops = {}; $lei->{auth}->op_merge($ops, $self); (my $op_c, $ops) = $lei->workers_start($self, 1, $ops);