display_template.py 4.7 KB

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