Skip to content

Commit 322b2e4

Browse files
committed
Updated default values for print functions.
1 parent a2b3639 commit 322b2e4

File tree

1 file changed

+57
-34
lines changed

1 file changed

+57
-34
lines changed

cmd2/cmd2.py

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,21 +1257,39 @@ def print_to(
12571257
sep: str = " ",
12581258
end: str = "\n",
12591259
style: StyleType | None = None,
1260-
soft_wrap: bool = True,
1261-
emoji: bool = False,
1262-
markup: bool = False,
1263-
highlight: bool = False,
1260+
soft_wrap: bool | None = None,
1261+
emoji: bool | None = None,
1262+
markup: bool | None = None,
1263+
highlight: bool | None = None,
12641264
rich_print_kwargs: RichPrintKwargs | None = None,
12651265
**kwargs: Any, # noqa: ARG002
12661266
) -> None:
12671267
"""Print objects to a given destination (file stream or cmd2 console).
12681268
1269-
This method is configured for general-purpose printing. By default, it enables
1270-
soft wrap and disables Rich's automatic detection for markup, emoji, and highlighting.
1271-
These defaults can be overridden by passing explicit keyword arguments.
1269+
If ``destination`` is a file-like object, it is wrapped in a ``Cmd2GeneralConsole``
1270+
which is configured for general-purpose printing. By default, it enables soft wrap and
1271+
disables Rich's automatic detection for markup, emoji, and highlighting. These defaults
1272+
can be overridden by passing explicit keyword arguments.
1273+
1274+
If ``destination`` is a ``Cmd2BaseConsole``, the console's default settings for
1275+
soft wrap, markup, emoji, and highlighting are used unless overridden by passing
1276+
explicit keyword arguments.
12721277
12731278
See the Rich documentation for more details on emoji codes, markup tags, and highlighting.
12741279
1280+
**Why use this method instead of console.print()?**
1281+
1282+
This method calls ``cmd2.rich_utils.prepare_objects_for_rendering()`` on the objects
1283+
being printed. This ensures that strings containing ANSI style sequences are correctly
1284+
converted to Rich Text objects, preserving their style and calculating the correct
1285+
display width. This is particularly important when capturing output from a console.
1286+
1287+
Example:
1288+
```py
1289+
with console.capture() as capture:
1290+
self.print_to(console, some_ansi_styled_string)
1291+
```
1292+
12751293
!!! note
12761294
12771295
To ensure consistent behavior, this method requires a file-like object or
@@ -1292,7 +1310,8 @@ def print_to(
12921310
:param sep: string to write between printed text. Defaults to " ".
12931311
:param end: string to write at end of printed text. Defaults to a newline.
12941312
:param style: optional style to apply to output
1295-
:param soft_wrap: Enable soft wrap mode. Defaults to True.
1313+
:param soft_wrap: Enable soft wrap mode. Defaults to None.
1314+
If None, the destination console's default behavior is used.
12961315
If True, text that doesn't fit will run on to the following line,
12971316
just like with print(). This is useful for raw text and logs.
12981317
If False, Rich wraps text to fit the terminal width.
@@ -1301,19 +1320,23 @@ def print_to(
13011320
For example, when soft_wrap is True Panels truncate text
13021321
which is wider than the terminal.
13031322
:param emoji: If True, Rich will replace emoji codes (e.g., :smiley:) with their
1304-
corresponding Unicode characters. Defaults to False.
1323+
corresponding Unicode characters. Defaults to None.
1324+
If None, the destination console's default behavior is used.
13051325
:param markup: If True, Rich will interpret strings with tags (e.g., [bold]hello[/bold])
1306-
as styled output. Defaults to False.
1326+
as styled output. Defaults to None.
1327+
If None, the destination console's default behavior is used.
13071328
:param highlight: If True, Rich will automatically apply highlighting to elements within
13081329
strings, such as common Python data types like numbers, booleans, or None.
13091330
This is particularly useful when pretty printing objects like lists and
1310-
dictionaries to display them in color. Defaults to False.
1331+
dictionaries to display them in color. Defaults to None.
1332+
If None, the destination console's default behavior is used.
13111333
:param rich_print_kwargs: optional additional keyword arguments to pass to Rich's Console.print().
13121334
:param kwargs: Arbitrary keyword arguments. This allows subclasses to extend the signature of this
13131335
method and still call `super()` without encountering unexpected keyword argument errors.
13141336
These arguments are not passed to Rich's Console.print().
13151337
:raises TypeError: If ``destination`` is a non-cmd2 ``Console`` instance that
13161338
does not derive from ``Cmd2BaseConsole``.
1339+
13171340
"""
13181341
if isinstance(destination, Console):
13191342
if not isinstance(destination, Cmd2BaseConsole):
@@ -1359,10 +1382,10 @@ def poutput(
13591382
sep: str = " ",
13601383
end: str = "\n",
13611384
style: StyleType | None = None,
1362-
soft_wrap: bool = True,
1363-
emoji: bool = False,
1364-
markup: bool = False,
1365-
highlight: bool = False,
1385+
soft_wrap: bool | None = None,
1386+
emoji: bool | None = None,
1387+
markup: bool | None = None,
1388+
highlight: bool | None = None,
13661389
rich_print_kwargs: RichPrintKwargs | None = None,
13671390
**kwargs: Any, # noqa: ARG002
13681391
) -> None:
@@ -1389,10 +1412,10 @@ def perror(
13891412
sep: str = " ",
13901413
end: str = "\n",
13911414
style: StyleType | None = Cmd2Style.ERROR,
1392-
soft_wrap: bool = True,
1393-
emoji: bool = False,
1394-
markup: bool = False,
1395-
highlight: bool = False,
1415+
soft_wrap: bool | None = None,
1416+
emoji: bool | None = None,
1417+
markup: bool | None = None,
1418+
highlight: bool | None = None,
13961419
rich_print_kwargs: RichPrintKwargs | None = None,
13971420
**kwargs: Any, # noqa: ARG002
13981421
) -> None:
@@ -1420,10 +1443,10 @@ def psuccess(
14201443
*objects: Any,
14211444
sep: str = " ",
14221445
end: str = "\n",
1423-
soft_wrap: bool = True,
1424-
emoji: bool = False,
1425-
markup: bool = False,
1426-
highlight: bool = False,
1446+
soft_wrap: bool | None = None,
1447+
emoji: bool | None = None,
1448+
markup: bool | None = None,
1449+
highlight: bool | None = None,
14271450
rich_print_kwargs: RichPrintKwargs | None = None,
14281451
**kwargs: Any, # noqa: ARG002
14291452
) -> None:
@@ -1448,10 +1471,10 @@ def pwarning(
14481471
*objects: Any,
14491472
sep: str = " ",
14501473
end: str = "\n",
1451-
soft_wrap: bool = True,
1452-
emoji: bool = False,
1453-
markup: bool = False,
1454-
highlight: bool = False,
1474+
soft_wrap: bool | None = None,
1475+
emoji: bool | None = None,
1476+
markup: bool | None = None,
1477+
highlight: bool | None = None,
14551478
rich_print_kwargs: RichPrintKwargs | None = None,
14561479
**kwargs: Any, # noqa: ARG002
14571480
) -> None:
@@ -1531,10 +1554,10 @@ def pfeedback(
15311554
sep: str = " ",
15321555
end: str = "\n",
15331556
style: StyleType | None = None,
1534-
soft_wrap: bool = True,
1535-
emoji: bool = False,
1536-
markup: bool = False,
1537-
highlight: bool = False,
1557+
soft_wrap: bool | None = None,
1558+
emoji: bool | None = None,
1559+
markup: bool | None = None,
1560+
highlight: bool | None = None,
15381561
rich_print_kwargs: RichPrintKwargs | None = None,
15391562
**kwargs: Any, # noqa: ARG002
15401563
) -> None:
@@ -1579,9 +1602,9 @@ def ppaged(
15791602
style: StyleType | None = None,
15801603
chop: bool = False,
15811604
soft_wrap: bool = True,
1582-
emoji: bool = False,
1583-
markup: bool = False,
1584-
highlight: bool = False,
1605+
emoji: bool | None = None,
1606+
markup: bool | None = None,
1607+
highlight: bool | None = None,
15851608
rich_print_kwargs: RichPrintKwargs | None = None,
15861609
**kwargs: Any, # noqa: ARG002
15871610
) -> None:

0 commit comments

Comments
 (0)