Skip to content

Commit

Permalink
Add Laravel Prompts to command interface
Browse files Browse the repository at this point in the history
  • Loading branch information
arduinomaster22 committed Jan 9, 2025
1 parent bba425c commit 7412651
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"php": "^8.1",
"illuminate/contracts": "^10.0|^11.0",
"laravel/helpers": "^1.7",
"laravel/prompts": "^0.1.25",
"lorisleiva/cron-translator": "^0.4.5",
"spatie/emoji": "^4.1",
"spatie/laravel-package-tools": "^1.14.0"
Expand Down
18 changes: 8 additions & 10 deletions src/Commands/UpdatePermanentCachesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
use Illuminate\Console\Command;
use Spatie\Emoji\Emoji;
use SplObjectStorage;
use Symfony\Component\Console\Helper\ProgressBar;
use Vormkracht10\PermanentCache\Facades\PermanentCache;

use function Laravel\Prompts\progress;

class UpdatePermanentCachesCommand extends Command
{
/**
Expand Down Expand Up @@ -47,36 +48,33 @@ public function handle()
$caches[$cache] = $parameters;
}

ProgressBar::setFormatDefinition('custom', ' %current%/%max% [%bar%] %message%');

$progressBar = $this->output->createProgressBar($caches->count());
$progressBar = progress(label: 'Updating caches', steps: $caches->count());

$progressBar->setFormat('custom');
$progressBar->setMessage('Starting...');
$progressBar->cancelMessage = 'Failed to update caches';

$progressBar->start();

foreach ($caches as $c) {
$cache = $caches->current();

$parameters = $caches->getInfo();

$currentTask = $cache->getName();
$emoji = ($progressBar->getProgress() % 2 ? Emoji::hourglassNotDone() : Emoji::hourglassDone());
$emoji = ($progressBar->progress % 2 ? Emoji::hourglassNotDone() : Emoji::hourglassDone());

$progressBar->setMessage('Updating: '.$currentTask.' '.$emoji);
$progressBar->hint('Updating: '.$currentTask.' '.$emoji);

try {
$cache->update($parameters);
} catch (Exception $exception) {
$progressBar->setMessage('Error: '.$currentTask.' '.Emoji::warning());
$progressBar->hint('Error: '.$currentTask.' '.Emoji::warning());

sleep(2);
}

$progressBar->advance();
}

$progressBar->setMessage('Finished!');
$progressBar->finish();
}
}

0 comments on commit 7412651

Please sign in to comment.