table_display.py 8.5 KB

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