laravel / prompts

Beautiful and user-friendly forms for your command-line PHP applications.
https://laravel.com/docs/prompts
MIT License
531 stars 94 forks source link

Allow re-rendering during progress callback #155

Closed jessarcher closed 4 months ago

jessarcher commented 4 months ago

This PR allows developers to update the progress label and hint at any point during their callback by calling the render method on the Progress class. Previously, a re-render only occurred between each step, so updating the label or hint to reflect what was about to happen was not possible.

progress(
    label: 'Uploading...',
    steps: [
        'file1.zip',
        'file2.zip',
        'file3.zip',
    ],
    callback: function ($step, $progress) {
        $progress
            ->label("Uploading {$step}...")
            ->hint(rand(5, 10).'MB')
            ->render();

        sleep(5);

        // It doesn't make sense to call `render` here because the prompt will already
        // re-render to update the progress bar once this callback is completed.
        $progress->label("Uploaded {$step}");
    },
);

Additional render calls on each iteration can add up to a meaningful delay over a lot of iterations, so I don't think it makes sense to always force a re-render on every call to the label or hint methods.

Fixes #153