display_variable.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. from __future__ import annotations
  2. import logging
  3. from typing import TYPE_CHECKING
  4. from rich.table import Table
  5. from .display_icons import IconManager
  6. from .display_settings import DisplaySettings
  7. logger = logging.getLogger(__name__)
  8. if TYPE_CHECKING:
  9. from ..template import Template
  10. from .display_base import BaseDisplay
  11. class VariableDisplay:
  12. """Variable-related rendering.
  13. Provides methods for displaying variables, sections,
  14. and their values with appropriate formatting based on context.
  15. """
  16. def __init__(self, settings: DisplaySettings, base: BaseDisplay):
  17. """Initialize VariableDisplay.
  18. Args:
  19. settings: Display settings for formatting
  20. base: BaseDisplay instance
  21. """
  22. self.settings = settings
  23. self.base = base
  24. def render_variable_value(
  25. self,
  26. variable,
  27. _context: str = "default",
  28. is_dimmed: bool = False,
  29. var_satisfied: bool = True,
  30. ) -> str:
  31. """Render variable value with appropriate formatting based on context.
  32. Args:
  33. variable: Variable instance to render
  34. _context: Display context (unused, kept for API compatibility)
  35. is_dimmed: Whether the variable should be dimmed
  36. var_satisfied: Whether the variable's dependencies are satisfied
  37. Returns:
  38. Formatted string representation of the variable value
  39. """
  40. # Handle disabled bool variables
  41. if (is_dimmed or not var_satisfied) and variable.type == "bool":
  42. if hasattr(variable, "_original_disabled") and variable._original_disabled is not False:
  43. return f"{variable._original_disabled} {IconManager.arrow_right()} False"
  44. return "False"
  45. # Handle config overrides with arrow
  46. if (
  47. variable.origin == "config"
  48. and hasattr(variable, "_original_stored")
  49. and variable.original_value != variable.value
  50. ):
  51. settings = self.settings
  52. orig = self._format_value(
  53. variable,
  54. variable.original_value,
  55. max_length=settings.VALUE_MAX_LENGTH_SHORT,
  56. )
  57. curr = variable.get_display_value(
  58. mask_sensitive=True,
  59. max_length=settings.VALUE_MAX_LENGTH_SHORT,
  60. show_none=False,
  61. )
  62. if not curr:
  63. curr = str(variable.value) if variable.value else settings.TEXT_EMPTY_OVERRIDE
  64. arrow = IconManager.arrow_right()
  65. color = settings.COLOR_WARNING
  66. return f"[dim]{orig}[/dim] [bold {color}]{arrow} {curr}[/bold {color}]"
  67. # Default formatting
  68. settings = self.settings
  69. value = variable.get_display_value(
  70. mask_sensitive=True,
  71. max_length=settings.VALUE_MAX_LENGTH_DEFAULT,
  72. show_none=True,
  73. )
  74. if not variable.value:
  75. return f"[{settings.COLOR_MUTED}]{value}[/{settings.COLOR_MUTED}]"
  76. return value
  77. def _format_value(self, variable, value, max_length: int | None = None) -> str:
  78. """Helper to format a specific value.
  79. Args:
  80. variable: Variable instance
  81. value: Value to format
  82. max_length: Maximum length before truncation
  83. Returns:
  84. Formatted value string
  85. """
  86. settings = self.settings
  87. if variable.sensitive:
  88. return settings.SENSITIVE_MASK
  89. if value is None or value == "":
  90. return f"[{settings.COLOR_MUTED}]({settings.TEXT_EMPTY_VALUE})[/{settings.COLOR_MUTED}]"
  91. val_str = str(value)
  92. return self.base.truncate(val_str, max_length)
  93. def render_section(self, title: str, description: str | None) -> None:
  94. """Display a section header.
  95. Args:
  96. title: Section title
  97. description: Optional section description
  98. """
  99. settings = self.settings
  100. if description:
  101. self.base.text(
  102. f"\n{title} - {description}",
  103. style=f"{settings.STYLE_SECTION_TITLE} {settings.STYLE_SECTION_DESC}",
  104. )
  105. else:
  106. self.base.text(f"\n{title}", style=settings.STYLE_SECTION_TITLE)
  107. self.base.text(
  108. settings.SECTION_SEPARATOR_CHAR * settings.SECTION_SEPARATOR_LENGTH,
  109. style=settings.COLOR_MUTED,
  110. )
  111. def _render_section_header(self, section, is_dimmed: bool) -> str:
  112. """Build section header text with appropriate styling.
  113. Args:
  114. section: VariableSection instance
  115. is_dimmed: Whether section is dimmed (disabled)
  116. Returns:
  117. Formatted header text with Rich markup
  118. """
  119. settings = self.settings
  120. # Show (disabled) label if section has a toggle and is not enabled
  121. disabled_text = settings.LABEL_DISABLED if (section.toggle and not section.is_enabled()) else ""
  122. if is_dimmed:
  123. style = settings.STYLE_DISABLED
  124. return f"[bold {style}]{section.title}{disabled_text}[/bold {style}]"
  125. return f"[bold]{section.title}{disabled_text}[/bold]"
  126. def _render_variable_row(self, var_name: str, variable, is_dimmed: bool, var_satisfied: bool) -> tuple:
  127. """Build variable row data for table display.
  128. Args:
  129. var_name: Variable name
  130. variable: Variable instance
  131. is_dimmed: Whether containing section is dimmed
  132. var_satisfied: Whether variable dependencies are satisfied
  133. Returns:
  134. Tuple of (var_display, type, default_val, description, row_style)
  135. """
  136. settings = self.settings
  137. # Build row style
  138. row_style = settings.STYLE_DISABLED if (is_dimmed or not var_satisfied) else None
  139. # Build default value
  140. default_val = self.render_variable_value(variable, is_dimmed=is_dimmed, var_satisfied=var_satisfied)
  141. # Build variable display name
  142. sensitive_icon = f" {IconManager.lock()}" if variable.sensitive else ""
  143. # Only show required indicator if variable is enabled (not dimmed and dependencies satisfied)
  144. required_indicator = settings.LABEL_REQUIRED if variable.required and not is_dimmed and var_satisfied else ""
  145. var_display = f"{settings.VAR_NAME_INDENT}{var_name}{sensitive_icon}{required_indicator}"
  146. return (
  147. var_display,
  148. variable.type or "str",
  149. default_val,
  150. variable.description or "",
  151. row_style,
  152. )
  153. def render_variables_table(self, template: Template) -> None:
  154. """Display a table of variables for a template.
  155. All variables and sections are always shown. Disabled sections/variables
  156. are displayed with dimmed styling.
  157. Args:
  158. template: Template instance
  159. """
  160. if not (template.variables and template.variables.has_sections()):
  161. return
  162. settings = self.settings
  163. self.base.text("")
  164. self.base.heading("Template Variables")
  165. variables_table = Table(show_header=True, header_style=settings.STYLE_TABLE_HEADER)
  166. variables_table.add_column("Variable", style=settings.STYLE_VAR_COL_NAME, no_wrap=True)
  167. variables_table.add_column("Type", style=settings.STYLE_VAR_COL_TYPE)
  168. variables_table.add_column("Default", style=settings.STYLE_VAR_COL_DEFAULT)
  169. variables_table.add_column("Description", style=settings.STYLE_VAR_COL_DESC)
  170. first_section = True
  171. for section in template.variables.get_sections().values():
  172. if not section.variables:
  173. continue
  174. if not first_section:
  175. variables_table.add_row("", "", "", "", style=settings.STYLE_DISABLED)
  176. first_section = False
  177. # Check if section is enabled AND dependencies are satisfied
  178. is_enabled = section.is_enabled()
  179. dependencies_satisfied = template.variables.is_section_satisfied(section.key)
  180. is_dimmed = not (is_enabled and dependencies_satisfied)
  181. # Render section header
  182. header_text = self._render_section_header(section, is_dimmed)
  183. variables_table.add_row(header_text, "", "", "")
  184. # Render variables
  185. for var_name, variable in section.variables.items():
  186. # Check if variable's needs are satisfied
  187. var_satisfied = template.variables.is_variable_satisfied(var_name)
  188. # Build and add row
  189. (
  190. var_display,
  191. var_type,
  192. default_val,
  193. description,
  194. row_style,
  195. ) = self._render_variable_row(var_name, variable, is_dimmed, var_satisfied)
  196. variables_table.add_row(var_display, var_type, default_val, description, style=row_style)
  197. self.base._print_table(variables_table)