template_display.py 4.4 KB

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