Skip to content

Commit

Permalink
feat: remove progress bar after completion
Browse files Browse the repository at this point in the history
  • Loading branch information
nerg4l committed Jan 21, 2024
1 parent c2c9034 commit e4ebbcb
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions src/Console/Commands/FindMissingTranslationStrings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CodingSocks\LostInTranslation\Console\Commands;

use Closure;
use CodingSocks\LostInTranslation\LostInTranslation;
use CodingSocks\LostInTranslation\NonStringArgumentException;
use Illuminate\Console\Command;
Expand All @@ -10,7 +11,9 @@
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Str;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\SplFileInfo;

class FindMissingTranslationStrings extends Command
{
Expand Down Expand Up @@ -49,7 +52,7 @@ public function handle(LostInTranslation $lit)
$reported = [];
$keys = [];

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

$translationKeys = $this->resolveFirstArgs($lit, $nodes);
Expand All @@ -61,9 +64,7 @@ public function handle(LostInTranslation $lit)
$keys[] = $key;
}
}
});

$this->newLine();
})->clear();

if ($this->option('sorted')) {
sort($keys);
Expand All @@ -74,6 +75,36 @@ public function handle(LostInTranslation $lit)
}
}

/**
* Execute a given callback while advancing a progress bar.
*
* @param iterable|int $totalSteps
* @param \Closure $callback
* @return \Symfony\Component\Console\Helper\ProgressBar
*/
protected function showProgress($totalSteps, Closure $callback)
{
$bar = $this->output->createProgressBar(
is_iterable($totalSteps) ? count($totalSteps) : $totalSteps
);

$bar->start();

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

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

$bar->finish();

return $bar;
}

/**
* @param \CodingSocks\LostInTranslation\LostInTranslation $lit
* @param array $nodes
Expand Down

0 comments on commit e4ebbcb

Please sign in to comment.