anixcode 0.8.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
anixcode/tui/anim.py ADDED
@@ -0,0 +1,242 @@
1
+ """Animated logo, the "agent is working" indicator, and the built-in themes."""
2
+ from __future__ import annotations
3
+ from ..i18n import tr
4
+
5
+ import math
6
+ import random
7
+ import time
8
+
9
+ from rich.color import Color, ColorTriplet, blend_rgb
10
+ from rich.style import Style
11
+ from rich.text import Text
12
+ from textual.widgets import Static
13
+
14
+ PEACH, BLUE, PURPLE = "#fab283", "#5c9cf5", "#9d7cd8"
15
+ WHITE = ColorTriplet(255, 255, 255)
16
+ BLACK = ColorTriplet(0, 0, 0)
17
+ SPINNER = "⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"
18
+ WAVE = "▁▂▃▄▅▆▇█"
19
+ SCRAMBLE = "░▒▓█▚▞▙▟▛▜"
20
+
21
+ # 5-row block font. Only █ and spaces: looks the same in cmd.exe and Windows Terminal.
22
+ FONT = {
23
+ "A": [" ████ ", "██ ██", "██████", "██ ██", "██ ██"],
24
+ "N": ["██ ██", "███ ██", "██ █ ██", "██ ███", "██ ██"],
25
+ "I": ["██", "██", "██", "██", "██"],
26
+ "X": ["██ ██", " ██ ██ ", " ███ ", " ██ ██ ", "██ ██"],
27
+ }
28
+ THEME_LABELS = {
29
+ "anix": tr("ANIX · персиковая"),
30
+ "noir": tr("Noir · чёрно-белая"),
31
+ "tokyo-night": "Tokyo Night",
32
+ "catppuccin-mocha": "Catppuccin Mocha",
33
+ "dracula": "Dracula",
34
+ "nord": "Nord",
35
+ "gruvbox": "Gruvbox",
36
+ "monokai": "Monokai",
37
+ "flexoki": "Flexoki",
38
+ "textual-dark": "Textual Dark",
39
+ }
40
+
41
+
42
+ def make_themes() -> list:
43
+ """Our Textual themes (empty on old Textual versions without theme support)."""
44
+ try:
45
+ from textual.theme import Theme
46
+ except ImportError:
47
+ return []
48
+ base = {"foreground": "#eeeeee", "background": "#0a0a0a", "surface": "#141414", "panel": "#1e1e1e",
49
+ "dark": True}
50
+ return [
51
+ Theme(name="anix", primary=PEACH, secondary=BLUE, accent=PURPLE, success="#7fd88f", warning="#f5a742",
52
+ error="#e06c75", **base),
53
+ Theme(name="noir", primary="#eeeeee", secondary="#a0a0a0", accent="#c8c8c8", success="#a6d189",
54
+ warning="#d8c08c", error="#e06c75", **base),
55
+ ]
56
+
57
+
58
+ def _rgb(hex_color: str) -> ColorTriplet:
59
+ return Color.parse(hex_color).get_truecolor()
60
+
61
+
62
+ def palette(app) -> tuple[str, str, str]:
63
+ """(primary, accent, secondary) of the active theme."""
64
+ try:
65
+ t = app.current_theme
66
+ return t.primary or PEACH, t.accent or PURPLE, t.secondary or BLUE
67
+ except Exception:
68
+ return PEACH, PURPLE, BLUE
69
+
70
+
71
+ class Gradient:
72
+ """Cyclic gradient through the given colors: at(0) == at(1)."""
73
+
74
+ def __init__(self, colors: tuple[str, ...]) -> None:
75
+ self.stops = [_rgb(c) for c in colors]
76
+
77
+ def at(self, x: float) -> ColorTriplet:
78
+ n = len(self.stops)
79
+ x = (x % 1.0) * n
80
+ i = int(x)
81
+ return blend_rgb(self.stops[i % n], self.stops[(i + 1) % n], x - i)
82
+
83
+
84
+ def _style(c: ColorTriplet, bold: bool = False) -> Style:
85
+ return Style(color=c.hex, bold=bold)
86
+
87
+
88
+ def render_word(word: str, gap: int = 2) -> list[str]:
89
+ rows = [""] * 5
90
+ for i, ch in enumerate(word):
91
+ glyph = FONT[ch]
92
+ width = max(len(r) for r in glyph)
93
+ for r in range(5):
94
+ rows[r] += glyph[r].ljust(width) + (" " * gap if i < len(word) - 1 else "")
95
+ return rows
96
+
97
+
98
+ class AnimatedLogo(Static):
99
+ """Big ANIX logo.
100
+
101
+ Intro: every block "decodes" from random glyphs, in a wave from left to right.
102
+ Then: the theme gradient flows through it, a diagonal shine crosses it every few
103
+ seconds, a soft 3D shadow sits underneath, and once in a while it glitches.
104
+ """
105
+
106
+ def __init__(self, footer: Text, animate: bool = True, word: str = "ANIX", **kwargs) -> None:
107
+ super().__init__("", **kwargs)
108
+ self.logo_rows = render_word(word)
109
+ self.logo_footer = footer
110
+ self.logo_motion = animate
111
+ self._logo_t0 = time.monotonic()
112
+ self._theme_name = ""
113
+ self._gradient = Gradient((PEACH, PURPLE, BLUE))
114
+ self.logo_w = max(len(r) for r in self.logo_rows)
115
+ self.logo_h = len(self.logo_rows)
116
+ rnd = random.Random(7)
117
+ self._cells = {(r, c) for r in range(self.logo_h) for c in range(self.logo_w)
118
+ if c < len(self.logo_rows[r]) and self.logo_rows[r][c] != " "}
119
+ self._settle = {cell: 0.15 + cell[1] / self.logo_w * 0.9 + rnd.random() * 0.3 for cell in self._cells}
120
+ self._noise = {cell: rnd.random() for cell in self._cells}
121
+
122
+ def on_mount(self) -> None:
123
+ self._frame()
124
+ if self.logo_motion:
125
+ self.set_interval(1 / 24, self._frame)
126
+
127
+ def _colors(self) -> None:
128
+ name = getattr(self.app, "theme", "")
129
+ if name != self._theme_name:
130
+ self._theme_name = name
131
+ self._gradient = Gradient(palette(self.app))
132
+
133
+ def _frame(self) -> None:
134
+ self._colors()
135
+ t = time.monotonic() - self._logo_t0 if self.logo_motion else 99.0
136
+ w, h = self.logo_w, self.logo_h
137
+ span = w + h * 2
138
+ shine = ((t - 1.6) % 5.0) / 5.0 * (span + 16) - 8 # diagonal band, every 5 s
139
+ glitching = self.logo_motion and t > 2 and (t % 7.3) < 0.14
140
+ frame_no = int(t * 24)
141
+ text = Text()
142
+ for r in range(h + 1): # +1 row for the shadow
143
+ for c in range(w + 1):
144
+ cell = (r, c)
145
+ if cell in self._cells:
146
+ settle = self._settle[cell]
147
+ if t < settle - 0.45:
148
+ text.append(" ")
149
+ continue
150
+ base = self._gradient.at(c / w * 0.55 + r * 0.035 - t * 0.07)
151
+ if t < settle: # still decoding: random glyph, bright
152
+ ch = SCRAMBLE[(frame_no + int(self._noise[cell] * 97)) % len(SCRAMBLE)]
153
+ text.append(ch, _style(blend_rgb(base, WHITE, 0.55), bold=True))
154
+ continue
155
+ if glitching and self._noise[cell] < 0.09:
156
+ text.append("▓", _style(blend_rgb(base, WHITE, 0.3), bold=True))
157
+ continue
158
+ glow = max(0.0, 1 - abs((c + r * 2) - shine) / 3.5)
159
+ pulse = 0.06 * math.sin(t * 1.7 + c * 0.25) # gentle breathing
160
+ color = blend_rgb(base, WHITE, min(1.0, max(0.0, glow * 0.8 + pulse)))
161
+ text.append("█", _style(color, bold=True))
162
+ elif (r - 1, c - 1) in self._cells and t >= self._settle[(r - 1, c - 1)]:
163
+ shade = self._gradient.at(c / w * 0.55 - t * 0.07)
164
+ text.append("░", _style(blend_rgb(shade, BLACK, 0.72)))
165
+ else:
166
+ text.append(" ")
167
+ text.append("\n")
168
+ # "c o d e" typed in after the logo settles
169
+ sub = "c o d e"
170
+ shown = max(0, min(len(sub), int((t - 1.3) * 18)))
171
+ line = sub[:shown].ljust(len(sub))
172
+ for i, ch in enumerate(line):
173
+ text.append(ch, _style(self._gradient.at(i / len(sub) * 0.4 - t * 0.07)))
174
+ text.append("\n")
175
+ text.append_text(self.logo_footer)
176
+ self.update(text)
177
+
178
+
179
+ class ActivityIndicator(Static):
180
+ """⠹ Пишет ответ… ▂▃▅▇▆▄▂▁▂▃▅▇ 12s · ≈1.2k токенов — shown while the agent works."""
181
+
182
+ def __init__(self, **kwargs) -> None:
183
+ super().__init__("", **kwargs)
184
+ self.phase = ""
185
+ self.chars = 0
186
+ self.paused = False
187
+ self.motion = True
188
+ self._t_start = time.monotonic()
189
+ self._gradient = Gradient((PEACH, PURPLE, BLUE))
190
+ self._c_primary = _rgb(PEACH)
191
+ self._c_dim = _rgb("#2a2a2a")
192
+
193
+ def on_mount(self) -> None:
194
+ self.display = False
195
+ self.set_interval(1 / 15, self._frame)
196
+
197
+ def start(self) -> None:
198
+ primary, accent, secondary = palette(self.app)
199
+ self._gradient = Gradient((primary, accent, secondary))
200
+ self._c_primary = _rgb(primary)
201
+ self._t_start = time.monotonic()
202
+ self.chars = 0
203
+ self.set_phase(tr("Думает"))
204
+ self.display = True
205
+
206
+ def stop(self) -> None:
207
+ self.display = False
208
+
209
+ def set_phase(self, phase: str, paused: bool = False) -> None:
210
+ self.phase = phase
211
+ self.paused = paused
212
+ self._frame()
213
+
214
+ def _frame(self) -> None:
215
+ if not self.display:
216
+ return
217
+ t = time.monotonic() - self._t_start
218
+ moving = self.motion and not self.paused
219
+ text = Text()
220
+ spin = "⏸" if self.paused else (SPINNER[int(t * 12) % len(SPINNER)] if moving else "●")
221
+ text.append(spin + " ", _style(self._gradient.at(t * 0.25), bold=True))
222
+ label = self.phase[:60] + ("" if self.paused else "…")
223
+ band = (t * 14) % (len(label) + 8) - 4 # shimmer running through the label
224
+ for i, ch in enumerate(label):
225
+ glow = max(0.0, 1 - abs(i - band) / 3) if moving else 0.0
226
+ text.append(ch, _style(blend_rgb(self._c_primary, WHITE, glow * 0.8), bold=glow > 0.4))
227
+ text.append(" ")
228
+ for i in range(16): # equalizer-style wave
229
+ if moving:
230
+ level = (math.sin(t * 5.2 - i * 0.55) * 0.6 + math.sin(t * 2.1 + i * 0.9) * 0.4 + 1) / 2
231
+ else:
232
+ level = 0.0
233
+ ch = WAVE[min(len(WAVE) - 1, int(level * len(WAVE)))]
234
+ color = blend_rgb(self._c_dim, self._gradient.at(i / 16 * 0.6 + t * 0.15), 0.35 + 0.65 * level)
235
+ text.append(ch, _style(color))
236
+ text.append(f" {int(t)}s", style="dim")
237
+ if self.chars:
238
+ approx = self.chars // 4
239
+ text.append(tr(' · ≈{0:.1f}k токенов', approx / 1000) if approx >= 1000 else tr(' · ≈{0} токенов', approx),
240
+ style="dim")
241
+ text.append(tr(" esc — стоп"), style="dim")
242
+ self.update(text)