unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 5/6] lei_saved_search: split "lei q --save" and "lei up" init paths
Date: Mon, 19 Apr 2021 08:52:14 +0000	[thread overview]
Message-ID: <20210419085215.14742-6-e@80x24.org> (raw)
In-Reply-To: <20210419085215.14742-1-e@80x24.org>

They're more different than alike, and having two separate
methods seems less confusing to me.
---
 lib/PublicInbox/LeiLsSearch.pm    |  2 +-
 lib/PublicInbox/LeiSavedSearch.pm | 79 ++++++++++++++++---------------
 lib/PublicInbox/LeiUp.pm          |  2 +-
 3 files changed, 44 insertions(+), 39 deletions(-)

diff --git a/lib/PublicInbox/LeiLsSearch.pm b/lib/PublicInbox/LeiLsSearch.pm
index 2aa457c0..9ac4870f 100644
--- a/lib/PublicInbox/LeiLsSearch.pm
+++ b/lib/PublicInbox/LeiLsSearch.pm
@@ -29,7 +29,7 @@ sub do_ls_search_long {
 	my @x = sort(grep(/\A\Q$pfx/, PublicInbox::LeiSavedSearch::list($lei)));
 	while (my $x = shift @x) {
 		$ORS = '' if !scalar(@x);
-		my $lss = PublicInbox::LeiSavedSearch->new($lei, $x) or next;
+		my $lss = PublicInbox::LeiSavedSearch->up($lei, $x) or next;
 		my $cfg = $lss->{-cfg};
 		my $ent = {
 			q => $cfg->get_all('lei.q'),
diff --git a/lib/PublicInbox/LeiSavedSearch.pm b/lib/PublicInbox/LeiSavedSearch.pm
index d3a32d36..948e4954 100644
--- a/lib/PublicInbox/LeiSavedSearch.pm
+++ b/lib/PublicInbox/LeiSavedSearch.pm
@@ -65,52 +65,57 @@ sub list {
 	} @$out
 }
 
-sub new {
+sub up { # updating existing saved search via "lei up"
 	my ($cls, $lei, $dst) = @_;
+	my $f;
 	my $self = bless { ale => $lei->ale }, $cls;
-	my $dir;
-	if (defined $dst) { # updating existing saved search via "lei up"
-		my $f;
-		$dir = $dst;
-		output2lssdir($self, $lei, \$dir, \$f) or
-			return $lei->fail("--save was not used with $dst cwd=".
-						$lei->rel2abs('.'));
-		$self->{'-f'} = $f;
-	} else { # new saved search "lei q --save"
-		$dst = $lei->{ovv}->{dst};
-		$dir = lss_dir_for($lei, \$dst);
-		require File::Path;
-		File::Path::make_path($dir); # raises on error
-		$self->{-cfg} = {};
-		my $f = $self->{'-f'} = "$dir/lei.saved-search";
-		open my $fh, '>', $f or return $lei->fail("open $f: $!");
-		my $sq_dst = PublicInbox::Config::squote_maybe($dst);
-		my $q = $lei->{mset_opt}->{q_raw} // die 'BUG: {q_raw} missing';
-		if (ref $q) {
-			$q = join("\n", map { "\tq = ".cquote_val($_) } @$q);
-		} else {
-			$q = "\tq = ".cquote_val($q);
-		}
-		$dst = "$lei->{ovv}->{fmt}:$dst" if $dst !~ m!\Aimaps?://!i;
-		print $fh <<EOM;
+	my $dir = $dst;
+	output2lssdir($self, $lei, \$dir, \$f) or
+		return $lei->fail("--save was not used with $dst cwd=".
+					$lei->rel2abs('.'));
+	$self->{-cfg} = PublicInbox::Config->git_config_dump($f);
+	$self->{-ovf} = "$dir/over.sqlite3";
+	$self->{'-f'} = $f;
+	$self->{lock_path} = "$self->{-f}.flock";
+	$self;
+}
+
+sub new { # new saved search "lei q --save"
+	my ($cls, $lei) = @_;
+	my $self = bless { ale => $lei->ale }, $cls;
+	my $dst = $lei->{ovv}->{dst};
+	my $dir = lss_dir_for($lei, \$dst);
+	require File::Path;
+	File::Path::make_path($dir); # raises on error
+	$self->{-cfg} = {};
+	my $f = $self->{'-f'} = "$dir/lei.saved-search";
+	open my $fh, '>', $f or return $lei->fail("open $f: $!");
+	my $sq_dst = PublicInbox::Config::squote_maybe($dst);
+	my $q = $lei->{mset_opt}->{q_raw} // die 'BUG: {q_raw} missing';
+	if (ref $q) {
+		$q = join("\n", map { "\tq = ".cquote_val($_) } @$q);
+	} else {
+		$q = "\tq = ".cquote_val($q);
+	}
+	$dst = "$lei->{ovv}->{fmt}:$dst" if $dst !~ m!\Aimaps?://!i;
+	print $fh <<EOM;
 ; to refresh with new results, run: lei up $sq_dst
 [lei]
-$q
+	$q
 [lei "q"]
 	output = $dst
 EOM
-		for my $k (ARRAY_FIELDS) {
-			my $ary = $lei->{opt}->{$k} // next;
-			for my $x (@$ary) {
-				print $fh "\t$k = ".cquote_val($x)."\n";
-			}
-		}
-		for my $k (BOOL_FIELDS) {
-			my $val = $lei->{opt}->{$k} // next;
-			print $fh "\t$k = ".($val ? 1 : 0)."\n";
+	for my $k (ARRAY_FIELDS) {
+		my $ary = $lei->{opt}->{$k} // next;
+		for my $x (@$ary) {
+			print $fh "\t$k = ".cquote_val($x)."\n";
 		}
-		close($fh) or return $lei->fail("close $f: $!");
 	}
+	for my $k (BOOL_FIELDS) {
+		my $val = $lei->{opt}->{$k} // next;
+		print $fh "\t$k = ".($val ? 1 : 0)."\n";
+	}
+	close($fh) or return $lei->fail("close $f: $!");
 	$self->{lock_path} = "$self->{-f}.flock";
 	$self->{-ovf} = "$dir/over.sqlite3";
 	$self;
diff --git a/lib/PublicInbox/LeiUp.pm b/lib/PublicInbox/LeiUp.pm
index e4cbc825..23c5c606 100644
--- a/lib/PublicInbox/LeiUp.pm
+++ b/lib/PublicInbox/LeiUp.pm
@@ -11,7 +11,7 @@ use PublicInbox::LeiOverview;
 sub lei_up {
 	my ($lei, $out) = @_;
 	$lei->{lse} = $lei->_lei_store(1)->search;
-	my $lss = PublicInbox::LeiSavedSearch->new($lei, $out) or return;
+	my $lss = PublicInbox::LeiSavedSearch->up($lei, $out) or return;
 	my $mset_opt = $lei->{mset_opt} = { relevance => -2 };
 	$mset_opt->{limit} = $lei->{opt}->{limit} // 10000;
 	my $q = $mset_opt->{q_raw} = $lss->{-cfg}->{'lei.q'} //

  parent reply	other threads:[~2021-04-19  8:52 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-19  8:52 [PATCH 0/6] lei saved search improvements Eric Wong
2021-04-19  8:52 ` [PATCH 1/6] lei: support unlinked/missing saved searches Eric Wong
2021-04-19  8:52 ` [PATCH 2/6] lei q: implement import-before default for --save Eric Wong
2021-04-19  8:52 ` [PATCH 3/6] lei_saved_search: avoid needless var shadowing Eric Wong
2021-04-19  8:52 ` [PATCH 4/6] config: git_config_dump blesses Eric Wong
2021-04-19  8:52 ` Eric Wong [this message]
2021-04-19  8:52 ` [PATCH 6/6] lei q: --save and --augment may be combined Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210419085215.14742-6-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).