grasmash/symfony-console-spinner

通过Symfony Console创建动画旋转器的实用工具。

dev-main 2023-01-19 23:34 UTC

This package is not auto-updated.

Last update: 2024-09-15 01:21:02 UTC


README

此实用工具为与Symfony Console一起使用提供了两个工具

  1. 动画旋转器类。这是一个围绕Symfony内置进度条的外包装,将显示彩色动画旋转器。为了使旋转器旋转,需要调用advance()。
  2. 清单类。这是一个围绕旋转器的包装。它允许您发出清单项,在旁边显示旋转器以表示它正在进行中,并在项下方写入“消息详情”。

image

用法

简单旋转器

$output = new \Symfony\Component\Console\Output\ConsoleOutput();
$spinner = new Spinner($output);
$spinner->setMessage('Fetching a really big file from far away');
$spinner->start();
while (getting_the_file()) {
    $spinner->advance();
}
$spinner->finish();

简单清单

$output = new \Symfony\Component\Console\Output\ConsoleOutput();
$checklist = new Checklist($output);
$checklist->addItem('Fetching a really big file from far away');
while (getting_the_file()) {
    $checklist->updateProgressBar('Still getting the file');
}
$checklist->completePreviousItem();

$checklist->addItem('Doing the next thing');

高级清单示例

  use Symfony\Component\Process\Process;
  use Symfony\Component\Console\Output\ConsoleOutput;
  
  public function runMyCommand() {
    $output = new ConsoleOutput();
    $checklist = new Checklist($output);
    $checklist->addItem('Running a command with lots of output');

    $process = new Process([
      'composer',
      'run-script',
      'my-script',
      '--no-interaction',
    ]);
    $process->start();
    $process->wait(function ($type, $buffer) use ($checklist, $output) {
      if (!$output->isVerbose() && $checklist->getItems()) {
        $checklist->updateProgressBar($buffer);
      }
      $output->writeln($buffer, OutputInterface::VERBOSITY_VERY_VERBOSE);
    });
    if (!$process->isSuccessful()) {
      throw new \Exception('Something went wrong! {message}' . $process->getErrorOutput());
    }

    $checklist->completePreviousItem();
  }