-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay.py
More file actions
409 lines (361 loc) · 11.9 KB
/
Copy pathdisplay.py
File metadata and controls
409 lines (361 loc) · 11.9 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
"""Rich-powered output: banner, spinners, next-steps panel."""
from __future__ import annotations
import os
import shutil
import subprocess
import sys
from contextlib import contextmanager
from pathlib import Path
from typing import Iterator
from rich.console import Console, Group
from rich.padding import Padding
from rich.panel import Panel
from rich.progress import Progress, SpinnerColumn, TextColumn
from rich.table import Table
from rich.text import Text
from . import strings
from .config import COLOR_AMBER, COLOR_CYAN, COLOR_LIME, VERSION
_BOLD_CYAN = f"bold {COLOR_CYAN}"
_BOLD_LIME = f"bold {COLOR_LIME}"
console = Console()
# Separate stderr stream for out-of-band notes (e.g. the hidden testing-shortcut warning),
# so they don't interleave with the primary stdout output.
err_console = Console(stderr=True)
NEXT_STEPS: dict[str, tuple[tuple[str, str | None], ...]] = {
"minimal_workspace": (
(strings.STEPS_LABEL_RUN_SAMPLE_SHOP, strings.CMD_DLTHUB_RUN_SAMPLE_SHOP),
(strings.STEPS_LABEL_VIEW_SAMPLE_SHOP_RUNS, strings.CMD_DLTHUB_JOB_RUNS_SHOW_SAMPLE_SHOP),
(strings.STEPS_LABEL_EDIT_PIPELINE, None),
),
}
CREATED_TREE: dict[str, tuple[str, ...]] = {
"minimal_workspace": (
"pyproject.toml",
"pipeline.py",
"__deployment__.py",
".dlt/",
"README.md",
),
}
def substep_done(message: str) -> None:
"""Tick a finished sub-step with a green check."""
console.print(f"[green]✓[/green] {message}")
def substep_detail(message: str) -> None:
"""A dimmed detail line beneath a sub-step."""
console.print(f"[dim]{message}[/dim]")
def print_launch_plan(headline: str, project_dir: Path, prompt: str) -> None:
"""Where the agent will run and the prompt it gets, shown before the launch
confirmation. The prompt is collapsed to one line to fit the grid row."""
grid = Table.grid(padding=(0, 1))
grid.add_column(no_wrap=True)
grid.add_column(overflow="fold")
grid.add_row(strings.LABEL_WORKSPACE, Text(str(project_dir)))
grid.add_row(strings.LABEL_PROMPT, Text(" ".join(prompt.split())))
console.print(Group(Text(f"\n{headline}", style="bold"), Padding(grid, (0, 0, 0, 2))))
def print_error(headline: str, message: str) -> None:
"""Red headline + the raw message indented below. Built as Text, not markup,
so output containing bracketed tokens (e.g. `[WARNING]`) renders literally."""
console.print(
Group(
Text(f"\n{headline}", style="bold red"),
Padding(Text(message, style="red"), (0, 0, 0, 2)),
)
)
def print_verbatim(text: str) -> None:
"""Print ``text`` exactly: no markup interpretation, no highlighting, no hard wrapping."""
console.print(text, markup=False, highlight=False, soft_wrap=True)
@contextmanager
def substep(running: str, done: str, *, verbose: bool = False) -> Iterator[None]:
"""Spinner while a quick subprocess step runs, swapped for a ✓ line when it finishes."""
if verbose:
console.print(f"[dim]{running}…[/dim]")
yield
else:
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
transient=True,
console=console,
) as progress:
progress.add_task(running, total=None)
yield
substep_done(done)
ROWS = [
[
("", ""),
("", ""),
("", ""),
("", ""),
("", ""),
("", ""),
("", ""),
("", ""),
("", ""),
("", ""),
("", ""),
("", ""),
],
[
(" ", ""),
("█", _BOLD_CYAN),
(" ", ""),
("█", _BOLD_CYAN),
(" ", ""),
("█", _BOLD_CYAN),
(" ", ""),
("█", _BOLD_LIME),
(" ", ""),
("█", _BOLD_LIME),
(" ", ""),
("█", _BOLD_LIME),
],
[
(" ", ""),
("█", _BOLD_CYAN),
(" ", ""),
("█", _BOLD_CYAN),
(" ", ""),
("█", _BOLD_CYAN),
(" ", ""),
("█", _BOLD_LIME),
(" ", ""),
("█", _BOLD_LIME),
(" ", ""),
("█", _BOLD_LIME),
],
[
(" ", ""),
("███", _BOLD_CYAN),
(" ", ""),
("█", _BOLD_CYAN),
(" ", ""),
("██", _BOLD_CYAN),
(" ", ""),
("█", _BOLD_LIME),
(" ", ""),
("█", _BOLD_LIME),
(" ", ""),
("█", _BOLD_LIME),
(" ", ""),
("█", _BOLD_LIME),
(" ", ""),
("███", _BOLD_LIME),
],
[
(" ", ""),
("█", _BOLD_CYAN),
(" ", ""),
("█", _BOLD_CYAN),
(" ", ""),
("█", _BOLD_CYAN),
(" ", ""),
("█", _BOLD_CYAN),
(" ", ""),
("████", _BOLD_LIME),
(" ", ""),
("█", _BOLD_LIME),
(" ", ""),
("█", _BOLD_LIME),
(" ", ""),
("█", _BOLD_LIME),
(" ", ""),
("█", _BOLD_LIME),
],
[
(" ", ""),
("█", _BOLD_CYAN),
(" ", ""),
("█", _BOLD_CYAN),
(" ", ""),
("█", _BOLD_CYAN),
(" ", ""),
("█", _BOLD_CYAN),
(" ", ""),
("█", _BOLD_LIME),
(" ", ""),
("█", _BOLD_LIME),
(" ", ""),
("█", _BOLD_LIME),
(" ", ""),
("█", _BOLD_LIME),
(" ", ""),
("█", _BOLD_LIME),
(" ", ""),
("█", _BOLD_LIME),
],
[
(" ", ""),
("█", _BOLD_CYAN),
(" ", ""),
("█", _BOLD_CYAN),
(" ", ""),
("█", _BOLD_CYAN),
(" ", ""),
("█", _BOLD_CYAN),
(" ", ""),
("█", _BOLD_LIME),
(" ", ""),
("█", _BOLD_LIME),
(" ", ""),
("█", _BOLD_LIME),
(" ", ""),
("█", _BOLD_LIME),
(" ", ""),
("█", _BOLD_LIME),
(" ", ""),
("█", _BOLD_LIME),
],
[
(" ", ""),
("███", _BOLD_CYAN),
(" ", ""),
("█", _BOLD_CYAN),
(" ", ""),
("█", _BOLD_LIME),
(" ", ""),
("█", _BOLD_LIME),
(" ", ""),
("█", _BOLD_LIME),
(" ", ""),
("███", _BOLD_LIME),
(" ", ""),
("███", _BOLD_LIME),
],
]
# Widen each run to offset the ~2:1 tall-to-wide terminal cell ratio.
_LOGO_WIDTH_SCALE = 2
def _build_logo() -> Text:
logo = Text()
for row in ROWS:
for text, style in row:
logo.append(text * _LOGO_WIDTH_SCALE, style=style)
logo.append("\n")
logo.append(f"\n {strings.HINT_BANNER_TAGLINE}", style="dim")
return logo
def print_banner() -> None:
title = Text.from_markup(strings.TITLE_BANNER.format(version=VERSION))
console.print()
console.print(
Panel(
_build_logo(),
title=title,
title_align="left",
border_style=COLOR_CYAN,
padding=(1, 2),
)
)
def copy_to_clipboard(text: str) -> bool:
"""Best-effort copy ``text`` to the system clipboard. Returns True on success.
Tries the platform's clipboard tool and silently no-ops (returns False) when
none is available or the copy fails — it's a convenience, never required.
"""
if sys.platform == "darwin":
candidates = [["pbcopy"]]
elif sys.platform == "win32":
candidates = [["clip"]]
else:
candidates = [["wl-copy"], ["xclip", "-selection", "clipboard"], ["xsel", "--clipboard", "--input"]]
for cmd in candidates:
if shutil.which(cmd[0]) is None:
continue
try:
subprocess.run(cmd, input=text.encode("utf-8"), check=True, capture_output=True)
return True
except (OSError, subprocess.SubprocessError):
continue
return False
def _cd_target(project_dir: Path) -> str:
"""Path for the `cd` step. Relative to the cwd the user ran from when the
workspace sits under it (so the command is short and copy-pasteable);
absolute otherwise (different parent, or a different Windows drive)."""
try:
relative = Path(os.path.relpath(project_dir))
except ValueError:
return str(project_dir)
if os.pardir in relative.parts:
return str(project_dir)
return str(relative)
def print_created_tree(scaffold: str) -> None:
"""List the files the scaffold dropped, printed right after creation."""
entries = CREATED_TREE[scaffold]
for index, entry in enumerate(entries):
branch = "`-- " if index == len(entries) - 1 else "|-- "
console.print(f"[dim]{branch}{entry}[/dim]")
def _print_steps_panel(body: Text, *, title: str) -> None:
console.print(
Panel(
body,
title=title,
title_align="left",
border_style=COLOR_LIME,
padding=(1, 2),
)
)
def print_next_steps(
project_dir: Path,
*,
scaffold: str,
agent_prompt: str | None = None,
headline: str = strings.TITLE_ALL_SET,
needs_uv_install: bool = False,
needs_deps: bool = False,
prompt_copied: bool = False,
) -> None:
"""The agent hand-off prompt when ``agent_prompt`` is set, else any remaining setup
commands followed by the steps to run the sample pipeline.
The hand-off prompt prints without a panel: box borders would be dragged
into a manual selection when the clipboard copy isn't available."""
if agent_prompt is not None:
console.print(Text(f"\n{headline}", style=_BOLD_LIME))
console.print(Text(strings.STEPS_LABEL_HANDOFF.format(project_dir=project_dir)))
console.print()
console.print(Text(agent_prompt, style=_BOLD_CYAN), soft_wrap=True)
console.print()
if prompt_copied:
console.print(Text(strings.HINT_PROMPT_COPIED, style=_BOLD_LIME))
docs = Text(f"{strings.LABEL_DOCS} ", style="dim")
docs.append(strings.LINK_DOCS_LABEL, style=f"underline {COLOR_CYAN} link {strings.LINK_DOCS_URL}")
console.print(docs)
return
body = Text()
cd = _cd_target(project_dir)
cd_step: tuple[tuple[str, str | None], ...] = (
() if cd == "." else ((strings.STEPS_LABEL_CD, strings.CMD_CD.format(project_dir=cd)),)
)
sections: list[tuple[str, tuple[tuple[str, str | None], ...]]] = []
if needs_uv_install or needs_deps:
finish: list[tuple[str, str | None]] = [*cd_step]
if needs_uv_install:
finish.append((strings.STEPS_LABEL_INSTALL_UV, strings.CMD_INSTALL_UV_UNIX))
if needs_deps:
finish.append((strings.STEPS_LABEL_INSTALL_DEPS, strings.CMD_UV_SYNC))
sections.append((strings.LABEL_FINISH_SETUP, tuple(finish)))
sections.append((strings.LABEL_WHAT_TO_TRY, NEXT_STEPS[scaffold]))
else:
sections.append((strings.LABEL_WHAT_TO_TRY, (*cd_step, *NEXT_STEPS[scaffold])))
step = 1
for index, (header, steps) in enumerate(sections):
if index:
body.append("\n")
body.append(f"{header}\n\n", style=_BOLD_LIME)
for label, command in steps:
body.append(f" {step}. {label}\n", style="dim")
if command is not None:
body.append(f" {command}\n", style=_BOLD_CYAN)
body.append("\n")
step += 1
if cd != ".":
body.append(f" {strings.MSG_AGENT_WORKSPACE_NOTE}", style="dim")
_print_steps_panel(body, title=strings.TITLE_ALMOST_THERE)
def print_dir_not_empty(project_dir: Path) -> None:
"""Render the directory-not-empty response as a clean panel (not a raw error)."""
body = Text.from_markup(strings.MSG_DIR_NOT_EMPTY.format(project_dir=project_dir))
console.print(
Panel(
body,
title=strings.TITLE_DIR_NOT_EMPTY,
title_align="left",
border_style=COLOR_AMBER,
padding=(1, 2),
)
)