sindresorhus / ora

Elegant terminal spinner
MIT License
9.08k stars 269 forks source link

Can a spinner output line be erased/reused when promise is resolved? #232

Closed mblais closed 9 months ago

mblais commented 10 months ago

I have a series of fetches where I display a spinner for each one, and when each fetch resolves, the spinner shows a checkmark and EOL. This results in a series of "checkmark" lines in my output (I'm not supplying any spinner text).

For these calls I would prefer to "re-use" the spinner line instead of having it output a newline when it's finished.

Is this possible?

sindresorhus commented 10 months ago

Just create a Ora instance and update the .spinner property manually as the promises resolve.

mblais commented 9 months ago

This works for me:

if (options.quiet) {
    // No spinner
    response    = await fetch(url, payload)
} else {
    // Show spinner
    const spinner = ora(options.verbose ? `${method} ${url}` : '').start()
    response      = await fetch(url, payload)
    if (options.verbose) spinner.succeed()           // Stop the spinner but keep the message line
    else                 spinner.stop()              // Delete the entire spinner line
}