feat: redesign spinner animation with tail

This commit is contained in:
Paul Gauthier 2025-05-08 15:57:56 -07:00 committed by Paul Gauthier (aider)
parent 5bb891b2bb
commit 3a5a46253d

View file

@ -267,36 +267,43 @@ class Spinner:
# Pre-render the animation frames using pure ASCII so they will # Pre-render the animation frames using pure ASCII so they will
# always display, even on very limited terminals. # always display, even on very limited terminals.
ascii_forward = """ ascii_frames = """
[#------] [#=-----]
[-#-----] [=#-----]
[--#----] [-=#----]
[---#---] [--=#---]
[----#--] [---=#--]
[-----#-] [----=#-]
[------#] [-----=#]
[-----#=]
[----#=-]
[---#=--]
[---#=--]
[---#=--]
[--#=---]
[-#=----]
""".strip().splitlines() """.strip().splitlines()
# If unicode is supported, swap the ASCII chars for nicer glyphs. # If unicode is supported, swap the ASCII chars for nicer glyphs.
if self._supports_unicode(): if self._supports_unicode():
scan_char, trail_char = "", "" scan_char, tail_char, trail_char = "", "=", ""
forward = [f.replace("#", scan_char).replace("-", trail_char) for f in ascii_forward] frames = [f.replace("#", scan_char).replace("-", trail_char) for f in ascii_frames]
else: else:
scan_char, trail_char = "#", "-" scan_char, tail_char, trail_char = "#", "=", "-"
forward = ascii_forward frames = ascii_frames
# Bounce the scanner back and forth. # Bounce the scanner back and forth.
self.frames = forward + forward[-2:0:-1] self.frames = frames
self.frame_idx = 0 self.frame_idx = 0
self.scan_char = scan_char self.scan_char = scan_char
self.width = len(forward[0]) - 2 # number of chars between the brackets self.width = len(frames[0]) - 2 # number of chars between the brackets
self.animation_len = len(forward[0]) self.animation_len = len(frames[0])
def _supports_unicode(self) -> bool: def _supports_unicode(self) -> bool:
if not self.is_tty: if not self.is_tty:
return False return False
try: try:
sys.stdout.write("\b \b") sys.stdout.write("\b\b")
sys.stdout.flush() sys.stdout.flush()
return True return True
except UnicodeEncodeError: except UnicodeEncodeError: