display_variable.py 8.8 KB

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