@@ -246,7 +246,6 @@ def my_completer(self, text, line, begidx, endidx, arg_tokens)
246
246
gettext ,
247
247
)
248
248
from typing import (
249
- IO ,
250
249
TYPE_CHECKING ,
251
250
Any ,
252
251
Callable ,
@@ -265,6 +264,8 @@ def my_completer(self, text, line, begidx, endidx, arg_tokens)
265
264
Group ,
266
265
RenderableType ,
267
266
)
267
+ from rich .table import Column , Table
268
+ from rich .text import Text
268
269
from rich_argparse import (
269
270
ArgumentDefaultsRichHelpFormatter ,
270
271
MetavarTypeRichHelpFormatter ,
@@ -274,8 +275,8 @@ def my_completer(self, text, line, begidx, endidx, arg_tokens)
274
275
)
275
276
276
277
from . import (
277
- ansi ,
278
278
constants ,
279
+ rich_utils ,
279
280
)
280
281
281
282
if TYPE_CHECKING : # pragma: no cover
@@ -1053,10 +1054,17 @@ def _SubParsersAction_remove_parser(self: argparse._SubParsersAction, name: str)
1053
1054
class Cmd2HelpFormatter (RichHelpFormatter ):
1054
1055
"""Custom help formatter to configure ordering of help text"""
1055
1056
1056
- # Render markup in usage, help, description, and epilog text.
1057
- RichHelpFormatter .usage_markup = True
1058
- RichHelpFormatter .help_markup = True
1059
- RichHelpFormatter .text_markup = True
1057
+ # Disable automatic highlighting in the help text.
1058
+ highlights = []
1059
+
1060
+ # Disable markup rendering in usage, help, description, and epilog text.
1061
+ # cmd2's built-in commands do not escape opening brackets in their help text
1062
+ # and therefore rely on these settings being False. If you desire to use
1063
+ # markup in your help text, inherit from a Cmd2 help formatter and override
1064
+ # these settings in that child class.
1065
+ usage_markup = False
1066
+ help_markup = False
1067
+ text_markup = False
1060
1068
1061
1069
def _format_usage (
1062
1070
self ,
@@ -1319,24 +1327,23 @@ def __init__(
1319
1327
1320
1328
def __rich__ (self ) -> Group :
1321
1329
"""Custom rendering logic."""
1322
- import rich
1323
-
1324
1330
formatter = self .formatter_creator ()
1325
1331
1326
- styled_title = rich . text . Text (
1332
+ styled_title = Text (
1327
1333
type (formatter ).group_name_formatter (f"{ self .title } :" ),
1328
1334
style = formatter .styles ["argparse.groups" ],
1329
1335
)
1330
1336
1331
1337
# Left pad the text like an argparse argument group does
1332
1338
left_padding = formatter ._indent_increment
1333
-
1334
- text_table = rich . table . Table (
1339
+ text_table = Table (
1340
+ Column ( overflow = "fold" ),
1335
1341
box = None ,
1336
1342
show_header = False ,
1337
1343
padding = (0 , 0 , 0 , left_padding ),
1338
1344
)
1339
1345
text_table .add_row (self .text )
1346
+
1340
1347
return Group (styled_title , text_table )
1341
1348
1342
1349
@@ -1413,12 +1420,20 @@ def error(self, message: str) -> NoReturn:
1413
1420
linum += 1
1414
1421
1415
1422
self .print_usage (sys .stderr )
1416
- formatted_message = ansi .style_error (formatted_message )
1417
- self .exit (2 , f'{ formatted_message } \n \n ' )
1423
+
1424
+ # Add failure style to message
1425
+ console = self ._get_formatter ().console
1426
+ with console .capture () as capture :
1427
+ console .print (formatted_message , style = rich_utils .style_failure , crop = False )
1428
+ formatted_message = f"{ capture .get ()} "
1429
+
1430
+ self .exit (2 , f'{ formatted_message } \n ' )
1418
1431
1419
1432
def _get_formatter (self ) -> Cmd2HelpFormatter :
1420
- """Copy of _get_formatter() with a different return type to assist type checkers."""
1421
- return cast (Cmd2HelpFormatter , super ()._get_formatter ())
1433
+ """Copy of _get_formatter() with customizations for Cmd2HelpFormatter."""
1434
+ formatter = cast (Cmd2HelpFormatter , super ()._get_formatter ())
1435
+ formatter .console = rich_utils .Cmd2Console (sys .stdout )
1436
+ return formatter
1422
1437
1423
1438
def format_help (self ) -> str :
1424
1439
"""Copy of format_help() from argparse.ArgumentParser with tweaks to separately display required parameters"""
@@ -1474,13 +1489,6 @@ def format_help(self) -> str:
1474
1489
# determine help from format above
1475
1490
return formatter .format_help () + '\n '
1476
1491
1477
- def _print_message (self , message : str , file : Optional [IO [str ]] = None ) -> None :
1478
- # Override _print_message to use style_aware_write() since we use ANSI escape characters to support color
1479
- if message :
1480
- if file is None :
1481
- file = sys .stderr
1482
- ansi .style_aware_write (file , message )
1483
-
1484
1492
def create_text_group (self , title : str , text : RenderableType ) -> TextGroup :
1485
1493
"""Create a TextGroup using this parser's formatter creator."""
1486
1494
return TextGroup (title , text , self ._get_formatter )
0 commit comments