131
131
shlex_split ,
132
132
)
133
133
from .rich_utils import (
134
+ Cmd2ExceptionConsole ,
134
135
Cmd2GeneralConsole ,
135
136
RichPrintKwargs ,
136
137
)
@@ -1207,7 +1208,7 @@ def print_to(
1207
1208
sep : str = " " ,
1208
1209
end : str = "\n " ,
1209
1210
style : StyleType | None = None ,
1210
- soft_wrap : bool | None = None ,
1211
+ soft_wrap : bool = True ,
1211
1212
rich_print_kwargs : RichPrintKwargs | None = None ,
1212
1213
** kwargs : Any , # noqa: ARG002
1213
1214
) -> None :
@@ -1218,11 +1219,8 @@ def print_to(
1218
1219
:param sep: string to write between print data. Defaults to " ".
1219
1220
:param end: string to write at end of print data. Defaults to a newline.
1220
1221
:param style: optional style to apply to output
1221
- :param soft_wrap: Enable soft wrap mode. If True, text lines will not be automatically word-wrapped to fit the
1222
- terminal width; instead, any text that doesn't fit will run onto the following line(s),
1223
- similar to the built-in print() function. Set to False to enable automatic word-wrapping.
1224
- If None (the default for this parameter), the output will default to no word-wrapping, as
1225
- configured by the Cmd2GeneralConsole.
1222
+ :param soft_wrap: Enable soft wrap mode. If True, lines of text will not be word-wrapped or cropped to
1223
+ fit the terminal width. Defaults to True.
1226
1224
:param rich_print_kwargs: optional additional keyword arguments to pass to Rich's Console.print().
1227
1225
:param kwargs: Arbitrary keyword arguments. This allows subclasses to extend the signature of this
1228
1226
method and still call `super()` without encountering unexpected keyword argument errors.
@@ -1254,7 +1252,7 @@ def poutput(
1254
1252
sep : str = " " ,
1255
1253
end : str = "\n " ,
1256
1254
style : StyleType | None = None ,
1257
- soft_wrap : bool | None = None ,
1255
+ soft_wrap : bool = True ,
1258
1256
rich_print_kwargs : RichPrintKwargs | None = None ,
1259
1257
** kwargs : Any , # noqa: ARG002
1260
1258
) -> None :
@@ -1278,7 +1276,7 @@ def perror(
1278
1276
sep : str = " " ,
1279
1277
end : str = "\n " ,
1280
1278
style : StyleType | None = Cmd2Style .ERROR ,
1281
- soft_wrap : bool | None = None ,
1279
+ soft_wrap : bool = True ,
1282
1280
rich_print_kwargs : RichPrintKwargs | None = None ,
1283
1281
** kwargs : Any , # noqa: ARG002
1284
1282
) -> None :
@@ -1303,7 +1301,7 @@ def psuccess(
1303
1301
* objects : Any ,
1304
1302
sep : str = " " ,
1305
1303
end : str = "\n " ,
1306
- soft_wrap : bool | None = None ,
1304
+ soft_wrap : bool = True ,
1307
1305
rich_print_kwargs : RichPrintKwargs | None = None ,
1308
1306
** kwargs : Any , # noqa: ARG002
1309
1307
) -> None :
@@ -1325,7 +1323,7 @@ def pwarning(
1325
1323
* objects : Any ,
1326
1324
sep : str = " " ,
1327
1325
end : str = "\n " ,
1328
- soft_wrap : bool | None = None ,
1326
+ soft_wrap : bool = True ,
1329
1327
rich_print_kwargs : RichPrintKwargs | None = None ,
1330
1328
** kwargs : Any , # noqa: ARG002
1331
1329
) -> None :
@@ -1345,44 +1343,59 @@ def pwarning(
1345
1343
def pexcept (
1346
1344
self ,
1347
1345
exception : BaseException ,
1348
- end : str = "\n " ,
1349
- rich_print_kwargs : RichPrintKwargs | None = None ,
1350
1346
** kwargs : Any , # noqa: ARG002
1351
1347
) -> None :
1352
1348
"""Print an exception to sys.stderr.
1353
1349
1354
- If `debug` is true, a full exception traceback is also printed, if one exists.
1350
+ If `debug` is true, a full traceback is also printed, if one exists.
1355
1351
1356
1352
:param exception: the exception to be printed.
1357
-
1358
- For details on the other parameters, refer to the `print_to` method documentation .
1353
+ :param kwargs: Arbitrary keyword arguments. This allows subclasses to extend the signature of this
1354
+ method and still call `super()` without encountering unexpected keyword argument errors .
1359
1355
"""
1360
- final_msg = Text ( )
1356
+ console = Cmd2ExceptionConsole ( sys . stderr )
1361
1357
1358
+ # Only print a traceback if we're in debug mode and one exists.
1362
1359
if self .debug and sys .exc_info () != (None , None , None ):
1363
- console = Cmd2GeneralConsole (sys .stderr )
1364
- console .print_exception (word_wrap = True , max_frames = 0 )
1365
- else :
1366
- final_msg += f"EXCEPTION of type '{ type (exception ).__name__ } ' occurred with message: { exception } "
1360
+ console .print_exception (
1361
+ width = console .width ,
1362
+ show_locals = True ,
1363
+ max_frames = 0 , # 0 means full traceback.
1364
+ word_wrap = True , # Wrap long lines of code instead of truncate
1365
+ )
1366
+ console .print ()
1367
+ return
1367
1368
1368
- if not self .debug and 'debug' in self .settables :
1369
- warning = "\n To enable full traceback, run the following command: 'set debug true'"
1370
- final_msg .append (warning , style = Cmd2Style .WARNING )
1369
+ # Otherwise highlight and print the exception.
1370
+ from rich .highlighter import ReprHighlighter
1371
1371
1372
- if final_msg :
1373
- self .perror (
1374
- final_msg ,
1375
- end = end ,
1376
- rich_print_kwargs = rich_print_kwargs ,
1372
+ highlighter = ReprHighlighter ()
1373
+
1374
+ final_msg = Text .assemble (
1375
+ ("EXCEPTION of type " , Cmd2Style .ERROR ),
1376
+ (f"{ type (exception ).__name__ } " , Cmd2Style .EXCEPTION_TYPE ),
1377
+ (" occurred with message: " , Cmd2Style .ERROR ),
1378
+ highlighter (str (exception )),
1379
+ )
1380
+
1381
+ if not self .debug and 'debug' in self .settables :
1382
+ help_msg = Text .assemble (
1383
+ "\n \n " ,
1384
+ ("To enable full traceback, run the following command: " , Cmd2Style .WARNING ),
1385
+ ("set debug true" , Cmd2Style .COMMAND_LINE ),
1377
1386
)
1387
+ final_msg .append (help_msg )
1388
+
1389
+ console .print (final_msg )
1390
+ console .print ()
1378
1391
1379
1392
def pfeedback (
1380
1393
self ,
1381
1394
* objects : Any ,
1382
1395
sep : str = " " ,
1383
1396
end : str = "\n " ,
1384
1397
style : StyleType | None = None ,
1385
- soft_wrap : bool | None = None ,
1398
+ soft_wrap : bool = True ,
1386
1399
rich_print_kwargs : RichPrintKwargs | None = None ,
1387
1400
** kwargs : Any , # noqa: ARG002
1388
1401
) -> None :
@@ -1420,7 +1433,7 @@ def ppaged(
1420
1433
end : str = "\n " ,
1421
1434
style : StyleType | None = None ,
1422
1435
chop : bool = False ,
1423
- soft_wrap : bool | None = None ,
1436
+ soft_wrap : bool = True ,
1424
1437
rich_print_kwargs : RichPrintKwargs | None = None ,
1425
1438
** kwargs : Any , # noqa: ARG002
1426
1439
) -> None :
@@ -1436,13 +1449,11 @@ def ppaged(
1436
1449
False -> causes lines longer than the screen width to wrap to the next line
1437
1450
- wrapping is ideal when you want to keep users from having to use horizontal scrolling
1438
1451
WARNING: On Windows, the text always wraps regardless of what the chop argument is set to
1439
- :param soft_wrap: Enable soft wrap mode. If True, text lines will not be automatically word-wrapped to fit the
1440
- terminal width; instead, any text that doesn't fit will run onto the following line(s),
1441
- similar to the built-in print() function. Set to False to enable automatic word-wrapping.
1442
- If None (the default for this parameter), the output will default to no word-wrapping, as
1443
- configured by the Cmd2GeneralConsole.
1452
+ :param soft_wrap: Enable soft wrap mode. If True, lines of text will not be word-wrapped or cropped to
1453
+ fit the terminal width. Defaults to True.
1444
1454
1445
- Note: If chop is True and a pager is used, soft_wrap is automatically set to True.
1455
+ Note: If chop is True and a pager is used, soft_wrap is automatically set to True to
1456
+ prevent wrapping and allow for horizontal scrolling.
1446
1457
1447
1458
For details on the other parameters, refer to the `print_to` method documentation.
1448
1459
"""
@@ -3527,7 +3538,7 @@ def _build_alias_create_parser(cls) -> Cmd2ArgumentParser:
3527
3538
alias_create_notes = Group (
3528
3539
"If you want to use redirection, pipes, or terminators in the value of the alias, then quote them." ,
3529
3540
"\n " ,
3530
- Text (" alias create save_results print_results \" >\" out.txt\n " , style = Cmd2Style .EXAMPLE ),
3541
+ Text (" alias create save_results print_results \" >\" out.txt\n " , style = Cmd2Style .COMMAND_LINE ),
3531
3542
(
3532
3543
"Since aliases are resolved during parsing, tab completion will function as it would "
3533
3544
"for the actual command the alias resolves to."
@@ -3740,14 +3751,14 @@ def _build_macro_create_parser(cls) -> Cmd2ArgumentParser:
3740
3751
"\n " ,
3741
3752
"The following creates a macro called my_macro that expects two arguments:" ,
3742
3753
"\n " ,
3743
- Text (" macro create my_macro make_dinner --meat {1} --veggie {2}" , style = Cmd2Style .EXAMPLE ),
3754
+ Text (" macro create my_macro make_dinner --meat {1} --veggie {2}" , style = Cmd2Style .COMMAND_LINE ),
3744
3755
"\n " ,
3745
3756
"When the macro is called, the provided arguments are resolved and the assembled command is run. For example:" ,
3746
3757
"\n " ,
3747
3758
Text .assemble (
3748
- (" my_macro beef broccoli" , Cmd2Style .EXAMPLE ),
3759
+ (" my_macro beef broccoli" , Cmd2Style .COMMAND_LINE ),
3749
3760
(" ───> " , Style (bold = True )),
3750
- ("make_dinner --meat beef --veggie broccoli" , Cmd2Style .EXAMPLE ),
3761
+ ("make_dinner --meat beef --veggie broccoli" , Cmd2Style .COMMAND_LINE ),
3751
3762
),
3752
3763
)
3753
3764
macro_create_parser = argparse_custom .DEFAULT_ARGUMENT_PARSER (description = macro_create_description )
@@ -3763,15 +3774,15 @@ def _build_macro_create_parser(cls) -> Cmd2ArgumentParser:
3763
3774
"first argument will populate both {1} instances."
3764
3775
),
3765
3776
"\n " ,
3766
- Text (" macro create ft file_taxes -p {1} -q {2} -r {1}" , style = Cmd2Style .EXAMPLE ),
3777
+ Text (" macro create ft file_taxes -p {1} -q {2} -r {1}" , style = Cmd2Style .COMMAND_LINE ),
3767
3778
"\n " ,
3768
3779
"To quote an argument in the resolved command, quote it during creation." ,
3769
3780
"\n " ,
3770
- Text (" macro create backup !cp \" {1}\" \" {1}.orig\" " , style = Cmd2Style .EXAMPLE ),
3781
+ Text (" macro create backup !cp \" {1}\" \" {1}.orig\" " , style = Cmd2Style .COMMAND_LINE ),
3771
3782
"\n " ,
3772
3783
"If you want to use redirection, pipes, or terminators in the value of the macro, then quote them." ,
3773
3784
"\n " ,
3774
- Text (" macro create show_results print_results -type {1} \" |\" less" , style = Cmd2Style .EXAMPLE ),
3785
+ Text (" macro create show_results print_results -type {1} \" |\" less" , style = Cmd2Style .COMMAND_LINE ),
3775
3786
"\n " ,
3776
3787
(
3777
3788
"Since macros don't resolve until after you press Enter, their arguments tab complete as paths. "
@@ -5316,7 +5327,7 @@ def _build_edit_parser(cls) -> Cmd2ArgumentParser:
5316
5327
"Note" ,
5317
5328
Text .assemble (
5318
5329
"To set a new editor, run: " ,
5319
- ("set editor <program>" , Cmd2Style .EXAMPLE ),
5330
+ ("set editor <program>" , Cmd2Style .COMMAND_LINE ),
5320
5331
),
5321
5332
)
5322
5333
0 commit comments