prompts icon indicating copy to clipboard operation
prompts copied to clipboard

Displaying a final message when rendering spinner

Open joetannenbaum opened this issue 2 years ago • 3 comments

This PR introduces an optional third argument to the spin helper, allowing the user to display a final message after the spinner has completed its process.

The message can either be a string:

spin(
    function () {
        sleep(4);

        return 'A-OK';
    },
    'Checking system...',
    'System looks good!',
);

or a closure that receives the result as an argument:

spin(
    function () {
        sleep(4);

        return '8.2';
    },
    'Detecting PHP Version...',
    fn ($result) => "PHP Version: <info>{$result}</info>",
);

Here's what it looks like:

https://github.com/laravel/prompts/assets/2702148/a1f8a719-5c47-4ef1-9d2d-95b01e36adc3

joetannenbaum avatar Oct 21 '23 19:10 joetannenbaum

I realized yesterday after submitting this PR that since the spinner erases itself, you could just output a message yourself using something like info afterwards.

I still like the idea of the spinner taking care of this for you, but if you're not into the idea of adding an additional argument let me know.

joetannenbaum avatar Oct 22 '23 12:10 joetannenbaum

I realized yesterday after submitting this PR that since the spinner erases itself, you could just output a message yourself using something like info afterwards.

I still like the idea of the spinner taking care of this for you, but if you're not into the idea of adding an additional argument let me know.

Personally I prefer the addition of this feature, just because more than once I have to print a message just right before the spinner ends. So for example, if need to perform a lot of tasks and then print a message for every task, well, I really prefer this approach instead of writing more info functions every time.

asciito avatar Oct 23 '23 00:10 asciito

@joetannenbaum I created a quick artisan command to test the solution you proposed in case this pull request isn't merged, and it seems to have a minor newline issue :

app\Console\Commands\Test.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

use function Laravel\Prompts\spin;
use function Laravel\Prompts\info;


class Test extends Command
{
    protected $signature = "test";

    public function handle() : void
    {
        spin( fn() => sleep( 2 ), "Sleeping..." );

        info( "Awake" );

        spin( fn() => sleep( 2 ), "Sleeping..." );

        info( "Awake" );

        spin( fn() => sleep( 2 ), "Sleeping..." );

        info( "Awake" );
    }
}

https://github.com/laravel/prompts/assets/17177411/368b9b52-22c4-4591-a0b2-797d6500cd92

It appears that when a spin is called for the second time and an info is added afterward, there is a new line between them.

@jessarcher I suppose I should maybe add this as an issue ?

mho22 avatar Mar 30 '24 19:03 mho22