template_display.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. from __future__ import annotations
  2. from pathlib import Path
  3. from typing import TYPE_CHECKING
  4. from rich.console import Console
  5. if TYPE_CHECKING:
  6. from . import DisplayManager
  7. from ..template import Template
  8. console = Console()
  9. class TemplateDisplayManager:
  10. """Handles all template-related rendering.
  11. This manager is responsible for displaying template information,
  12. file trees, and metadata.
  13. """
  14. def __init__(self, parent: "DisplayManager"):
  15. """Initialize TemplateDisplayManager.
  16. Args:
  17. parent: Reference to parent DisplayManager for accessing shared resources
  18. """
  19. self.parent = parent
  20. def render_template(self, template: "Template", template_id: str) -> None:
  21. """Display template information panel and variables table.
  22. Args:
  23. template: Template instance to display
  24. template_id: ID of the template
  25. """
  26. self.render_template_header(template, template_id)
  27. self.render_file_tree(template)
  28. self.parent.variables.render_variables_table(template)
  29. def render_template_header(self, template: "Template", template_id: str) -> None:
  30. """Display the header for a template with library information.
  31. Args:
  32. template: Template instance
  33. template_id: ID of the template
  34. """
  35. settings = self.parent.settings
  36. template_name = template.metadata.name or settings.TEXT_UNNAMED_TEMPLATE
  37. version = (
  38. str(template.metadata.version)
  39. if template.metadata.version
  40. else settings.TEXT_VERSION_NOT_SPECIFIED
  41. )
  42. schema = template.schema_version if hasattr(template, "schema_version") else "1.0"
  43. description = template.metadata.description or settings.TEXT_NO_DESCRIPTION
  44. # Get library information and format with helper
  45. library_name = template.metadata.library or ""
  46. library_type = template.metadata.library_type or "git"
  47. library_display = self.parent._format_library_display(library_name, library_type)
  48. console.print(
  49. f"[{settings.STYLE_HEADER}]{template_name} ({template_id} - [cyan]{version}[/cyan] - [magenta]schema {schema}[/magenta]) {library_display}[/{settings.STYLE_HEADER}]"
  50. )
  51. console.print(description)
  52. def render_file_tree(self, template: "Template") -> None:
  53. """Display the file structure of a template.
  54. Args:
  55. template: Template instance
  56. """
  57. from . import IconManager
  58. settings = self.parent.settings
  59. console.print()
  60. console.print(f"[{settings.STYLE_HEADER}]Template File Structure:[/{settings.STYLE_HEADER}]")
  61. def get_template_file_info(template_file):
  62. display_name = (
  63. template_file.output_path.name
  64. if hasattr(template_file, "output_path")
  65. else template_file.relative_path.name
  66. )
  67. return (template_file.relative_path, display_name, "white", None)
  68. file_tree = self.parent._render_file_tree_internal(
  69. f"{IconManager.folder()} [white]{template.id}[/white]",
  70. template.template_files,
  71. get_template_file_info,
  72. )
  73. if file_tree.children:
  74. console.print(file_tree)
  75. def render_file_generation_confirmation(
  76. self,
  77. output_dir: Path,
  78. files: dict[str, str],
  79. existing_files: list[Path] | None = None,
  80. ) -> None:
  81. """Display files to be generated with confirmation prompt.
  82. Args:
  83. output_dir: Output directory path
  84. files: Dictionary of file paths to content
  85. existing_files: List of existing files that will be overwritten
  86. """
  87. from . import IconManager
  88. console.print()
  89. console.print("[bold]Files to be generated:[/bold]")
  90. def get_file_generation_info(file_path_str):
  91. file_path = Path(file_path_str)
  92. file_name = file_path.parts[-1] if file_path.parts else file_path.name
  93. full_path = output_dir / file_path
  94. if existing_files and full_path in existing_files:
  95. return (file_path, file_name, "yellow", "[red](will overwrite)[/red]")
  96. else:
  97. return (file_path, file_name, "green", None)
  98. file_tree = self.parent._render_file_tree_internal(
  99. f"{IconManager.folder()} [cyan]{output_dir.resolve()}[/cyan]",
  100. files.keys(),
  101. get_file_generation_info,
  102. )
  103. console.print(file_tree)
  104. console.print()