Skip to content

Commit

Permalink
chore: clean up progress tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
nerg4l committed Jan 21, 2024
1 parent 060c021 commit 9574557
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions src/Console/Commands/FindMissingTranslationStrings.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use CodingSocks\LostInTranslation\LostInTranslation;
use CodingSocks\LostInTranslation\NonStringArgumentException;
use Countable;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
Expand Down Expand Up @@ -57,9 +58,8 @@ public function handle(LostInTranslation $lit)
});

$reported = [];
$keys = [];

$this->showProgress($files, function (SplFileInfo $file, ProgressBar $bar) use ($lit, $locale, &$reported, &$keys, &$barCopy) {
$this->trackProgress($files, function (SplFileInfo $file) use ($lit, $locale, &$reported) {
$nodes = $lit->findInFile($file);

$translationKeys = $this->resolveFirstArgs($lit, $nodes);
Expand All @@ -68,11 +68,12 @@ public function handle(LostInTranslation $lit)
if (!Lang::has($key, $locale) && !array_key_exists($key, $reported)) {
// TODO: find a better way to check uniqueness
$reported[$key] = true;
$keys[] = $key;
}
}
})->clear();

$keys = array_keys($reported);

if ($this->option('sorted')) {
sort($keys);
}
Expand All @@ -85,26 +86,20 @@ public function handle(LostInTranslation $lit)
/**
* Execute a given callback while advancing a progress bar.
*
* @param iterable|int $totalSteps
* @param iterable $totalSteps
* @param \Closure $callback
* @return \Symfony\Component\Console\Helper\ProgressBar
*/
protected function showProgress($totalSteps, Closure $callback)
protected function trackProgress(Countable|array $totalSteps, Closure $callback)
{
$bar = $this->output->createProgressBar(
is_iterable($totalSteps) ? count($totalSteps) : $totalSteps
);
$bar = $this->output->createProgressBar(count($totalSteps));

$bar->start();

if (is_iterable($totalSteps)) {
foreach ($totalSteps as $value) {
$callback($value, $bar);
foreach ($totalSteps as $value) {
$callback($value, $bar);

$bar->advance();
}
} else {
$callback($bar);
$bar->advance();
}

$bar->finish();
Expand Down

0 comments on commit 9574557

Please sign in to comment.