From 29116801dee43956c4283c765089dc978293cc2f Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Fri, 5 Mar 2021 23:50:53 -0700 Subject: [PATCH] show-pack-counts.pl: switch to Girocco::CLIUtil::Progress class Eliminate code duplication and use the new Progress class. The output is very similar, but with the actual current progress count shown. Signed-off-by: Kyle J. McKay --- toolbox/show-pack-counts.pl | 54 +++++++++++---------------------------------- 1 file changed, 13 insertions(+), 41 deletions(-) diff --git a/toolbox/show-pack-counts.pl b/toolbox/show-pack-counts.pl index 6f1f5bd..d368e57 100755 --- a/toolbox/show-pack-counts.pl +++ b/toolbox/show-pack-counts.pl @@ -56,37 +56,9 @@ sub defval { return defined($_[0]) ? $_[0] : $_[1]; } -my ($lastpct, $lastts); - -sub progress { - return unless $progress; - my ($prog, $total, $prefix) = @_; - $total or $total = 100; - defined $prefix or $prefix = ""; - my $now = time(); - defined $lastts or $lastts = $now + 1; - if ($now > $lastts) { - $lastts = $now; - defined $lastpct or $lastpct = ""; - my $pct = int($prog * 100 / $total); - $pct = 100 if $pct > 100; - if ($pct ne $lastpct) { - $lastpct = $pct; - $prefix ne "" and $prefix .= " "; - printf STDERR "\r%s%3d%%", $prefix, $pct; - } - } -} +my ($packs, $packobjs, $loose, $sorton, $progobj); -sub progress_done { - return unless $progress && defined($lastpct); - my $prefix = $_[0]; - defined $prefix or $prefix = ""; - $prefix ne "" and $prefix .= " "; - printf STDERR "\r%s100%%, done.\n", $prefix; -} - -my ($packs, $packobjs, $loose, $sorton); +sub pwarn { $progobj->warn(@_) } sub main { local *ARGV = \@_; @@ -156,23 +128,23 @@ sub main { } @projlist or exit 1; } else { - @projlist = sort({lc($a) cmp lc($b)} Girocco::Project::get_full_list); + @projlist = sort({lc($a) cmp lc($b) || $a cmp $b} Girocco::Project::get_full_list); } my @results = (); - my $pmsg = "Inspecting projects:"; + $progobj = Girocco::CLIUtil::Progress->new(($progress && $sorton ? scalar(@projlist) : 0), + "Inspecting projects"); foreach my $proj (@projlist) { my ($pcks, $pobjs, $lobjs) = inspect_proj("$reporoot/$proj.git", $packs, $packobjs, $loose); if ($sorton) { push(@results, [$proj, $pcks, $pobjs, $lobjs]); - progress(scalar(@results), scalar(@projlist), $pmsg); } else { show_one($proj, $pcks, $pobjs, $lobjs); } - } + } continue {$progobj->update} + $progobj = undef; if ($sorton) { - progress_done($pmsg); my $sorter; my $desc = $asc ? 1 : -1; $sorton eq "packs" and $sorter = sub { @@ -249,14 +221,14 @@ sub count_proj_packs { my $projdir = shift; my $objs = shift; my $pd = $projdir . "/objects/pack"; - -d $pd or warn("no such project path (anymore) $pd\n"), return undef; + -d $pd or pwarn("no such project path (anymore) $pd\n"), return undef; open LPCMD, '-|', @lpcmd, ($objs ? "--count-objects" : "--count"), $pd or - warn("failed to run list_packs for project dir $pd\n"), return undef; + pwarn("failed to run list_packs for project dir $pd\n"), return undef; my $pcount = ; chomp($pcount); close LPCMD; $pcount =~ /^\d+$/ or - warn("list_packs produced invalid output for project dir $pd\n"), return undef; + pwarn("list_packs produced invalid output for project dir $pd\n"), return undef; return 0 + $pcount; } @@ -269,15 +241,15 @@ BEGIN { sub count_proj_loose { my $projdir = shift; my $od = $projdir . "/objects"; - -d $od or warn("no such project path (anymore) $od\n"), return undef; + -d $od or pwarn("no such project path (anymore) $od\n"), return undef; open FINDCMD, '-|', $shbin, '-c', "cd " . quotemeta($od) . " && " . "find -L $octet -maxdepth 1 -name '$octet19*' -print 2>/dev/null | wc -l" or - warn("failed to run find for project dir $od\n"), return undef; + pwarn("failed to run find for project dir $od\n"), return undef; my $ocount = ; $ocount =~ s/\s+//gs; close FINDCMD; $ocount =~ /^\d+$/ or - warn("find + wc produced invalid output for project dir $od\n"), return undef; + pwarn("find + wc produced invalid output for project dir $od\n"), return undef; return 0 + $ocount; } -- 2.11.4.GIT