jolicode / castor

🦫 DX oriented task runner and command launcher built with PHP.
https://castor.jolicode.com
MIT License
414 stars 22 forks source link

Async process progress indicator #489

Closed pmontoya closed 4 months ago

pmontoya commented 4 months ago

Hello,

I would like to display a progress indicator running a command. However, the run() function is sync and I don't how to switch it in async. I try with wait_for but without any success as isTerminated isser will be updating after run process. Any idea ?

If I create a PR, do you think it would be a better idea to add a runningIndicatorMessage to run() function or to add a parameter to make run async ?

Thank you for your help

joelwurtz commented 4 months ago

You could use a fiber for that, we use it to allow parallel running, something like this should work :

$fiber = new \Fiber(fn () => run(...))
$fiber->start();

while (!$fiber->isTerminated()) {
    if ($fiber->isSuspended()) {
        echo 'Still running'; // Here do you own progress indicator
        $fiber->resume();
    }
}
pmontoya commented 4 months ago

Thank you @joelwurtz !!