agent-plex 0.9.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.
- agent_plex-0.9.0.dist-info/METADATA +221 -0
- agent_plex-0.9.0.dist-info/RECORD +34 -0
- agent_plex-0.9.0.dist-info/WHEEL +5 -0
- agent_plex-0.9.0.dist-info/entry_points.txt +2 -0
- agent_plex-0.9.0.dist-info/licenses/LICENSE +22 -0
- agent_plex-0.9.0.dist-info/top_level.txt +1 -0
- plex_cli/__init__.py +1 -0
- plex_cli/auth.py +80 -0
- plex_cli/citations.py +42 -0
- plex_cli/cli.py +243 -0
- plex_cli/client.py +256 -0
- plex_cli/config.py +223 -0
- plex_cli/files.py +33 -0
- plex_cli/render.py +150 -0
- plex_cli/session.py +259 -0
- plex_cli/themes/catppuccin-mocha.toml +22 -0
- plex_cli/themes/dracula.toml +14 -0
- plex_cli/themes/gruvbox.toml +16 -0
- plex_cli/themes/matrix.toml +5 -0
- plex_cli/themes/terminal-coral.toml +3 -0
- plex_cli/themes/terminal-green.toml +4 -0
- plex_cli/themes/terminal-violet.toml +3 -0
- plex_cli/themes/terminal.toml +4 -0
- plex_cli/themes/tokyo-night.toml +14 -0
- plex_cli/tui/__init__.py +3 -0
- plex_cli/tui/app.py +565 -0
- plex_cli/tui/app.tcss +264 -0
- plex_cli/tui/backdrop.py +65 -0
- plex_cli/tui/commands.py +49 -0
- plex_cli/tui/effects.py +174 -0
- plex_cli/tui/screens.py +290 -0
- plex_cli/tui/themes.py +150 -0
- plex_cli/tui/widgets.py +259 -0
- plex_cli/turn.py +94 -0
plex_cli/tui/app.tcss
ADDED
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
Screen {
|
|
2
|
+
background: $background;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
#body {
|
|
6
|
+
height: 1fr;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/* ---- sidebar ---------------------------------------------------------- */
|
|
10
|
+
|
|
11
|
+
#sidebar {
|
|
12
|
+
width: 36;
|
|
13
|
+
height: 1fr;
|
|
14
|
+
background: $surface;
|
|
15
|
+
border-right: tall $panel;
|
|
16
|
+
padding: 1 0 0 0;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
#sidebar-title {
|
|
20
|
+
padding: 0 2 1 2;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
#sessions {
|
|
24
|
+
height: 1fr;
|
|
25
|
+
border: none;
|
|
26
|
+
background: $surface;
|
|
27
|
+
padding: 0 1;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
#sessions > .option-list--option {
|
|
31
|
+
padding: 0 1;
|
|
32
|
+
margin-bottom: 1;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
#sessions:focus {
|
|
36
|
+
border: none;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/* ---- chat ------------------------------------------------------------- */
|
|
40
|
+
|
|
41
|
+
#main {
|
|
42
|
+
height: 1fr;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
#chat {
|
|
46
|
+
width: 100%; /* not 1fr: Textual sizes fr widths net of the widest sibling margin (#statusbar 3+4) */
|
|
47
|
+
height: 1fr;
|
|
48
|
+
padding: 1 3 0 3;
|
|
49
|
+
scrollbar-size-vertical: 1;
|
|
50
|
+
scrollbar-color: $panel;
|
|
51
|
+
scrollbar-background: $background;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
#welcome {
|
|
55
|
+
width: 100%;
|
|
56
|
+
height: 100%;
|
|
57
|
+
padding: 3 2;
|
|
58
|
+
content-align: center middle;
|
|
59
|
+
text-align: center;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
.user {
|
|
63
|
+
height: auto;
|
|
64
|
+
margin: 1 0 0 0;
|
|
65
|
+
padding: 0 2;
|
|
66
|
+
border-left: thick $secondary;
|
|
67
|
+
background: $boost;
|
|
68
|
+
color: $foreground;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
.assistant {
|
|
72
|
+
height: auto;
|
|
73
|
+
margin: 1 0 1 0;
|
|
74
|
+
padding: 0 0 0 1;
|
|
75
|
+
border-left: thick $accent 50%;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
.assistant.failed {
|
|
79
|
+
border-left: thick $error;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
.assistant .who {
|
|
83
|
+
height: 1;
|
|
84
|
+
padding: 0 1;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
.assistant .status {
|
|
88
|
+
height: 1;
|
|
89
|
+
padding: 0 1;
|
|
90
|
+
display: none;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
.assistant .answer {
|
|
94
|
+
height: auto;
|
|
95
|
+
margin: 0;
|
|
96
|
+
padding: 0 1;
|
|
97
|
+
background: transparent;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
.assistant .answer > MarkdownParagraph,
|
|
101
|
+
.assistant .answer > MarkdownBulletList,
|
|
102
|
+
.assistant .answer > MarkdownOrderedList {
|
|
103
|
+
margin: 0 0 1 0;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
.assistant .sources {
|
|
107
|
+
height: auto;
|
|
108
|
+
padding: 0 1;
|
|
109
|
+
margin-top: 0;
|
|
110
|
+
display: none;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
.assistant .meta {
|
|
114
|
+
height: auto;
|
|
115
|
+
padding: 0 1;
|
|
116
|
+
margin-top: 1;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/* ---- prompt ------------------------------------------------------------ */
|
|
120
|
+
|
|
121
|
+
#menu {
|
|
122
|
+
height: auto;
|
|
123
|
+
max-height: 14;
|
|
124
|
+
margin: 0 3;
|
|
125
|
+
border: round $panel;
|
|
126
|
+
background: $surface;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/* Welcome screen: float the menu just above the prompt rather than taking
|
|
130
|
+
space from #chat, which would re-center (move) the logo as it opens/filters. */
|
|
131
|
+
#main.-welcome #menu {
|
|
132
|
+
overlay: screen;
|
|
133
|
+
offset-y: -100%;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/* Focus stays in the prompt while the menu is open, so OptionList would draw
|
|
137
|
+
its faint "blurred" cursor; use the focused cursor so the active row shows. */
|
|
138
|
+
#menu > .option-list--option-highlighted {
|
|
139
|
+
color: $block-cursor-foreground;
|
|
140
|
+
background: $block-cursor-background;
|
|
141
|
+
text-style: $block-cursor-text-style;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
#prompt-box {
|
|
145
|
+
height: auto;
|
|
146
|
+
margin: 0 3;
|
|
147
|
+
border: round $panel;
|
|
148
|
+
padding: 0 1;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
#prompt-box:focus-within {
|
|
152
|
+
border: round $accent;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
#prompt {
|
|
156
|
+
height: auto;
|
|
157
|
+
min-height: 1;
|
|
158
|
+
max-height: 10;
|
|
159
|
+
background: transparent;
|
|
160
|
+
border: none;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
#statusbar {
|
|
164
|
+
height: 1;
|
|
165
|
+
margin: 0 3 0 4;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
#status-left {
|
|
169
|
+
width: 1fr;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
#status-right {
|
|
173
|
+
width: auto;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/* ---- dialogs ----------------------------------------------------------- */
|
|
177
|
+
|
|
178
|
+
ModalScreen {
|
|
179
|
+
align: center middle;
|
|
180
|
+
background: $background 60%;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
.dialog {
|
|
184
|
+
width: 64;
|
|
185
|
+
height: auto;
|
|
186
|
+
max-height: 90%;
|
|
187
|
+
padding: 1 2;
|
|
188
|
+
border: round $accent;
|
|
189
|
+
background: $surface;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
.dialog.wide {
|
|
193
|
+
width: 84;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
.dialog .title {
|
|
197
|
+
margin-bottom: 1;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
.dialog .hint {
|
|
201
|
+
margin-top: 1;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
.dialog Input {
|
|
205
|
+
margin-top: 1;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
.dialog OptionList {
|
|
209
|
+
height: auto;
|
|
210
|
+
border: none;
|
|
211
|
+
background: $surface;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/* ---- terminal themes ---------------------------------------------------
|
|
215
|
+
The terminal's own background shows through; lines are dim gray and only
|
|
216
|
+
accents carry color. */
|
|
217
|
+
|
|
218
|
+
#sidebar:ansi {
|
|
219
|
+
border-right: vkey ansi_bright_black;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
.user:ansi {
|
|
223
|
+
border-left: outer $secondary;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
.assistant:ansi {
|
|
227
|
+
border-left: outer $accent;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
#menu:ansi, #prompt-box:ansi, .dialog:ansi {
|
|
231
|
+
border: round ansi_bright_black;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
#prompt-box:ansi:focus-within, .dialog:ansi {
|
|
235
|
+
border: round $accent;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
CommandPalette:ansi #--input {
|
|
239
|
+
border: hkey ansi_bright_black;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
CommandPalette:ansi CommandList {
|
|
243
|
+
border-bottom: hkey ansi_bright_black;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/* ---- notifications ---------------------------------------------------- */
|
|
247
|
+
|
|
248
|
+
/* Toasts sit in the top-right corner instead of Textual's bottom-right. */
|
|
249
|
+
ToastRack {
|
|
250
|
+
dock: top;
|
|
251
|
+
align: right top;
|
|
252
|
+
margin-bottom: 0;
|
|
253
|
+
margin-top: 1;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/* Plain notices take the theme's accent instead of the fixed success green;
|
|
257
|
+
warnings and errors keep their own colors. */
|
|
258
|
+
Toast.-information {
|
|
259
|
+
border-left: outer $accent;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
Toast.-information .toast--title {
|
|
263
|
+
color: $accent;
|
|
264
|
+
}
|
plex_cli/tui/backdrop.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"""The app's animated background: whether the theme's effect is on, and when it animates.
|
|
2
|
+
|
|
3
|
+
effects.py draws the effect; this module decides when. The app creates one `Backdrop`,
|
|
4
|
+
calls `start` once mounted, `apply` whenever the theme changes, and `focus_changed` when the
|
|
5
|
+
terminal window gains or loses focus.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from textual.app import App
|
|
11
|
+
from textual.timer import Timer
|
|
12
|
+
|
|
13
|
+
from .effects import RAIN, RainField
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class Backdrop:
|
|
17
|
+
def __init__(self, app: App, widgets: str) -> None:
|
|
18
|
+
"""`widgets` selects, on the app's main screen, the backdrop widgets
|
|
19
|
+
(effects.RainBackdrop) to redraw on every frame."""
|
|
20
|
+
self._app = app
|
|
21
|
+
self._widgets = widgets
|
|
22
|
+
self.rain = RainField() # read by the backdrop widgets
|
|
23
|
+
self._timer: Timer | None = None
|
|
24
|
+
|
|
25
|
+
@property
|
|
26
|
+
def active(self) -> bool:
|
|
27
|
+
"""The theme has the effect on."""
|
|
28
|
+
return self.rain.active
|
|
29
|
+
|
|
30
|
+
@property
|
|
31
|
+
def animating(self) -> bool:
|
|
32
|
+
"""Frames are being drawn: the effect is on and the terminal window has focus."""
|
|
33
|
+
return self._timer is not None and self.rain.active and self._app.app_focus
|
|
34
|
+
|
|
35
|
+
def start(self) -> None:
|
|
36
|
+
self._timer = self._app.set_interval(1 / RainField.FPS, self._tick, pause=True)
|
|
37
|
+
self._sync()
|
|
38
|
+
|
|
39
|
+
def apply(self, theme: object) -> None:
|
|
40
|
+
"""Turn the effect on or off for `theme` (a themes.PlexTheme sets `effect`)."""
|
|
41
|
+
active = getattr(theme, "effect", None) == RAIN
|
|
42
|
+
if active == self.rain.active:
|
|
43
|
+
return
|
|
44
|
+
self.rain.active = active
|
|
45
|
+
if active:
|
|
46
|
+
self.rain.resize(*self._app.size)
|
|
47
|
+
self._sync()
|
|
48
|
+
self._redraw()
|
|
49
|
+
|
|
50
|
+
def focus_changed(self) -> None:
|
|
51
|
+
self._sync()
|
|
52
|
+
|
|
53
|
+
def _sync(self) -> None:
|
|
54
|
+
if self._timer is not None:
|
|
55
|
+
(self._timer.resume if self.animating else self._timer.pause)()
|
|
56
|
+
|
|
57
|
+
def _tick(self) -> None:
|
|
58
|
+
self.rain.resize(*self._app.size)
|
|
59
|
+
self.rain.tick()
|
|
60
|
+
self._redraw()
|
|
61
|
+
|
|
62
|
+
def _redraw(self) -> None:
|
|
63
|
+
main = self._app.screen_stack[0] # the chat screen, even while a dialog is open
|
|
64
|
+
for widget in main.query(self._widgets):
|
|
65
|
+
widget.refresh()
|
plex_cli/tui/commands.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"""Slash commands: one table for the menu, the help screen, the command palette and dispatch."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@dataclass(frozen=True)
|
|
9
|
+
class Command:
|
|
10
|
+
name: str
|
|
11
|
+
help: str
|
|
12
|
+
handler: str # PlexApp method, called with the text after the command
|
|
13
|
+
aliases: tuple[str, ...] = () # also accepted, not listed in the menu
|
|
14
|
+
fill_on_enter: bool = False # enter in the menu types `/name ` instead of running it
|
|
15
|
+
palette: tuple[str, str] | None = None # (title, help) in the ctrl+p command palette
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
# In menu order. `exit`/`quit` and `clear`/`new` are listed separately so both names show up.
|
|
19
|
+
COMMANDS: list[Command] = [
|
|
20
|
+
Command("exit", "Exit plex", "cmd_quit", aliases=("q",)),
|
|
21
|
+
Command("history", "Browse chat history", "cmd_history",
|
|
22
|
+
palette=("History", "Toggle the conversation list")),
|
|
23
|
+
Command("preset", "Pick a research effort preset (drops a selected model)", "cmd_preset",
|
|
24
|
+
aliases=("auto-effort",), palette=("Preset", "Change research effort preset")),
|
|
25
|
+
Command("model", "Pick a model from a list, or /model <id> · /model off", "cmd_model",
|
|
26
|
+
palette=("Model", "Pick a specific model")),
|
|
27
|
+
Command("effort", "Reasoning effort for the selected model", "cmd_effort",
|
|
28
|
+
palette=("Effort", "Reasoning effort for the selected model")),
|
|
29
|
+
Command("clear", "Start a new conversation", "cmd_new"),
|
|
30
|
+
Command("search", "Toggle web search, or /search on · /search off", "cmd_search",
|
|
31
|
+
palette=("Toggle web search", "Turn web search on or off")),
|
|
32
|
+
Command("sources", "List sources of the last answer", "cmd_sources"),
|
|
33
|
+
Command("copy", "Copy the last answer to the clipboard", "cmd_copy"),
|
|
34
|
+
Command("rename", "Rename this conversation: /rename <title>", "cmd_rename", fill_on_enter=True),
|
|
35
|
+
Command("delete", "Delete this conversation", "cmd_delete"),
|
|
36
|
+
Command("new", "Start a new conversation", "cmd_new",
|
|
37
|
+
palette=("New chat", "Start a new conversation")),
|
|
38
|
+
Command("theme", "Change the color theme", "cmd_theme"),
|
|
39
|
+
Command("login", "Set your Perplexity API key", "cmd_login", aliases=("key",),
|
|
40
|
+
palette=("Set API key", "Store a Perplexity API key")),
|
|
41
|
+
Command("help", "Keys and commands", "cmd_help", palette=("Help", "Keys and commands")),
|
|
42
|
+
Command("quit", "Exit plex", "cmd_quit"),
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
_BY_NAME = {name: c for c in COMMANDS for name in (c.name, *c.aliases)}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def lookup(name: str) -> Command | None:
|
|
49
|
+
return _BY_NAME.get(name.lower())
|
plex_cli/tui/effects.py
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
"""Animated background effects that a theme can turn on (`effect = "rain"`).
|
|
2
|
+
|
|
3
|
+
The rain is one screen-sized field; backdrop.py turns it on and animates it.
|
|
4
|
+
Widgets that form the app's background (`RainBackdrop`) draw their slice of it
|
|
5
|
+
into their empty cells, so it falls around messages, the input and the sidebar
|
|
6
|
+
without running behind any text.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import random
|
|
12
|
+
from dataclasses import dataclass
|
|
13
|
+
|
|
14
|
+
from rich.segment import Segment
|
|
15
|
+
from rich.style import Style
|
|
16
|
+
from textual.containers import Vertical, VerticalScroll
|
|
17
|
+
from textual.geometry import Region
|
|
18
|
+
from textual.strip import Strip
|
|
19
|
+
|
|
20
|
+
RAIN = "rain"
|
|
21
|
+
EFFECTS = {RAIN}
|
|
22
|
+
|
|
23
|
+
# Half-width katakana (one cell wide, like the film's code) plus digits and a few symbols.
|
|
24
|
+
GLYPHS = "ヲアウエオカキケコサシスセソタツテナニヌネハヒホマミムメモヤユラリワ0123456789:.=*+-<>¦"
|
|
25
|
+
|
|
26
|
+
# Head first, then the trail fading out. Kept dim so it stays in the background.
|
|
27
|
+
SHADES = ["#3DAA55", "#13802C", "#0E6B23", "#0B581D", "#084516", "#063510", "#04260B"]
|
|
28
|
+
_SHADE_STYLES = [Style(color=c) for c in SHADES]
|
|
29
|
+
|
|
30
|
+
GAP = 2 # cells kept clear around text drawn by a backdrop widget (e.g. the welcome logo)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@dataclass
|
|
34
|
+
class Drop:
|
|
35
|
+
y: float # head row
|
|
36
|
+
speed: float # rows per tick
|
|
37
|
+
length: int # trail length
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class RainField:
|
|
41
|
+
FPS = 8
|
|
42
|
+
DENSITY = 0.45 # share of columns with a drop falling at any moment
|
|
43
|
+
|
|
44
|
+
def __init__(self) -> None:
|
|
45
|
+
self.active = False
|
|
46
|
+
self.width = self.height = 0
|
|
47
|
+
self._drops: list[Drop | None] = []
|
|
48
|
+
self._glyphs: list[list[str]] = []
|
|
49
|
+
self._shade: list[list[int]] = [] # per cell: index into SHADES, or -1 for empty
|
|
50
|
+
self._rows: dict[tuple[int, Style], Strip] = {} # full screen rows built this frame
|
|
51
|
+
self._styles: dict[Style, list[Style]] = {}
|
|
52
|
+
|
|
53
|
+
def resize(self, width: int, height: int) -> None:
|
|
54
|
+
if (width, height) == (self.width, self.height):
|
|
55
|
+
return
|
|
56
|
+
self.width, self.height = width, height
|
|
57
|
+
self._glyphs = [[random.choice(GLYPHS) for _ in range(width)] for _ in range(height)]
|
|
58
|
+
self._drops = [self._new_drop(anywhere=True) if random.random() < self.DENSITY else None
|
|
59
|
+
for _ in range(width)]
|
|
60
|
+
self._update_shade()
|
|
61
|
+
|
|
62
|
+
def _new_drop(self, anywhere: bool = False) -> Drop:
|
|
63
|
+
h = self.height
|
|
64
|
+
start = random.uniform(0, h) if anywhere else -random.uniform(0, h / 2)
|
|
65
|
+
return Drop(y=start, speed=random.uniform(0.5, 1.4), length=random.randint(6, max(8, h // 2)))
|
|
66
|
+
|
|
67
|
+
def tick(self) -> None:
|
|
68
|
+
w, h = self.width, self.height
|
|
69
|
+
if not w or not h:
|
|
70
|
+
return
|
|
71
|
+
for x, drop in enumerate(self._drops):
|
|
72
|
+
if drop is None:
|
|
73
|
+
if random.random() < self.DENSITY / 20:
|
|
74
|
+
self._drops[x] = self._new_drop()
|
|
75
|
+
else:
|
|
76
|
+
drop.y += drop.speed
|
|
77
|
+
if drop.y - drop.length > h:
|
|
78
|
+
self._drops[x] = None
|
|
79
|
+
for _ in range(max(1, w * h // 60)): # a few glyphs change in place, as in the film
|
|
80
|
+
self._glyphs[random.randrange(h)][random.randrange(w)] = random.choice(GLYPHS)
|
|
81
|
+
self._update_shade()
|
|
82
|
+
|
|
83
|
+
def _update_shade(self) -> None:
|
|
84
|
+
w, h = self.width, self.height
|
|
85
|
+
shade = [[-1] * w for _ in range(h)]
|
|
86
|
+
last = len(SHADES) - 1
|
|
87
|
+
for x, drop in enumerate(self._drops):
|
|
88
|
+
if drop is None:
|
|
89
|
+
continue
|
|
90
|
+
head = int(drop.y)
|
|
91
|
+
for behind in range(drop.length):
|
|
92
|
+
y = head - behind
|
|
93
|
+
if 0 <= y < h:
|
|
94
|
+
shade[y][x] = 0 if behind == 0 else min(last, 1 + behind * last // drop.length)
|
|
95
|
+
self._shade = shade
|
|
96
|
+
self._rows.clear()
|
|
97
|
+
|
|
98
|
+
def _row(self, y: int, base: Style) -> Strip:
|
|
99
|
+
"""One full screen row of rain, built once per frame and shared by every backdrop."""
|
|
100
|
+
key = (y, base)
|
|
101
|
+
if (strip := self._rows.get(key)) is not None:
|
|
102
|
+
return strip
|
|
103
|
+
styles = self._styles.get(base)
|
|
104
|
+
if styles is None:
|
|
105
|
+
styles = self._styles[base] = [base + s for s in _SHADE_STYLES]
|
|
106
|
+
shades, glyphs = self._shade[y], self._glyphs[y]
|
|
107
|
+
segments: list[Segment] = []
|
|
108
|
+
start = 0
|
|
109
|
+
for x in range(1, self.width + 1):
|
|
110
|
+
if x < self.width and shades[x] == shades[start]:
|
|
111
|
+
continue
|
|
112
|
+
idx = shades[start]
|
|
113
|
+
if idx < 0:
|
|
114
|
+
segments.append(Segment(" " * (x - start), base))
|
|
115
|
+
else:
|
|
116
|
+
segments.append(Segment("".join(glyphs[start:x]), styles[idx]))
|
|
117
|
+
start = x
|
|
118
|
+
strip = self._rows[key] = Strip(segments, self.width)
|
|
119
|
+
return strip
|
|
120
|
+
|
|
121
|
+
def strip(self, x0: int, y: int, width: int, base: Style) -> Strip:
|
|
122
|
+
"""Screen row `y`, columns x0..x0+width, drawn over the `base` (background) style."""
|
|
123
|
+
if width <= 0:
|
|
124
|
+
return Strip([], 0)
|
|
125
|
+
if not 0 <= y < self.height or x0 < 0 or x0 + width > self.width:
|
|
126
|
+
return Strip.blank(width, base)
|
|
127
|
+
return self._row(y, base).crop(x0, x0 + width)
|
|
128
|
+
|
|
129
|
+
def overlay(self, strip: Strip, x0: int, y: int) -> Strip:
|
|
130
|
+
"""Fill the blank cells of `strip` (screen row y, from column x0) with rain,
|
|
131
|
+
keeping any text plus a small margin around it clear."""
|
|
132
|
+
text = strip.text
|
|
133
|
+
width = strip.cell_length
|
|
134
|
+
if len(text) != width: # wide characters: leave the line alone
|
|
135
|
+
return strip
|
|
136
|
+
segments = list(strip)
|
|
137
|
+
base = Style(bgcolor=segments[0].style.bgcolor) if segments and segments[0].style else Style()
|
|
138
|
+
start = len(text) - len(text.lstrip(" "))
|
|
139
|
+
if start == width:
|
|
140
|
+
return self.strip(x0, y, width, base)
|
|
141
|
+
end = len(text.rstrip(" "))
|
|
142
|
+
left, right = max(0, start - GAP), min(width, end + GAP)
|
|
143
|
+
return Strip.join([
|
|
144
|
+
self.strip(x0, y, left, base),
|
|
145
|
+
strip.crop(left, right),
|
|
146
|
+
self.strip(x0 + right, y, width - right, base),
|
|
147
|
+
])
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
class RainBackdrop:
|
|
151
|
+
"""Mixin for background widgets: draws the app's rain into their blank cells."""
|
|
152
|
+
|
|
153
|
+
TEXTLESS = False # True for containers: their own cells are all blank, so skip the text scan
|
|
154
|
+
|
|
155
|
+
def render_lines(self, crop: Region) -> list[Strip]:
|
|
156
|
+
backdrop = getattr(self.app, "backdrop", None) # backdrop.Backdrop, on the plex app
|
|
157
|
+
rain: RainField | None = backdrop.rain if backdrop is not None else None
|
|
158
|
+
if rain is None or not rain.active:
|
|
159
|
+
return super().render_lines(crop) # type: ignore[misc]
|
|
160
|
+
region = self.region # type: ignore[attr-defined]
|
|
161
|
+
x0, y0 = region.x + crop.x, region.y + crop.y
|
|
162
|
+
if self.TEXTLESS:
|
|
163
|
+
base = Style(bgcolor=self.rich_style.bgcolor) # type: ignore[attr-defined]
|
|
164
|
+
return [rain.strip(x0, y0 + i, crop.width, base) for i in range(crop.height)]
|
|
165
|
+
lines = super().render_lines(crop) # type: ignore[misc]
|
|
166
|
+
return [rain.overlay(line, x0, y0 + i) for i, line in enumerate(lines)]
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
class BackdropVertical(RainBackdrop, Vertical):
|
|
170
|
+
TEXTLESS = True
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
class BackdropScroll(RainBackdrop, VerticalScroll):
|
|
174
|
+
TEXTLESS = True
|