table_display.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. from __future__ import annotations
  2. import logging
  3. from typing import TYPE_CHECKING
  4. from rich.console import Console
  5. from rich.table import Table
  6. from rich.tree import Tree
  7. if TYPE_CHECKING:
  8. from . import DisplayManager
  9. logger = logging.getLogger(__name__)
  10. console = Console()
  11. class TableDisplayManager:
  12. """Handles table rendering.
  13. This manager is responsible for displaying various types of tables
  14. including templates lists, status tables, and summaries.
  15. """
  16. def __init__(self, parent: "DisplayManager"):
  17. """Initialize TableDisplayManager.
  18. Args:
  19. parent: Reference to parent DisplayManager for accessing shared resources
  20. """
  21. self.parent = parent
  22. def render_templates_table(
  23. self, templates: list, module_name: str, title: str
  24. ) -> None:
  25. """Display a table of templates with library type indicators.
  26. Args:
  27. templates: List of Template objects
  28. module_name: Name of the module
  29. title: Title for the table
  30. """
  31. if not templates:
  32. logger.info(f"No templates found for module '{module_name}'")
  33. return
  34. logger.info(f"Listing {len(templates)} templates for module '{module_name}'")
  35. table = Table(title=title)
  36. table.add_column("ID", style="bold", no_wrap=True)
  37. table.add_column("Name")
  38. table.add_column("Tags")
  39. table.add_column("Version", no_wrap=True)
  40. table.add_column("Schema", no_wrap=True)
  41. table.add_column("Library", no_wrap=True)
  42. settings = self.parent.settings
  43. for template in templates:
  44. name = template.metadata.name or settings.TEXT_UNNAMED_TEMPLATE
  45. tags_list = template.metadata.tags or []
  46. tags = ", ".join(tags_list) if tags_list else "-"
  47. version = str(template.metadata.version) if template.metadata.version else ""
  48. schema = template.schema_version if hasattr(template, "schema_version") else "1.0"
  49. # Use helper for library display
  50. library_name = template.metadata.library or ""
  51. library_type = template.metadata.library_type or "git"
  52. library_display = self.parent._format_library_display(library_name, library_type)
  53. table.add_row(template.id, name, tags, version, schema, library_display)
  54. console.print(table)
  55. def render_status_table(
  56. self,
  57. title: str,
  58. rows: list[tuple[str, str, bool]],
  59. columns: tuple[str, str] = ("Item", "Status"),
  60. ) -> None:
  61. """Display a status table with success/error indicators.
  62. Args:
  63. title: Table title
  64. rows: List of tuples (name, message, success_bool)
  65. columns: Column headers (name_header, status_header)
  66. """
  67. from . import IconManager
  68. table = Table(title=title, show_header=True)
  69. table.add_column(columns[0], style="cyan", no_wrap=True)
  70. table.add_column(columns[1])
  71. for name, message, success in rows:
  72. status_style = "green" if success else "red"
  73. status_icon = IconManager.get_status_icon(
  74. "success" if success else "error"
  75. )
  76. table.add_row(
  77. name, f"[{status_style}]{status_icon} {message}[/{status_style}]"
  78. )
  79. console.print(table)
  80. def render_summary_table(self, title: str, items: dict[str, str]) -> None:
  81. """Display a simple two-column summary table.
  82. Args:
  83. title: Table title
  84. items: Dictionary of key-value pairs to display
  85. """
  86. settings = self.parent.settings
  87. table = Table(title=title, show_header=False, box=None, padding=settings.PADDING_TABLE_NORMAL)
  88. table.add_column(style="bold")
  89. table.add_column()
  90. for key, value in items.items():
  91. table.add_row(key, value)
  92. console.print(table)
  93. def render_file_operation_table(
  94. self, files: list[tuple[str, int, str]]
  95. ) -> None:
  96. """Display a table of file operations with sizes and statuses.
  97. Args:
  98. files: List of tuples (file_path, size_bytes, status)
  99. """
  100. settings = self.parent.settings
  101. table = Table(
  102. show_header=True, header_style=settings.STYLE_HEADER_ALT, box=None, padding=settings.PADDING_TABLE_COMPACT
  103. )
  104. table.add_column("File", style="white", no_wrap=False)
  105. table.add_column("Size", justify="right", style=settings.COLOR_MUTED)
  106. table.add_column("Status", style=settings.COLOR_WARNING)
  107. for file_path, size_bytes, status in files:
  108. size_str = self.parent._format_file_size(size_bytes)
  109. table.add_row(str(file_path), size_str, status)
  110. console.print(table)
  111. def render_config_tree(
  112. self, spec: dict, module_name: str, show_all: bool = False
  113. ) -> None:
  114. """Display configuration spec as a tree view.
  115. Args:
  116. spec: The configuration spec dictionary
  117. module_name: Name of the module
  118. show_all: If True, show all details including descriptions
  119. """
  120. from . import IconManager
  121. if not spec:
  122. console.print(
  123. f"[yellow]No configuration found for module '{module_name}'[/yellow]"
  124. )
  125. return
  126. # Create root tree node
  127. tree = Tree(
  128. f"[bold blue]{IconManager.config()} {str.capitalize(module_name)} Configuration[/bold blue]"
  129. )
  130. for section_name, section_data in spec.items():
  131. if not isinstance(section_data, dict):
  132. continue
  133. # Determine if this is a section with variables
  134. section_vars = section_data.get("vars") or {}
  135. section_desc = section_data.get("description", "")
  136. section_required = section_data.get("required", False)
  137. section_toggle = section_data.get("toggle", None)
  138. section_needs = section_data.get("needs", None)
  139. # Build section label
  140. section_label = f"[cyan]{section_name}[/cyan]"
  141. if section_required:
  142. section_label += " [yellow](required)[/yellow]"
  143. if section_toggle:
  144. section_label += f" [dim](toggle: {section_toggle})[/dim]"
  145. if section_needs:
  146. needs_str = (
  147. ", ".join(section_needs)
  148. if isinstance(section_needs, list)
  149. else section_needs
  150. )
  151. section_label += f" [dim](needs: {needs_str})[/dim]"
  152. if show_all and section_desc:
  153. section_label += f"\n [dim]{section_desc}[/dim]"
  154. section_node = tree.add(section_label)
  155. # Add variables
  156. if section_vars:
  157. for var_name, var_data in section_vars.items():
  158. if isinstance(var_data, dict):
  159. var_type = var_data.get("type", "string")
  160. var_default = var_data.get("default", "")
  161. var_desc = var_data.get("description", "")
  162. var_sensitive = var_data.get("sensitive", False)
  163. # Build variable label
  164. var_label = (
  165. f"[green]{var_name}[/green] [dim]({var_type})[/dim]"
  166. )
  167. if var_default is not None and var_default != "":
  168. settings = self.parent.settings
  169. display_val = settings.SENSITIVE_MASK if var_sensitive else str(var_default)
  170. if not var_sensitive:
  171. display_val = self.parent._truncate_value(display_val, settings.VALUE_MAX_LENGTH_DEFAULT)
  172. var_label += f" = [{settings.COLOR_WARNING}]{display_val}[/{settings.COLOR_WARNING}]"
  173. if show_all and var_desc:
  174. var_label += f"\n [dim]{var_desc}[/dim]"
  175. section_node.add(var_label)
  176. else:
  177. # Simple key-value pair
  178. section_node.add(
  179. f"[green]{var_name}[/green] = [yellow]{var_data}[/yellow]"
  180. )
  181. console.print(tree)