table_display.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. from .icon_manager import IconManager
  7. if TYPE_CHECKING:
  8. from . import DisplayManager
  9. logger = logging.getLogger(__name__)
  10. class TableDisplayManager:
  11. """Handles table rendering.
  12. This manager is responsible for displaying various types of tables
  13. including templates lists, status tables, and summaries.
  14. """
  15. def __init__(self, parent: DisplayManager):
  16. """Initialize TableDisplayManager.
  17. Args:
  18. parent: Reference to parent DisplayManager for accessing shared resources
  19. """
  20. self.parent = parent
  21. def render_templates_table(
  22. self, templates: list, module_name: str, title: str
  23. ) -> None:
  24. """Display a table of templates with library type indicators.
  25. Args:
  26. templates: List of Template objects
  27. module_name: Name of the module
  28. title: Title for the table
  29. """
  30. if not templates:
  31. logger.info(f"No templates found for module '{module_name}'")
  32. return
  33. logger.info(f"Listing {len(templates)} templates for module '{module_name}'")
  34. table = Table()
  35. table.add_column("ID", style="bold", no_wrap=True)
  36. table.add_column("Name")
  37. table.add_column("Tags")
  38. table.add_column("Version", no_wrap=True)
  39. table.add_column("Schema", no_wrap=True)
  40. table.add_column("Library", no_wrap=True)
  41. settings = self.parent.settings
  42. for template in templates:
  43. name = template.metadata.name or settings.TEXT_UNNAMED_TEMPLATE
  44. tags_list = template.metadata.tags or []
  45. tags = ", ".join(tags_list) if tags_list else "-"
  46. version = (
  47. str(template.metadata.version) if template.metadata.version else ""
  48. )
  49. schema = (
  50. template.schema_version
  51. if hasattr(template, "schema_version")
  52. else "1.0"
  53. )
  54. # Use helper for library display
  55. library_name = template.metadata.library or ""
  56. library_type = template.metadata.library_type or "git"
  57. library_display = self.parent._format_library_display(
  58. library_name, library_type
  59. )
  60. table.add_row(template.id, name, tags, version, schema, library_display)
  61. self.parent._print_table(table)
  62. def render_status_table(
  63. self,
  64. title: str,
  65. rows: list[tuple[str, str, bool]],
  66. columns: tuple[str, str] = ("Item", "Status"),
  67. ) -> None:
  68. """Display a status table with success/error indicators.
  69. Args:
  70. title: Table title
  71. rows: List of tuples (name, message, success_bool)
  72. columns: Column headers (name_header, status_header)
  73. """
  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. if not spec:
  131. self.parent.text(
  132. f"No configuration found for module '{module_name}'", style="yellow"
  133. )
  134. return
  135. # Create root tree node
  136. tree = Tree(
  137. f"[bold blue]{IconManager.config()} {str.capitalize(module_name)} Configuration[/bold blue]"
  138. )
  139. for section_name, section_data in spec.items():
  140. if not isinstance(section_data, dict):
  141. continue
  142. # Determine if this is a section with variables
  143. section_vars = section_data.get("vars") or {}
  144. section_desc = section_data.get("description", "")
  145. section_required = section_data.get("required", False)
  146. section_toggle = section_data.get("toggle", None)
  147. section_needs = section_data.get("needs", None)
  148. # Build section label
  149. section_label = f"[cyan]{section_name}[/cyan]"
  150. if section_required:
  151. section_label += " [yellow](required)[/yellow]"
  152. if section_toggle:
  153. section_label += f" [dim](toggle: {section_toggle})[/dim]"
  154. if section_needs:
  155. needs_str = (
  156. ", ".join(section_needs)
  157. if isinstance(section_needs, list)
  158. else section_needs
  159. )
  160. section_label += f" [dim](needs: {needs_str})[/dim]"
  161. if show_all and section_desc:
  162. section_label += f"\n [dim]{section_desc}[/dim]"
  163. section_node = tree.add(section_label)
  164. # Add variables
  165. if section_vars:
  166. for var_name, var_data in section_vars.items():
  167. if isinstance(var_data, dict):
  168. var_type = var_data.get("type", "string")
  169. var_default = var_data.get("default", "")
  170. var_desc = var_data.get("description", "")
  171. var_sensitive = var_data.get("sensitive", False)
  172. # Build variable label
  173. var_label = f"[green]{var_name}[/green] [dim]({var_type})[/dim]"
  174. if var_default is not None and var_default != "":
  175. settings = self.parent.settings
  176. display_val = (
  177. settings.SENSITIVE_MASK
  178. if var_sensitive
  179. else str(var_default)
  180. )
  181. if not var_sensitive:
  182. display_val = self.parent._truncate_value(
  183. display_val, settings.VALUE_MAX_LENGTH_DEFAULT
  184. )
  185. var_label += f" = [{settings.COLOR_WARNING}]{display_val}[/{settings.COLOR_WARNING}]"
  186. if show_all and var_desc:
  187. var_label += f"\n [dim]{var_desc}[/dim]"
  188. section_node.add(var_label)
  189. else:
  190. # Simple key-value pair
  191. section_node.add(
  192. f"[green]{var_name}[/green] = [yellow]{var_data}[/yellow]"
  193. )
  194. self.parent._print_tree(tree)