yaspin icon indicating copy to clipboard operation
yaspin copied to clipboard

Switch to deactivate spinner

Open schmatzler-jona opened this issue 7 months ago • 1 comments

When using the spinner in a non-tty console (for example the logs of a job in GitHub Actions), we get way too many logs (one every _timer milliseconds (default is 80ms)) because there is no refresh of the lines.

To avoid this behavior, the easy solution is to use a switch depending on the environment:

use_spinner = os.environ.get("CI")  # or sys.stdout.isatty()
if use_spinner:
    with yaspin(text="Processing..."):
        # do something
        ...
        spinner.ok("Done.")  
else:
    print("Processing...")  # fallback log
    # do something
    ...
    print("Done.")

It would be nice to have a boolean parameter enabled in yaspin() that would make it act as a simple print("Processing") when set to true. We then could simplify the code above as:

use_spinner = os.environ.get("CI")  # or sys.stdout.isatty()
with yaspin(text="Processing...", enabled=use_spinner):
    # do something
    spinner.ok("Done.")  

schmatzler-jona avatar May 09 '25 08:05 schmatzler-jona

Hello,

Thanks for raising this issue. Env variable approach looks reasonable. I'll take a closer look on the implementation side. Also, please feel free to submit PR if you already have something.

pavdmyt avatar May 16 '25 10:05 pavdmyt

With v3.3.0, it is possible to set custom streams now. So, for your example, it would be something like:

use_spinner = os.environ.get("CI")
stream = sys.stderr if use_spinner else sys.stdout
if use_spinner:
    with yaspin(text="Processing...", stream=stream) as spinner:
        # do something
        ...
        spinner.ok("Done.")  
else:
    print("Processing...")  # fallback log
    # do something
    ...
    print("Done.")

pavdmyt avatar Oct 11 '25 09:10 pavdmyt

I'm closing this now. If proposed solution is not sufficient, feel free to re-open.

pavdmyt avatar Oct 20 '25 12:10 pavdmyt