316 lines
9.9 KiB
Python
316 lines
9.9 KiB
Python
"""Braille animations for the AiRouter TUI.
|
|
|
|
The wave generator is a compact, dependency-free adaptation of the braille
|
|
ideas used in /home/koder/Repos/Scripts/bipfr_braille.py.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import math
|
|
|
|
from rich.text import Text
|
|
from textual.widgets import Static
|
|
|
|
BRAILLE_BASE = 0x2800
|
|
# Dot bits for a 2x4 braille cell: [row][column].
|
|
DOT_BITS = (
|
|
(0x01, 0x08),
|
|
(0x02, 0x10),
|
|
(0x04, 0x20),
|
|
(0x40, 0x80),
|
|
)
|
|
SPINNER_FRAMES = "⣾⣽⣻⢿⡿⣟⣯⣷"
|
|
SPINNER_FRAMES_FINE = "⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"
|
|
WAVE_COLORS = ("#7dd3fc", "#818cf8", "#a78bfa", "#c084fc", "#e879f9")
|
|
WAVE_COLORS_DIM = ("#33417d", "#3e3d82", "#4a3a87", "#57388c")
|
|
|
|
|
|
def spinner_frame(tick: int) -> str:
|
|
return SPINNER_FRAMES[tick % len(SPINNER_FRAMES)]
|
|
|
|
|
|
def braille_wave_frame(phase: float, width: int = 30, frequency: float = 0.34) -> str:
|
|
"""Generate one row of braille cells forming a moving sine wave."""
|
|
chars: list[str] = []
|
|
for cell in range(width):
|
|
bits = 0
|
|
for sub in (0, 1):
|
|
x = cell * 2 + sub
|
|
y = 1.5 + 1.35 * math.sin(x * frequency - phase)
|
|
y += 0.45 * math.sin(x * frequency * 0.45 + phase * 0.7)
|
|
for row in range(4):
|
|
if abs(row - y) < 0.58:
|
|
bits |= DOT_BITS[row][sub]
|
|
chars.append(chr(BRAILLE_BASE + bits))
|
|
return "".join(chars)
|
|
|
|
|
|
def wave_text(
|
|
width: int = 30,
|
|
phase: float = 0.0,
|
|
*,
|
|
dim: bool = False,
|
|
) -> Text:
|
|
colors = WAVE_COLORS_DIM if dim else WAVE_COLORS
|
|
text = Text()
|
|
for index, char in enumerate(braille_wave_frame(phase, width)):
|
|
if char != chr(BRAILLE_BASE):
|
|
text.append(char, style=colors[index % len(colors)])
|
|
return text
|
|
|
|
|
|
class BrailleSpinner(Static):
|
|
"""Small animated braille spinner used in the top bar."""
|
|
|
|
def __init__(self, **kwargs) -> None:
|
|
super().__init__("", **kwargs)
|
|
self._tick = 0
|
|
|
|
def on_mount(self) -> None:
|
|
self.set_interval(0.09, self._advance)
|
|
|
|
def _advance(self) -> None:
|
|
self._tick += 1
|
|
if self.display:
|
|
self.update(Text(spinner_frame(self._tick), style="#f0a35e"))
|
|
|
|
|
|
_ACTIVITY_FRAMES = (
|
|
"▁▂▃▄▅▆▇",
|
|
"▂▃▄▅▆▇▆",
|
|
"▃▄▅▆▇▆▅",
|
|
"▄▅▆▇▆▅▄",
|
|
"▅▆▇▆▅▄▃",
|
|
"▆▇▆▅▄▃▂",
|
|
"▇▆▅▄▃▂▁",
|
|
"▆▅▄▃▂▁▂",
|
|
"▅▄▃▂▁▂▃",
|
|
"▄▃▂▁▂▃▄",
|
|
"▃▂▁▂▃▄▅",
|
|
"▂▁▂▃▄▅▆",
|
|
)
|
|
|
|
|
|
class ActivityWave(Static):
|
|
"""A compact animated equalizer for active agent work."""
|
|
|
|
def __init__(self, *, active: bool = False, reduced: bool = False, **kwargs) -> None:
|
|
super().__init__("", **kwargs)
|
|
self.active = active
|
|
self.reduced = reduced
|
|
self._tick = 0
|
|
|
|
def on_mount(self) -> None:
|
|
self.set_interval(0.12, self._advance)
|
|
self._draw()
|
|
|
|
def set_active(self, active: bool, *, reduced: bool | None = None) -> None:
|
|
self.active = active
|
|
if reduced is not None:
|
|
self.reduced = reduced
|
|
self._draw()
|
|
|
|
def _advance(self) -> None:
|
|
if not self.display or self.reduced:
|
|
return
|
|
self._tick += 1
|
|
self._draw()
|
|
|
|
def _draw(self) -> None:
|
|
if not self.active:
|
|
self.update(Text("▁▁▁▁▁▁▁", style="#3a3f49"))
|
|
return
|
|
frame = "▁▂▃▄▅▆▇" if self.reduced else _ACTIVITY_FRAMES[self._tick % len(_ACTIVITY_FRAMES)]
|
|
text = Text()
|
|
colors = ("#7dd3fc", "#818cf8", "#a78bfa", "#c084fc", "#e879f9")
|
|
for index, char in enumerate(frame):
|
|
text.append(char, style=f"bold {colors[index % len(colors)]}")
|
|
self.update(text)
|
|
|
|
|
|
class BootLoader(Static):
|
|
"""BIPfR-style startup screen: breathing braille word, halo, star dust."""
|
|
|
|
def __init__(self, message: str = "загрузка…", **kwargs) -> None:
|
|
super().__init__("", **kwargs)
|
|
self.message = message
|
|
self._phase = 0.0
|
|
self._dots = 0
|
|
|
|
def on_mount(self) -> None:
|
|
self.set_interval(0.10, self._advance)
|
|
self._render_body()
|
|
|
|
def set_message(self, message: str) -> None:
|
|
self.message = message
|
|
self._render_body()
|
|
|
|
def _advance(self) -> None:
|
|
if not self.display:
|
|
return
|
|
self._phase += 0.38
|
|
self._dots = (self._dots + 1) % 4
|
|
self._render_body()
|
|
|
|
def _render_body(self) -> None:
|
|
body = Text()
|
|
body.append("\n\n")
|
|
for line in bipfr_word_lines(self._phase, self.size.width or 120):
|
|
body.append_text(line)
|
|
body.append("\n")
|
|
body.append("\n")
|
|
body.append_text(wave_text(24, self._phase * 1.3, dim=True))
|
|
body.append("\n\n")
|
|
for index, char in enumerate("BIPfR"):
|
|
body.append(char, style=f"bold {_WORD_PALETTE[index % len(_WORD_PALETTE)]}")
|
|
body.append("\n")
|
|
body.append("оркестратор нейросетей", style="#9a9fa9")
|
|
body.append("\n")
|
|
body.append(self.message + "." * self._dots, style="#8b8fa8")
|
|
body.append("\n\n")
|
|
filled = 1 + int(self._phase * 1.7) % 14
|
|
body.append(
|
|
"[" + "#" * filled + "-" * (14 - filled) + "]",
|
|
style="#818cf8",
|
|
)
|
|
self.update(body)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# BIPfR-inspired word scene: a breathing braille word with halo and star dust.
|
|
# ---------------------------------------------------------------------------
|
|
|
|
_FONT_5X7 = {
|
|
"B": ("11110", "10001", "10001", "11110", "10001", "10001", "11110"),
|
|
"I": ("11111", "00100", "00100", "00100", "00100", "00100", "11111"),
|
|
"P": ("11110", "10001", "10001", "11110", "10000", "10000", "10000"),
|
|
"f": ("00110", "01000", "11100", "01000", "01000", "01000", "01000"),
|
|
"R": ("11110", "10001", "10001", "11110", "10100", "10010", "10001"),
|
|
}
|
|
_WORD = "BIPfR"
|
|
_WORD_PALETTE = ("#7dd3fc", "#818cf8", "#a78bfa", "#c084fc", "#e879f9")
|
|
_GLOW = "#343a7d"
|
|
_STAR = "#bfdbfe"
|
|
|
|
|
|
def _word_bitmap(scale: int) -> set[tuple[int, int]]:
|
|
dots: set[tuple[int, int]] = set()
|
|
char_w = 5 * scale
|
|
gap = scale
|
|
x0 = 2 * scale
|
|
y0 = 2 * scale
|
|
for char_index, char in enumerate(_WORD):
|
|
glyph = _FONT_5X7.get(char)
|
|
if not glyph:
|
|
continue
|
|
for y, row in enumerate(glyph):
|
|
for x, cell in enumerate(row):
|
|
if cell != "1":
|
|
continue
|
|
for dy in range(scale):
|
|
for dx in range(scale):
|
|
dots.add(
|
|
(
|
|
y0 + y * scale + dy,
|
|
x0 + char_index * (char_w + gap) + x * scale + dx,
|
|
)
|
|
)
|
|
return dots
|
|
|
|
|
|
def bipfr_word_lines(phase: float, width_hint: int = 120) -> list[Text]:
|
|
"""Render BIPfR as a breathing braille word with halo and star dust."""
|
|
import random
|
|
|
|
scale = 2 if width_hint >= 82 else 1
|
|
word = _word_bitmap(scale)
|
|
if not word:
|
|
return [Text("BIPfR", style="bold #a78bfa")]
|
|
|
|
max_y = max(y for y, _ in word)
|
|
max_x = max(x for _, x in word)
|
|
height = max_y + 1
|
|
width = max_x + 1
|
|
cells_w = (width + 1) // 2
|
|
cells_h = (height + 3) // 4
|
|
|
|
glow: set[tuple[int, int]] = set()
|
|
for y, x in word:
|
|
for dy in (-1, 0, 1):
|
|
for dx in (-1, 0, 1):
|
|
if dy == 0 and dx == 0:
|
|
continue
|
|
point = (y + dy, x + dx)
|
|
if 0 <= point[0] < height and 0 <= point[1] < width and point not in word:
|
|
glow.add(point)
|
|
|
|
rng = random.Random(17)
|
|
stars = [
|
|
(
|
|
rng.randrange(-3, height + 4),
|
|
rng.randrange(-4, width + 5),
|
|
rng.random() * 6.283,
|
|
)
|
|
for _ in range(34)
|
|
]
|
|
|
|
lines: list[Text] = []
|
|
for cy in range(cells_h):
|
|
line = Text()
|
|
for cx in range(cells_w):
|
|
word_bits = 0
|
|
glow_bits = 0
|
|
star_bits = 0
|
|
for sub_x in (0, 1):
|
|
for row in range(4):
|
|
x = cx * 2 + sub_x
|
|
y = cy * 4 + row
|
|
if (y, x) in word:
|
|
word_bits |= DOT_BITS[row][sub_x]
|
|
if (y, x) in glow:
|
|
glow_bits |= DOT_BITS[row][sub_x]
|
|
for sub_x in (0, 1):
|
|
for row in range(4):
|
|
x = cx * 2 + sub_x
|
|
y = cy * 4 + row
|
|
for sy, sx, sph in stars:
|
|
if (sy, sx) == (y, x) and math.sin(phase * 1.7 + sph) > 0.35:
|
|
star_bits |= DOT_BITS[row][sub_x]
|
|
if word_bits:
|
|
sweep = 0.5 + 0.5 * math.sin(cx * 0.34 - phase * 1.8)
|
|
color = _WORD_PALETTE[
|
|
min(len(_WORD_PALETTE) - 1, int(sweep * len(_WORD_PALETTE)))
|
|
]
|
|
line.append(chr(BRAILLE_BASE + word_bits), style=f"bold {color}")
|
|
elif glow_bits:
|
|
line.append(chr(BRAILLE_BASE + glow_bits), style=_GLOW)
|
|
elif star_bits:
|
|
line.append(chr(BRAILLE_BASE + star_bits), style=_STAR)
|
|
else:
|
|
line.append(" ")
|
|
lines.append(line)
|
|
return lines
|
|
|
|
|
|
|
|
class BipfrWordLoader(Static):
|
|
"""Standalone animated word (useful for splash screens)."""
|
|
|
|
def __init__(self, **kwargs) -> None:
|
|
super().__init__("", **kwargs)
|
|
self._phase = 0.0
|
|
|
|
def on_mount(self) -> None:
|
|
self.set_interval(0.1, self._advance)
|
|
|
|
def _advance(self) -> None:
|
|
if not self.display:
|
|
return
|
|
self._phase += 0.35
|
|
width = self.size.width or 120
|
|
body = Text("\n")
|
|
for line in bipfr_word_lines(self._phase, width):
|
|
body.append_text(line)
|
|
body.append("\n")
|
|
self.update(body)
|