File size: 3,672 Bytes
a7d7463
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"""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()