Репозитории /
AiRouter /
airouter/tui/anim.py
anim.py
| 1 | """Braille animations for the AiRouter TUI. |
| 2 | |
| 3 | The wave generator is a compact, dependency-free adaptation of the braille |
| 4 | ideas used in /home/koder/Repos/Scripts/bipfr_braille.py. |
| 5 | """ |
| 6 | |
| 7 | from __future__ import annotations |
| 8 | |
| 9 | import math |
| 10 | |
| 11 | from rich.text import Text |
| 12 | from textual.widgets import Static |
| 13 | |
| 14 | BRAILLE_BASE = 0x2800 |
| 15 | # Dot bits for a 2x4 braille cell: [row][column]. |
| 16 | DOT_BITS = ( |
| 17 | (0x01, 0x08), |
| 18 | (0x02, 0x10), |
| 19 | (0x04, 0x20), |
| 20 | (0x40, 0x80), |
| 21 | ) |
| 22 | SPINNER_FRAMES = "⣾⣽⣻⢿⡿⣟⣯⣷" |
| 23 | SPINNER_FRAMES_FINE = "⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏" |
| 24 | WAVE_COLORS = ("#7dd3fc", "#818cf8", "#a78bfa", "#c084fc", "#e879f9") |
| 25 | WAVE_COLORS_DIM = ("#33417d", "#3e3d82", "#4a3a87", "#57388c") |
| 26 | |
| 27 | |
| 28 | def spinner_frame(tick: int) -> str: |
| 29 | return SPINNER_FRAMES[tick % len(SPINNER_FRAMES)] |
| 30 | |
| 31 | |
| 32 | def braille_wave_frame(phase: float, width: int = 30, frequency: float = 0.34) -> str: |
| 33 | """Generate one row of braille cells forming a moving sine wave.""" |
| 34 | chars: list[str] = [] |
| 35 | for cell in range(width): |
| 36 | bits = 0 |
| 37 | for sub in (0, 1): |
| 38 | x = cell * 2 + sub |
| 39 | y = 1.5 + 1.35 * math.sin(x * frequency - phase) |
| 40 | y += 0.45 * math.sin(x * frequency * 0.45 + phase * 0.7) |
| 41 | for row in range(4): |
| 42 | if abs(row - y) < 0.58: |
| 43 | bits |= DOT_BITS[row][sub] |
| 44 | chars.append(chr(BRAILLE_BASE + bits)) |
| 45 | return "".join(chars) |
| 46 | |
| 47 | |
| 48 | def wave_text( |
| 49 | width: int = 30, |
| 50 | phase: float = 0.0, |
| 51 | *, |
| 52 | dim: bool = False, |
| 53 | ) -> Text: |
| 54 | colors = WAVE_COLORS_DIM if dim else WAVE_COLORS |
| 55 | text = Text() |
| 56 | for index, char in enumerate(braille_wave_frame(phase, width)): |
| 57 | if char != chr(BRAILLE_BASE): |
| 58 | text.append(char, style=colors[index % len(colors)]) |
| 59 | return text |
| 60 | |
| 61 | |
| 62 | class BrailleSpinner(Static): |
| 63 | """Small animated braille spinner used in the top bar.""" |
| 64 | |
| 65 | def __init__(self, **kwargs) -> None: |
| 66 | super().__init__("", **kwargs) |
| 67 | self._tick = 0 |
| 68 | |
| 69 | def on_mount(self) -> None: |
| 70 | self.set_interval(0.09, self._advance) |
| 71 | |
| 72 | def _advance(self) -> None: |
| 73 | self._tick += 1 |
| 74 | if self.display: |
| 75 | self.update(Text(spinner_frame(self._tick), style="#f0a35e")) |
| 76 | |
| 77 | |
| 78 | class BootLoader(Static): |
| 79 | """BIPfR-style startup screen: breathing braille word, halo, star dust.""" |
| 80 | |
| 81 | def __init__(self, message: str = "загрузка…", **kwargs) -> None: |
| 82 | super().__init__("", **kwargs) |
| 83 | self.message = message |
| 84 | self._phase = 0.0 |
| 85 | self._dots = 0 |
| 86 | |
| 87 | def on_mount(self) -> None: |
| 88 | self.set_interval(0.10, self._advance) |
| 89 | self._render_body() |
| 90 | |
| 91 | def set_message(self, message: str) -> None: |
| 92 | self.message = message |
| 93 | self._render_body() |
| 94 | |
| 95 | def _advance(self) -> None: |
| 96 | if not self.display: |
| 97 | return |
| 98 | self._phase += 0.38 |
| 99 | self._dots = (self._dots + 1) % 4 |
| 100 | self._render_body() |
| 101 | |
| 102 | def _render_body(self) -> None: |
| 103 | body = Text() |
| 104 | body.append("\n\n") |
| 105 | for line in bipfr_word_lines(self._phase, self.size.width or 120): |
| 106 | body.append_text(line) |
| 107 | body.append("\n") |
| 108 | body.append("\n") |
| 109 | body.append_text(wave_text(24, self._phase * 1.3, dim=True)) |
| 110 | body.append("\n\n") |
| 111 | for index, char in enumerate("BIPfR"): |
| 112 | body.append(char, style=f"bold {_WORD_PALETTE[index % len(_WORD_PALETTE)]}") |
| 113 | body.append("\n") |
| 114 | body.append("оркестратор нейросетей", style="#9a9fa9") |
| 115 | body.append("\n") |
| 116 | body.append(self.message + "." * self._dots, style="#8b8fa8") |
| 117 | body.append("\n\n") |
| 118 | filled = 1 + int(self._phase * 1.7) % 14 |
| 119 | body.append( |
| 120 | "[" + "#" * filled + "-" * (14 - filled) + "]", |
| 121 | style="#818cf8", |
| 122 | ) |
| 123 | self.update(body) |
| 124 | |
| 125 | |
| 126 | # --------------------------------------------------------------------------- |
| 127 | # BIPfR-inspired word scene: a breathing braille word with halo and star dust. |
| 128 | # --------------------------------------------------------------------------- |
| 129 | |
| 130 | _FONT_5X7 = { |
| 131 | "B": ("11110", "10001", "10001", "11110", "10001", "10001", "11110"), |
| 132 | "I": ("11111", "00100", "00100", "00100", "00100", "00100", "11111"), |
| 133 | "P": ("11110", "10001", "10001", "11110", "10000", "10000", "10000"), |
| 134 | "f": ("00110", "01000", "11100", "01000", "01000", "01000", "01000"), |
| 135 | "R": ("11110", "10001", "10001", "11110", "10100", "10010", "10001"), |
| 136 | } |
| 137 | _WORD = "BIPfR" |
| 138 | _WORD_PALETTE = ("#7dd3fc", "#818cf8", "#a78bfa", "#c084fc", "#e879f9") |
| 139 | _GLOW = "#343a7d" |
| 140 | _STAR = "#bfdbfe" |
| 141 | |
| 142 | |
| 143 | def _word_bitmap(scale: int) -> set[tuple[int, int]]: |
| 144 | dots: set[tuple[int, int]] = set() |
| 145 | char_w = 5 * scale |
| 146 | gap = scale |
| 147 | x0 = 2 * scale |
| 148 | y0 = 2 * scale |
| 149 | for char_index, char in enumerate(_WORD): |
| 150 | glyph = _FONT_5X7.get(char) |
| 151 | if not glyph: |
| 152 | continue |
| 153 | for y, row in enumerate(glyph): |
| 154 | for x, cell in enumerate(row): |
| 155 | if cell != "1": |
| 156 | continue |
| 157 | for dy in range(scale): |
| 158 | for dx in range(scale): |
| 159 | dots.add( |
| 160 | ( |
| 161 | y0 + y * scale + dy, |
| 162 | x0 + char_index * (char_w + gap) + x * scale + dx, |
| 163 | ) |
| 164 | ) |
| 165 | return dots |
| 166 | |
| 167 | |
| 168 | def bipfr_word_lines(phase: float, width_hint: int = 120) -> list[Text]: |
| 169 | """Render BIPfR as a breathing braille word with halo and star dust.""" |
| 170 | import random |
| 171 | |
| 172 | scale = 2 if width_hint >= 82 else 1 |
| 173 | word = _word_bitmap(scale) |
| 174 | if not word: |
| 175 | return [Text("BIPfR", style="bold #a78bfa")] |
| 176 | |
| 177 | max_y = max(y for y, _ in word) |
| 178 | max_x = max(x for _, x in word) |
| 179 | height = max_y + 1 |
| 180 | width = max_x + 1 |
| 181 | cells_w = (width + 1) // 2 |
| 182 | cells_h = (height + 3) // 4 |
| 183 | |
| 184 | glow: set[tuple[int, int]] = set() |
| 185 | for y, x in word: |
| 186 | for dy in (-1, 0, 1): |
| 187 | for dx in (-1, 0, 1): |
| 188 | if dy == 0 and dx == 0: |
| 189 | continue |
| 190 | point = (y + dy, x + dx) |
| 191 | if 0 <= point[0] < height and 0 <= point[1] < width and point not in word: |
| 192 | glow.add(point) |
| 193 | |
| 194 | rng = random.Random(17) |
| 195 | stars = [ |
| 196 | ( |
| 197 | rng.randrange(-3, height + 4), |
| 198 | rng.randrange(-4, width + 5), |
| 199 | rng.random() * 6.283, |
| 200 | ) |
| 201 | for _ in range(34) |
| 202 | ] |
| 203 | |
| 204 | lines: list[Text] = [] |
| 205 | for cy in range(cells_h): |
| 206 | line = Text() |
| 207 | for cx in range(cells_w): |
| 208 | word_bits = 0 |
| 209 | glow_bits = 0 |
| 210 | star_bits = 0 |
| 211 | for sub_x in (0, 1): |
| 212 | for row in range(4): |
| 213 | x = cx * 2 + sub_x |
| 214 | y = cy * 4 + row |
| 215 | if (y, x) in word: |
| 216 | word_bits |= DOT_BITS[row][sub_x] |
| 217 | if (y, x) in glow: |
| 218 | glow_bits |= DOT_BITS[row][sub_x] |
| 219 | for sub_x in (0, 1): |
| 220 | for row in range(4): |
| 221 | x = cx * 2 + sub_x |
| 222 | y = cy * 4 + row |
| 223 | for sy, sx, sph in stars: |
| 224 | if (sy, sx) == (y, x) and math.sin(phase * 1.7 + sph) > 0.35: |
| 225 | star_bits |= DOT_BITS[row][sub_x] |
| 226 | if word_bits: |
| 227 | sweep = 0.5 + 0.5 * math.sin(cx * 0.34 - phase * 1.8) |
| 228 | color = _WORD_PALETTE[ |
| 229 | min(len(_WORD_PALETTE) - 1, int(sweep * len(_WORD_PALETTE))) |
| 230 | ] |
| 231 | line.append(chr(BRAILLE_BASE + word_bits), style=f"bold {color}") |
| 232 | elif glow_bits: |
| 233 | line.append(chr(BRAILLE_BASE + glow_bits), style=_GLOW) |
| 234 | elif star_bits: |
| 235 | line.append(chr(BRAILLE_BASE + star_bits), style=_STAR) |
| 236 | else: |
| 237 | line.append(" ") |
| 238 | lines.append(line) |
| 239 | return lines |
| 240 | |
| 241 | |
| 242 | |
| 243 | class BipfrWordLoader(Static): |
| 244 | """Standalone animated word (useful for splash screens).""" |
| 245 | |
| 246 | def __init__(self, **kwargs) -> None: |
| 247 | super().__init__("", **kwargs) |
| 248 | self._phase = 0.0 |
| 249 | |
| 250 | def on_mount(self) -> None: |
| 251 | self.set_interval(0.1, self._advance) |
| 252 | |
| 253 | def _advance(self) -> None: |
| 254 | if not self.display: |
| 255 | return |
| 256 | self._phase += 0.35 |
| 257 | width = self.size.width or 120 |
| 258 | body = Text("\n") |
| 259 | for line in bipfr_word_lines(self._phase, width): |
| 260 | body.append_text(line) |
| 261 | body.append("\n") |
| 262 | self.update(body) |