manrajgrover / halo

💫 Beautiful spinners for terminal, IPython and Jupyter
MIT License
2.86k stars 148 forks source link

Bug: Crash when using an animation and switching to a new animated message shorter than current position #162

Open Nemo157 opened 2 years ago

Nemo157 commented 2 years ago

Description

If you set a message to be rendered using one of the animations, then stop it and change to a different message which is shorter than the original message but still needs animating, if the current index is longer than the new message an exception is thrown

System settings

Error

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.9/threading.py", line 973, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.9/threading.py", line 910, in run
    self._target(*self._args, **self._kwargs)
  File ".../lib/python3.9/site-packages/halo/halo.py", line 421, in render
    self._render_frame()
  File ".../lib/python3.9/site-packages/halo/halo.py", line 407, in _render_frame
    frame = self.frame()
  File ".../lib/python3.9/site-packages/halo/halo.py", line 441, in frame
    text_frame = self.text_frame()
  File ".../lib/python3.9/site-packages/halo/halo.py", line 464, in text_frame
    frame = frames[self._text_index]
IndexError: list index out of range

Expected behaviour

Reset _text_index to the start of the new message.

Steps to recreate

> python -c "import halo;halo=halo.Halo(animation='marquee',interval=1);halo.start('abc'*$COLUMNS);import time;time.sleep($COLUMNS*0.01*2);halo.start('x'*($COLUMNS+2))"

(not 100% reliable, can take a few tries to reproduce)