88from rich .console import Group , RenderableType
99from rich .text import Text
1010
11+ from cli_patterns .ui .design .registry import theme_registry
1112from cli_patterns .ui .design .tokens import HierarchyToken , StatusToken
1213
1314if TYPE_CHECKING :
@@ -92,6 +93,9 @@ def has_flag(self, flag: str) -> bool:
9293class ParseError (Exception ):
9394 """Exception raised during command parsing.
9495
96+ This class implements the Rich __rich__() protocol for automatic themed display
97+ when printed to a Rich console. Use console.print(error) for best results.
98+
9599 Attributes:
96100 message: Human-readable error message
97101 error_type: Type of parsing error
@@ -123,26 +127,29 @@ def __str__(self) -> str:
123127 def __rich__ (self ) -> RenderableType :
124128 """Rich rendering protocol implementation for automatic themed display.
125129
130+ Uses the global theme_registry to resolve design tokens to themed styles,
131+ ensuring consistency with the application's design system.
132+
126133 Returns:
127134 RenderableType (Group) containing styled error message and suggestions
128135 """
129136 # Map error_type to StatusToken
130137 status_token = self ._get_status_token ()
131138
132- # Create styled error message
139+ # Create styled error message using theme registry
133140 error_text = Text ()
134141 error_text .append (
135- f"{ self .error_type } : " , style = self . _get_status_style (status_token )
142+ f"{ self .error_type } : " , style = theme_registry . resolve (status_token )
136143 )
137144 error_text .append (self .message )
138145
139146 # Create suggestions list with hierarchy styling (limit to 3)
140147 renderables : list [RenderableType ] = [error_text ]
141148
142149 if self .suggestions :
143- # Add "Did you mean:" prompt
150+ # Add "Did you mean:" prompt using theme registry
144151 prompt_text = Text (
145- "\n \n Did you mean:" , style = self . _get_status_style (StatusToken .INFO )
152+ "\n \n Did you mean:" , style = theme_registry . resolve (StatusToken .INFO )
146153 )
147154 renderables .append (prompt_text )
148155
@@ -151,7 +158,7 @@ def __rich__(self) -> RenderableType:
151158 hierarchy = self ._get_suggestion_hierarchy (idx )
152159 suggestion_text = Text ()
153160 suggestion_text .append (
154- f"\n • { suggestion } " , style = self . _get_hierarchy_style (hierarchy )
161+ f"\n • { suggestion } " , style = theme_registry . resolve (hierarchy )
155162 )
156163 renderables .append (suggestion_text )
157164
@@ -185,6 +192,9 @@ def _get_status_token(self) -> StatusToken:
185192 def _get_suggestion_hierarchy (self , index : int ) -> HierarchyToken :
186193 """Get hierarchy token for suggestion based on ranking.
187194
195+ The first suggestion is PRIMARY (best match), second is SECONDARY (good match),
196+ and third is TERTIARY (possible match).
197+
188198 Args:
189199 index: Position in suggestions list (0-based)
190200
@@ -198,50 +208,6 @@ def _get_suggestion_hierarchy(self, index: int) -> HierarchyToken:
198208 else :
199209 return HierarchyToken .TERTIARY # Possible match
200210
201- def _get_status_style (self , status : StatusToken ) -> str :
202- """Get Rich style string for StatusToken.
203-
204- Args:
205- status: StatusToken to convert to style
206-
207- Returns:
208- Rich style string
209- """
210- if status == StatusToken .ERROR :
211- return "bold red"
212- elif status == StatusToken .WARNING :
213- return "bold yellow"
214- elif status == StatusToken .INFO :
215- return "blue"
216- elif status == StatusToken .SUCCESS :
217- return "bold green"
218- elif status == StatusToken .RUNNING :
219- return "cyan"
220- elif status == StatusToken .MUTED :
221- return "dim"
222- else :
223- return "default"
224-
225- def _get_hierarchy_style (self , hierarchy : HierarchyToken ) -> str :
226- """Get Rich style string for HierarchyToken.
227-
228- Args:
229- hierarchy: HierarchyToken to convert to style
230-
231- Returns:
232- Rich style string
233- """
234- if hierarchy == HierarchyToken .PRIMARY :
235- return "bold"
236- elif hierarchy == HierarchyToken .SECONDARY :
237- return "default"
238- elif hierarchy == HierarchyToken .TERTIARY :
239- return "dim"
240- elif hierarchy == HierarchyToken .QUATERNARY :
241- return "dim italic"
242- else :
243- return "default"
244-
245211
246212@dataclass
247213class Context :
0 commit comments