1

I am using Symfonie's Process components, I am running a git clone command and would like to show a progressbar of it So far I have done this:

protected function cloneRepo(String $name)
{
    $process = new Process(
        "git clone {$this->getGitUrl(true)} {$name}" // does clone the repo works
    );

    $output = new ConsoleOutput();
    // creates a new progress bar (100 units)
    $progressBar = new ProgressBar($output, 100);

    $process->run();
    // starts and displays the progress bar
    $progressBar->start();

    $files = array_filter(explode("\n", $process->getOutput()), 'strlen');

    for ($i = 0; $i < count($files); $i++) {
        $progressBar->advance();
    }

    // ensures that the progress bar is at 100%
    $progressBar->finish();

    // executes after the command finishes
    if (!$process->isSuccessful()) {
        throw new ProcessFailedException($process);
    }

    echo $process->getOutput();
}

But that only shows the finished progress bar after the clone is already finished

Jon not doe xx
  • 533
  • 3
  • 10
  • 20

1 Answers1

0

I don't think there is an easy way to draw a progress bar, because as far as I can tell there is no way to tell how much of the data is processed. Generally speaking you could start the process and then use a callback in wait() to try to fetch the current output and calculate the progress from it. So your code could look something like this. You still have to replace the TODO with the actual logic to determine how much to advance.

$process = new Process(
    "git clone {$this->getGitUrl(true)} {$name}" // does clone the repo works
);
$progressBar = new ProgressBar($output, 100);
$process->start();
$progressBar->start();

$process->wait(function($type, $buffer) use ($progressBar) {
    // TODO: Read the current output from buffer and determine progress
    $progressBar->advance();
});
$progressBar->finish();
Ariel Magbanua
  • 3,083
  • 7
  • 37
  • 48
dbrumann
  • 16,803
  • 2
  • 42
  • 58