|
15 | 15 | from enum import Enum
|
16 | 16 | from dataclasses import dataclass
|
17 | 17 | from rich.text import Text
|
18 |
| -from rich.console import Console |
| 18 | +from rich.tree import Tree |
19 | 19 |
|
20 | 20 | from . import _engine
|
21 | 21 | from . import index
|
@@ -462,61 +462,33 @@ def _lazy_engine_flow() -> _engine.Flow:
|
462 | 462 | return engine_flow
|
463 | 463 | self._lazy_engine_flow = _lazy_engine_flow
|
464 | 464 |
|
465 |
| - def _format_flow(self, flow_dict: dict) -> Text: |
466 |
| - output = Text() |
| 465 | + def _render_spec(self, verbose: bool = False) -> Tree: |
| 466 | + """ |
| 467 | + Render the flow spec as a styled rich Tree with hierarchical structure. |
| 468 | + """ |
| 469 | + spec = self._get_spec(verbose=verbose) |
| 470 | + tree = Tree(f"Flow: {self.name}", style="cyan") |
467 | 471 |
|
468 |
| - def add_line(content, indent=0, style=None, end="\n"): |
469 |
| - output.append(" " * indent) |
470 |
| - output.append(content, style=style) |
471 |
| - output.append(end) |
| 472 | + def build_tree(label: str, lines: list): |
| 473 | + node = Tree(label, style="bold magenta" if lines else "cyan") |
| 474 | + for line in lines: |
| 475 | + child_node = node.add(Text(line.content, style="yellow")) |
| 476 | + child_node.children = build_tree("", line.children).children |
| 477 | + return node |
472 | 478 |
|
473 |
| - def format_key_value(key, value, indent): |
474 |
| - if isinstance(value, (dict, list)): |
475 |
| - add_line(f"- {key}:", indent, style="green") |
476 |
| - format_data(value, indent + 2) |
477 |
| - else: |
478 |
| - add_line(f"- {key}:", indent, style="green", end="") |
479 |
| - add_line(f" {value}", style="yellow") |
480 |
| - |
481 |
| - def format_data(data, indent=0): |
482 |
| - if isinstance(data, dict): |
483 |
| - for key, value in data.items(): |
484 |
| - format_key_value(key, value, indent) |
485 |
| - elif isinstance(data, list): |
486 |
| - for i, item in enumerate(data): |
487 |
| - format_key_value(f"[{i}]", item, indent) |
488 |
| - else: |
489 |
| - add_line(str(data), indent, style="yellow") |
490 |
| - |
491 |
| - # Header |
492 |
| - flow_name = flow_dict.get("name", "Unnamed") |
493 |
| - add_line(f"Flow: {flow_name}", style="bold cyan") |
494 |
| - |
495 |
| - # Section |
496 |
| - for section_title, section_key in [ |
497 |
| - ("Sources:", "import_ops"), |
498 |
| - ("Processing:", "reactive_ops"), |
499 |
| - ("Targets:", "export_ops"), |
500 |
| - ]: |
501 |
| - add_line("") |
502 |
| - add_line(section_title, style="bold cyan") |
503 |
| - format_data(flow_dict.get(section_key, []), indent=0) |
504 |
| - |
505 |
| - return output |
506 |
| - |
507 |
| - def _render_text(self) -> Text: |
508 |
| - flow_spec_str = str(self._lazy_engine_flow()) |
509 |
| - try: |
510 |
| - flow_dict = json.loads(flow_spec_str) |
511 |
| - return self._format_flow(flow_dict) |
512 |
| - except json.JSONDecodeError: |
513 |
| - return Text(flow_spec_str) |
| 479 | + for section, lines in spec.sections: |
| 480 | + section_node = build_tree(f"{section}:", lines) |
| 481 | + tree.children.append(section_node) |
| 482 | + return tree |
| 483 | + |
| 484 | + def _get_spec(self, verbose: bool = False) -> list[tuple[str, str, int]]: |
| 485 | + return self._lazy_engine_flow().get_spec(output_mode="verbose" if verbose else "concise") |
514 | 486 |
|
515 |
| - def _render_schema(self) -> list[tuple[str, str, str]]: |
| 487 | + def _get_schema(self) -> list[tuple[str, str, str]]: |
516 | 488 | return self._lazy_engine_flow().get_schema()
|
517 | 489 |
|
518 | 490 | def __str__(self):
|
519 |
| - return str(self._render_text()) |
| 491 | + return str(self._get_spec()) |
520 | 492 |
|
521 | 493 | def __repr__(self):
|
522 | 494 | return repr(self._lazy_engine_flow())
|
|
0 commit comments