6464)
6565
6666import rich .box
67- from rich .console import Group , RenderableType
67+ from rich .console import Console , Group , RenderableType
6868from rich .highlighter import ReprHighlighter
6969from rich .rule import Rule
7070from rich .style import Style , StyleType
@@ -1467,17 +1467,22 @@ def pwarning(
14671467 def pexcept (
14681468 self ,
14691469 exception : BaseException ,
1470+ * ,
1471+ console : Console | None = None ,
14701472 ** kwargs : Any , # noqa: ARG002
14711473 ) -> None :
14721474 """Print an exception to sys.stderr.
14731475
14741476 If `debug` is true, a full traceback is also printed, if one exists.
14751477
14761478 :param exception: the exception to be printed.
1479+ :param console: optional Rich console to use for printing. If None, a new Cmd2ExceptionConsole
1480+ instance is created which writes to sys.stderr.
14771481 :param kwargs: Arbitrary keyword arguments. This allows subclasses to extend the signature of this
14781482 method and still call `super()` without encountering unexpected keyword argument errors.
14791483 """
1480- console = Cmd2ExceptionConsole (sys .stderr )
1484+ if console is None :
1485+ console = Cmd2ExceptionConsole (sys .stderr )
14811486
14821487 # Only print a traceback if we're in debug mode and one exists.
14831488 if self .debug and sys .exc_info () != (None , None , None ):
@@ -2555,9 +2560,9 @@ def complete(
25552560 # above the prompt so it remains in the scrollback.
25562561 if ex .apply_style :
25572562 # Render the error with style to a string using Rich
2558- console = ru .Cmd2GeneralConsole ()
2559- with console .capture () as capture :
2560- console .print ("\n " + err_str , style = Cmd2Style .ERROR )
2563+ general_console = ru .Cmd2GeneralConsole ()
2564+ with general_console .capture () as capture :
2565+ general_console .print ("\n " + err_str , style = Cmd2Style .ERROR )
25612566 self .completion_header = capture .get ()
25622567
25632568 # Otherwise, this is a hint that should be displayed below the prompt.
@@ -2566,8 +2571,11 @@ def complete(
25662571 return None
25672572 except Exception as ex : # noqa: BLE001
25682573 # Insert a newline so the exception doesn't print in the middle of the command line being tab completed
2569- self .perror ()
2570- self .pexcept (ex )
2574+ exception_console = ru .Cmd2ExceptionConsole ()
2575+ with exception_console .capture () as capture :
2576+ exception_console .print ()
2577+ self .pexcept (ex , console = exception_console )
2578+ self .completion_header = capture .get ()
25712579 return None
25722580
25732581 def in_script (self ) -> bool :
0 commit comments