template_display.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 = (
  43. template.schema_version if hasattr(template, "schema_version") else "1.0"
  44. )
  45. description = template.metadata.description or settings.TEXT_NO_DESCRIPTION
  46. # Get library information and format with helper
  47. library_name = template.metadata.library or ""
  48. library_type = template.metadata.library_type or "git"
  49. library_display = self.parent._format_library_display(
  50. library_name, library_type
  51. )
  52. console.print(
  53. f"[{settings.STYLE_HEADER}]{template_name} ({template_id} - [cyan]{version}[/cyan] - [magenta]schema {schema}[/magenta]) {library_display}[/{settings.STYLE_HEADER}]"
  54. )
  55. console.print(description)
  56. def render_file_tree(self, template: "Template") -> None:
  57. """Display the file structure of a template.
  58. Args:
  59. template: Template instance
  60. """
  61. from . import IconManager
  62. settings = self.parent.settings
  63. console.print()
  64. console.print(
  65. f"[{settings.STYLE_HEADER}]Template File Structure:[/{settings.STYLE_HEADER}]"
  66. )
  67. def get_template_file_info(template_file):
  68. display_name = (
  69. template_file.output_path.name
  70. if hasattr(template_file, "output_path")
  71. else template_file.relative_path.name
  72. )
  73. return (template_file.relative_path, display_name, "white", None)
  74. file_tree = self.parent._render_file_tree_internal(
  75. f"{IconManager.folder()} [white]{template.id}[/white]",
  76. template.template_files,
  77. get_template_file_info,
  78. )
  79. if file_tree.children:
  80. console.print(file_tree)
  81. def render_file_generation_confirmation(
  82. self,
  83. output_dir: Path,
  84. files: dict[str, str],
  85. existing_files: list[Path] | None = None,
  86. ) -> None:
  87. """Display files to be generated with confirmation prompt.
  88. Args:
  89. output_dir: Output directory path
  90. files: Dictionary of file paths to content
  91. existing_files: List of existing files that will be overwritten
  92. """
  93. from . import IconManager
  94. console.print()
  95. console.print("[bold]Files to be generated:[/bold]")
  96. def get_file_generation_info(file_path_str):
  97. file_path = Path(file_path_str)
  98. file_name = file_path.parts[-1] if file_path.parts else file_path.name
  99. full_path = output_dir / file_path
  100. if existing_files and full_path in existing_files:
  101. return (file_path, file_name, "yellow", "[red](will overwrite)[/red]")
  102. else:
  103. return (file_path, file_name, "green", None)
  104. file_tree = self.parent._render_file_tree_internal(
  105. f"{IconManager.folder()} [cyan]{output_dir.resolve()}[/cyan]",
  106. files.keys(),
  107. get_file_generation_info,
  108. )
  109. console.print(file_tree)
  110. console.print()