|
8 | 8 | from __future__ import annotations |
9 | 9 |
|
10 | 10 | from dataclasses import dataclass |
| 11 | +from typing import Any, Optional |
11 | 12 |
|
12 | | -from .tokens import CategoryToken, EmphasisToken, HierarchyToken, StatusToken |
| 13 | +from rich.console import Console |
| 14 | +from rich.panel import Panel as RichPanel |
| 15 | +from rich.text import Text |
| 16 | + |
| 17 | +from .tokens import ( |
| 18 | + CategoryToken, |
| 19 | + DisplayMetadata, |
| 20 | + DisplayStyle, |
| 21 | + EmphasisToken, |
| 22 | + HierarchyToken, |
| 23 | + StatusToken, |
| 24 | +) |
13 | 25 |
|
14 | 26 |
|
15 | 27 | @dataclass |
@@ -50,3 +62,294 @@ class Output: |
50 | 62 | stderr_status: StatusToken = StatusToken.ERROR |
51 | 63 | debug_emphasis: EmphasisToken = EmphasisToken.SUBTLE |
52 | 64 | info_status: StatusToken = StatusToken.INFO |
| 65 | + |
| 66 | + |
| 67 | +@dataclass |
| 68 | +class ErrorDisplay: |
| 69 | + """Component for displaying error messages with design token integration. |
| 70 | +
|
| 71 | + This component provides consistent error formatting across the CLI interface |
| 72 | + using design tokens for styling. It handles error titles, messages, and |
| 73 | + suggestions with appropriate visual hierarchy. |
| 74 | + """ |
| 75 | + |
| 76 | + border_category: CategoryToken = CategoryToken.CAT_1 |
| 77 | + title_hierarchy: HierarchyToken = HierarchyToken.PRIMARY |
| 78 | + title_emphasis: EmphasisToken = EmphasisToken.STRONG |
| 79 | + content_emphasis: EmphasisToken = EmphasisToken.NORMAL |
| 80 | + error_status: StatusToken = StatusToken.ERROR |
| 81 | + suggestion_emphasis: EmphasisToken = EmphasisToken.SUBTLE |
| 82 | + |
| 83 | + def create_error_title_style( |
| 84 | + self, metadata: Optional[DisplayMetadata] |
| 85 | + ) -> DisplayStyle: |
| 86 | + """Create display style for error title. |
| 87 | +
|
| 88 | + Args: |
| 89 | + metadata: Optional display metadata for customization |
| 90 | +
|
| 91 | + Returns: |
| 92 | + DisplayStyle for error title |
| 93 | + """ |
| 94 | + return DisplayStyle( |
| 95 | + category=metadata.category if metadata else CategoryToken.CAT_1, |
| 96 | + hierarchy=self.title_hierarchy, |
| 97 | + emphasis=self.title_emphasis, |
| 98 | + status=self.error_status, |
| 99 | + ) |
| 100 | + |
| 101 | + def create_error_content_style( |
| 102 | + self, metadata: Optional[DisplayMetadata] |
| 103 | + ) -> DisplayStyle: |
| 104 | + """Create display style for error content. |
| 105 | +
|
| 106 | + Args: |
| 107 | + metadata: Optional display metadata for customization |
| 108 | +
|
| 109 | + Returns: |
| 110 | + DisplayStyle for error content |
| 111 | + """ |
| 112 | + return DisplayStyle( |
| 113 | + category=metadata.category if metadata else CategoryToken.CAT_1, |
| 114 | + hierarchy=metadata.hierarchy if metadata else HierarchyToken.PRIMARY, |
| 115 | + emphasis=self.content_emphasis, |
| 116 | + status=self.error_status, |
| 117 | + ) |
| 118 | + |
| 119 | + def create_suggestion_style( |
| 120 | + self, metadata: Optional[DisplayMetadata] |
| 121 | + ) -> DisplayStyle: |
| 122 | + """Create display style for suggestions. |
| 123 | +
|
| 124 | + Args: |
| 125 | + metadata: Optional display metadata for customization |
| 126 | +
|
| 127 | + Returns: |
| 128 | + DisplayStyle for suggestions |
| 129 | + """ |
| 130 | + return DisplayStyle( |
| 131 | + category=metadata.category if metadata else CategoryToken.CAT_1, |
| 132 | + hierarchy=metadata.hierarchy if metadata else HierarchyToken.PRIMARY, |
| 133 | + emphasis=self.suggestion_emphasis, |
| 134 | + status=StatusToken.INFO, # Suggestions use info status |
| 135 | + ) |
| 136 | + |
| 137 | + def create_border_style(self, metadata: Optional[DisplayMetadata]) -> DisplayStyle: |
| 138 | + """Create display style for border. |
| 139 | +
|
| 140 | + Args: |
| 141 | + metadata: Optional display metadata for customization |
| 142 | +
|
| 143 | + Returns: |
| 144 | + DisplayStyle for border |
| 145 | + """ |
| 146 | + return DisplayStyle( |
| 147 | + category=self.border_category, |
| 148 | + hierarchy=metadata.hierarchy if metadata else HierarchyToken.PRIMARY, |
| 149 | + emphasis=metadata.emphasis if metadata else EmphasisToken.NORMAL, |
| 150 | + status=self.error_status, |
| 151 | + ) |
| 152 | + |
| 153 | + def render_error_title( |
| 154 | + self, title_text: str, style: DisplayStyle, console: Console |
| 155 | + ) -> Text: |
| 156 | + """Render error title with styling. |
| 157 | +
|
| 158 | + Args: |
| 159 | + title_text: The error title text |
| 160 | + style: Display style to apply |
| 161 | + console: Rich console for rendering |
| 162 | +
|
| 163 | + Returns: |
| 164 | + Styled Rich Text object |
| 165 | + """ |
| 166 | + text = Text(title_text) |
| 167 | + # Apply styling based on design tokens |
| 168 | + # The actual styling would be resolved through the theme system |
| 169 | + return text |
| 170 | + |
| 171 | + def render_error_message( |
| 172 | + self, message_text: str, style: DisplayStyle, console: Console |
| 173 | + ) -> Text: |
| 174 | + """Render error message with styling. |
| 175 | +
|
| 176 | + Args: |
| 177 | + message_text: The error message text |
| 178 | + style: Display style to apply |
| 179 | + console: Rich console for rendering |
| 180 | +
|
| 181 | + Returns: |
| 182 | + Styled Rich Text object |
| 183 | + """ |
| 184 | + text = Text(message_text) |
| 185 | + # Apply styling based on design tokens |
| 186 | + # The actual styling would be resolved through the theme system |
| 187 | + return text |
| 188 | + |
| 189 | + def render_suggestions_list( |
| 190 | + self, suggestions: list[str], style: DisplayStyle, console: Console |
| 191 | + ) -> Optional[Text]: |
| 192 | + """Render suggestions list with styling. |
| 193 | +
|
| 194 | + Args: |
| 195 | + suggestions: List of suggestion strings |
| 196 | + style: Display style to apply |
| 197 | + console: Rich console for rendering |
| 198 | +
|
| 199 | + Returns: |
| 200 | + Styled Rich Text object or None for empty suggestions |
| 201 | + """ |
| 202 | + if not suggestions: |
| 203 | + return Text("") # Return empty text for empty suggestions |
| 204 | + |
| 205 | + # Format suggestions as a list |
| 206 | + text_parts = [] |
| 207 | + for suggestion in suggestions: |
| 208 | + text_parts.append(suggestion) |
| 209 | + |
| 210 | + text = Text("\n".join(text_parts)) |
| 211 | + # Apply styling based on design tokens |
| 212 | + # The actual styling would be resolved through the theme system |
| 213 | + return text |
| 214 | + |
| 215 | + def render_error_panel( |
| 216 | + self, |
| 217 | + error_data: dict[str, Any], |
| 218 | + metadata: Optional[DisplayMetadata], |
| 219 | + console: Console, |
| 220 | + ) -> RichPanel: |
| 221 | + """Render complete error panel. |
| 222 | +
|
| 223 | + Args: |
| 224 | + error_data: Dictionary containing error information |
| 225 | + metadata: Optional display metadata for styling |
| 226 | + console: Rich console for rendering |
| 227 | +
|
| 228 | + Returns: |
| 229 | + Rich Panel with complete error display |
| 230 | + """ |
| 231 | + # Create styles |
| 232 | + title_style = self.create_error_title_style(metadata) |
| 233 | + content_style = self.create_error_content_style(metadata) |
| 234 | + suggestion_style = self.create_suggestion_style(metadata) |
| 235 | + border_style = self.create_border_style(metadata) |
| 236 | + |
| 237 | + # Render components |
| 238 | + title_text = self.render_error_title( |
| 239 | + error_data.get("title", ""), title_style, console |
| 240 | + ) |
| 241 | + message_text = self.render_error_message( |
| 242 | + error_data.get("message", ""), content_style, console |
| 243 | + ) |
| 244 | + |
| 245 | + # Combine content |
| 246 | + panel_content = Text() |
| 247 | + panel_content.append_text(title_text) |
| 248 | + panel_content.append("\n\n") |
| 249 | + panel_content.append_text(message_text) |
| 250 | + |
| 251 | + # Add suggestions if present |
| 252 | + suggestions = error_data.get("suggestions", []) |
| 253 | + if suggestions: |
| 254 | + suggestions_text = self.render_suggestions_list( |
| 255 | + suggestions, suggestion_style, console |
| 256 | + ) |
| 257 | + if suggestions_text and suggestions_text.plain.strip(): |
| 258 | + panel_content.append("\n\nSuggestions:\n") |
| 259 | + panel_content.append_text(suggestions_text) |
| 260 | + |
| 261 | + # Create panel with border styling |
| 262 | + # Convert status to color for border |
| 263 | + border_color = "red" # Default error color |
| 264 | + if border_style.status == StatusToken.WARNING: |
| 265 | + border_color = "yellow" |
| 266 | + elif border_style.status == StatusToken.INFO: |
| 267 | + border_color = "blue" |
| 268 | + elif border_style.status == StatusToken.SUCCESS: |
| 269 | + border_color = "green" |
| 270 | + |
| 271 | + return RichPanel(panel_content, title="Error", border_style=border_color) |
| 272 | + |
| 273 | + def format_error_data( |
| 274 | + self, error_type: str, message: str, suggestions: Optional[list[str]] |
| 275 | + ) -> dict[str, Any]: |
| 276 | + """Format error data into structured dictionary. |
| 277 | +
|
| 278 | + Args: |
| 279 | + error_type: Type of error |
| 280 | + message: Error message |
| 281 | + suggestions: Optional list of suggestions |
| 282 | +
|
| 283 | + Returns: |
| 284 | + Formatted error data dictionary |
| 285 | + """ |
| 286 | + return { |
| 287 | + "title": error_type, |
| 288 | + "message": message, |
| 289 | + "suggestions": suggestions or [], |
| 290 | + } |
| 291 | + |
| 292 | + def truncate_text(self, text: str, max_length: int) -> str: |
| 293 | + """Truncate text to maximum length with ellipsis. |
| 294 | +
|
| 295 | + Args: |
| 296 | + text: Text to truncate |
| 297 | + max_length: Maximum length |
| 298 | +
|
| 299 | + Returns: |
| 300 | + Truncated text with ellipsis if needed |
| 301 | + """ |
| 302 | + if len(text) <= max_length: |
| 303 | + return text |
| 304 | + return text[: max_length - 3] + "..." |
| 305 | + |
| 306 | + def wrap_text(self, text: str, width: int) -> list[str]: |
| 307 | + """Wrap text to specified width. |
| 308 | +
|
| 309 | + Args: |
| 310 | + text: Text to wrap |
| 311 | + width: Maximum line width |
| 312 | +
|
| 313 | + Returns: |
| 314 | + List of wrapped lines |
| 315 | + """ |
| 316 | + lines = text.split("\n") |
| 317 | + wrapped_lines = [] |
| 318 | + |
| 319 | + for line in lines: |
| 320 | + if len(line) <= width: |
| 321 | + wrapped_lines.append(line) |
| 322 | + else: |
| 323 | + # Simple word wrapping |
| 324 | + words = line.split(" ") |
| 325 | + current_line = "" |
| 326 | + |
| 327 | + for word in words: |
| 328 | + if len(current_line + " " + word) <= width: |
| 329 | + if current_line: |
| 330 | + current_line += " " + word |
| 331 | + else: |
| 332 | + current_line = word |
| 333 | + else: |
| 334 | + if current_line: |
| 335 | + wrapped_lines.append(current_line) |
| 336 | + current_line = word |
| 337 | + |
| 338 | + if current_line: |
| 339 | + wrapped_lines.append(current_line) |
| 340 | + |
| 341 | + return wrapped_lines |
| 342 | + |
| 343 | + def sanitize_text(self, text: str) -> str: |
| 344 | + """Sanitize text for safe display. |
| 345 | +
|
| 346 | + Args: |
| 347 | + text: Text to sanitize |
| 348 | +
|
| 349 | + Returns: |
| 350 | + Sanitized text |
| 351 | + """ |
| 352 | + # Replace control characters with visible representations |
| 353 | + sanitized = text.replace("\t", " ") # Replace tabs with spaces |
| 354 | + sanitized = sanitized.replace("\r", "") # Remove carriage returns |
| 355 | + return sanitized |
0 commit comments