| """Typing Effect - Typewriter animation""" |
|
|
| import sys |
| import time |
| from typing import Optional, Callable |
| from contextlib import contextmanager |
|
|
| from .config import DEFAULT_CONFIG, AnimationConfig |
|
|
|
|
| class TypingEffect: |
| """Typewriter-style text animation""" |
|
|
| def __init__( |
| self, |
| text: str = "", |
| delay: Optional[float] = None, |
| config: Optional[AnimationConfig] = None, |
| ): |
| self.text = text |
| self.config = config or DEFAULT_CONFIG |
| self.delay = delay if delay is not None else self.config.speed |
| self.cursor = "█" |
| self.blink_delay = 0.3 |
|
|
| def animate(self) -> str: |
| """Animate typing the text and return result""" |
| result = "" |
| cursor_on = True |
|
|
| for i, char in enumerate(self.text): |
| result += char |
| cursor_str = self.cursor if cursor_on else " " |
| sys.stdout.write(f"\r{result}{cursor_str}") |
| sys.stdout.flush() |
|
|
| time.sleep(self.delay) |
| cursor_on = not cursor_on |
|
|
| sys.stdout.write(f"\r{result} \n") |
| sys.stdout.flush() |
|
|
| return result |
|
|
| def animate_with_callback( |
| self, callback: Callable[[str], None], interval: float = 1.0 |
| ): |
| """Animate text and call callback at intervals""" |
| result = "" |
| callback_count = 0 |
|
|
| for char in self.text: |
| result += char |
| callback_count += 1 |
|
|
| sys.stdout.write(f"\r{result}") |
| sys.stdout.flush() |
|
|
| if callback_count % int(interval / self.delay) == 0: |
| callback(result) |
|
|
| time.sleep(self.delay) |
|
|
| sys.stdout.write(f"\r{result} \n") |
| sys.stdout.flush() |
|
|
|
|
| class SlowTypingEffect(TypingEffect): |
| """Slow typewriter effect for dramatic effect""" |
|
|
| def __init__(self, text: str = ""): |
| super().__init__(text=text, delay=0.1) |
|
|
|
|
| class FastTypingEffect(TypingEffect): |
| """Fast typewriter effect""" |
|
|
| def __init__(self, text: str = ""): |
| super().__init__(text=text, delay=0.01) |
|
|
|
|
| class CodeTypingEffect(TypingEffect): |
| """Typing effect optimized for code""" |
|
|
| def __init__(self, text: str = ""): |
| super().__init__(text=text, delay=0.005) |
|
|
| def __call__(self): |
| return self.animate() |
|
|
|
|
| @contextmanager |
| def typing_context(text: str, delay: float = 0.05): |
| """Context manager for typing effect""" |
| effect = TypingEffect(text=text, delay=delay) |
| print(f"\r{text[:10]}...", end="", flush=True) |
|
|
| try: |
| yield effect |
| finally: |
| for _ in range(len(text) + 5): |
| sys.stdout.write("\b \b") |
| sys.stdout.flush() |
| sys.stdout.flush() |
|
|
|
|
| class LiveTypingDisplay: |
| """Display that shows typing in real-time""" |
|
|
| def __init__(self): |
| self.content = "" |
|
|
| def append(self, char: str): |
| """Append character and update display""" |
| self.content += char |
| sys.stdout.write(char) |
| sys.stdout.flush() |
|
|
| def append_many(self, text: str): |
| """Append multiple characters""" |
| for char in text: |
| self.append(char) |
|
|
| def clear(self): |
| """Clear the display""" |
| self.content = "" |
| sys.stdout.write("\033[2K\r") |
| sys.stdout.flush() |
|
|
| def backspace(self, count: int = 1): |
| """Backspace characters""" |
| self.content = self.content[:-count] |
| backspace_seq = "\b \b" * count |
| sys.stdout.write(backspace_seq) |
| sys.stdout.flush() |
|
|
|
|
| def type_text(text: str, delay: float = 0.05): |
| """Quick function to type text to terminal""" |
| effect = TypingEffect(text=text, delay=delay) |
| return effect.animate() |
|
|