Last active
March 1, 2022 20:16
-
-
Save ptmcg/77eabb5e22bce1d62b1a657f68780c53 to your computer and use it in GitHub Desktop.
Rich spinners for "the computer is thinking"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import rich.progress | |
import rich.spinner | |
import time | |
import random | |
class RandomChars(rich.progress.ProgressColumn): | |
"""Simulation of computer 'thinking' by displaying random characters | |
Args: | |
chars (str): characters from which to choose for display. Defaults to 0-9A-F. | |
size (int, optional): Width of blinking lights column. Defaults to 12. | |
speed (float, optional): Speed factor of spinner, from 0.0 to 1.0. Defaults to 1.0. | |
""" | |
def __init__(self, | |
size: int = 12, | |
speed: float = 1.0, | |
chars: str = "0123456789ABCDEF", | |
finished_char: str = " ", | |
color="white", | |
**kwargs): | |
if not (0 < speed <= 1.0): | |
raise ValueError(f"speed must be a value 1-10 ({speed})") | |
super().__init__(**kwargs) | |
self.size = size | |
self.chars = chars | |
self.finished_char = finished_char | |
self.interval = int(speed * 10) | |
self.color = color | |
self.count = 0 | |
self.text = self.make_text() | |
def make_text(self): | |
return "".join(random.choice(self.chars) for _ in range(self.size)) | |
def render(self, task): | |
self.count += 1 | |
if (self.count % (11-self.interval)) == 0: | |
self.text = self.make_text() | |
text = ( | |
self.finished_char * self.size | |
if task.finished | |
else self.text | |
) | |
return f"[{self.color}]{text}[/{self.color}]" | |
class FlashingLights(RandomChars): | |
"""Flashing computer lights effect | |
Args: | |
size (int, optional): Width of blinking lights column. Defaults to 12. | |
speed (float, optional): Speed factor of spinner, from 0.0 to 1.0. Defaults to 1.0. | |
""" | |
def __init__(self, | |
size: int = 12, | |
speed: float = 1.0, | |
finished_char: str = chr(10304), | |
**kwargs): | |
super().__init__(chars="".join(chr(x) for x in range(10241, 10496)), size=size, speed=speed, finished_char=finished_char, **kwargs) | |
class FlashingLeds(RandomChars): | |
"""Flashing computer LEDs effect | |
Args: | |
size (int, optional): Width of blinking lights column. Defaults to 16. | |
speed (float, optional): Speed factor of spinner, from 0.0 to 1.0. Defaults to 1.0. | |
""" | |
def __init__(self, | |
size: int = 16, | |
speed: float = 1.0, | |
**kwargs): | |
super().__init__(chars=" ···", size=size, speed=speed, **kwargs) | |
if __name__ == '__main__': | |
progbar = rich.progress.Progress( | |
FlashingLights(speed=0.4, color="bright_yellow"), | |
RandomChars(speed=0.8, size=8, chars="0123456789ABCDEF"), | |
FlashingLeds(color="bright_red"), | |
rich.progress.SpinnerColumn(spinner_name="dots2", speed=0.5, style="white"), | |
) | |
with progbar: | |
n = 100 | |
task1 = progbar.add_task("[red]I'm thinking...", total=100) | |
for i in range(n): | |
time.sleep(0.2) | |
progbar.advance(task1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment