Skip to content

Commit 0e90022

Browse files
authored
Added 'justify' to the print methods. (#1610)
1 parent d5642c3 commit 0e90022

File tree

5 files changed

+29
-9
lines changed

5 files changed

+29
-9
lines changed

cmd2/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
# String Utils
100100
'stylize',
101101
# Styles,
102-
"Cmd2Style",
102+
'Cmd2Style',
103103
# Utilities
104104
'categorize',
105105
'CustomCompletionSettings',

cmd2/cmd2.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
from prompt_toolkit.shortcuts import CompleteStyle, PromptSession, choice, set_title
8585
from rich.console import (
8686
Group,
87+
JustifyMethod,
8788
RenderableType,
8889
)
8990
from rich.highlighter import ReprHighlighter
@@ -1344,6 +1345,7 @@ def print_to(
13441345
end: str = "\n",
13451346
style: StyleType | None = None,
13461347
soft_wrap: bool = True,
1348+
justify: JustifyMethod | None = None,
13471349
emoji: bool = False,
13481350
markup: bool = False,
13491351
highlight: bool = False,
@@ -1369,6 +1371,7 @@ def print_to(
13691371
Tables, Panels, or Columns to ensure they render as expected.
13701372
For example, when soft_wrap is True Panels truncate text
13711373
which is wider than the terminal.
1374+
:param justify: justify method ("left", "center", "right", "full"). Defaults to None.
13721375
:param emoji: If True, Rich will replace emoji codes (e.g., :smiley:) with their
13731376
corresponding Unicode characters. Defaults to False.
13741377
:param markup: If True, Rich will interpret strings with tags (e.g., [bold]hello[/bold])
@@ -1395,6 +1398,7 @@ def print_to(
13951398
sep=sep,
13961399
end=end,
13971400
style=style,
1401+
justify=justify,
13981402
soft_wrap=soft_wrap,
13991403
**(rich_print_kwargs if rich_print_kwargs is not None else {}),
14001404
)
@@ -1414,6 +1418,7 @@ def poutput(
14141418
end: str = "\n",
14151419
style: StyleType | None = None,
14161420
soft_wrap: bool = True,
1421+
justify: JustifyMethod | None = None,
14171422
emoji: bool = False,
14181423
markup: bool = False,
14191424
highlight: bool = False,
@@ -1431,6 +1436,7 @@ def poutput(
14311436
end=end,
14321437
style=style,
14331438
soft_wrap=soft_wrap,
1439+
justify=justify,
14341440
emoji=emoji,
14351441
markup=markup,
14361442
highlight=highlight,
@@ -1444,6 +1450,7 @@ def perror(
14441450
end: str = "\n",
14451451
style: StyleType | None = Cmd2Style.ERROR,
14461452
soft_wrap: bool = True,
1453+
justify: JustifyMethod | None = None,
14471454
emoji: bool = False,
14481455
markup: bool = False,
14491456
highlight: bool = False,
@@ -1463,6 +1470,7 @@ def perror(
14631470
end=end,
14641471
style=style,
14651472
soft_wrap=soft_wrap,
1473+
justify=justify,
14661474
emoji=emoji,
14671475
markup=markup,
14681476
highlight=highlight,
@@ -1475,6 +1483,7 @@ def psuccess(
14751483
sep: str = " ",
14761484
end: str = "\n",
14771485
soft_wrap: bool = True,
1486+
justify: JustifyMethod | None = None,
14781487
emoji: bool = False,
14791488
markup: bool = False,
14801489
highlight: bool = False,
@@ -1491,6 +1500,7 @@ def psuccess(
14911500
end=end,
14921501
style=Cmd2Style.SUCCESS,
14931502
soft_wrap=soft_wrap,
1503+
justify=justify,
14941504
emoji=emoji,
14951505
markup=markup,
14961506
highlight=highlight,
@@ -1503,6 +1513,7 @@ def pwarning(
15031513
sep: str = " ",
15041514
end: str = "\n",
15051515
soft_wrap: bool = True,
1516+
justify: JustifyMethod | None = None,
15061517
emoji: bool = False,
15071518
markup: bool = False,
15081519
highlight: bool = False,
@@ -1519,6 +1530,7 @@ def pwarning(
15191530
end=end,
15201531
style=Cmd2Style.WARNING,
15211532
soft_wrap=soft_wrap,
1533+
justify=justify,
15221534
emoji=emoji,
15231535
markup=markup,
15241536
highlight=highlight,
@@ -1597,6 +1609,7 @@ def pfeedback(
15971609
end: str = "\n",
15981610
style: StyleType | None = None,
15991611
soft_wrap: bool = True,
1612+
justify: JustifyMethod | None = None,
16001613
emoji: bool = False,
16011614
markup: bool = False,
16021615
highlight: bool = False,
@@ -1618,6 +1631,7 @@ def pfeedback(
16181631
end=end,
16191632
style=style,
16201633
soft_wrap=soft_wrap,
1634+
justify=justify,
16211635
emoji=emoji,
16221636
markup=markup,
16231637
highlight=highlight,
@@ -1630,6 +1644,7 @@ def pfeedback(
16301644
end=end,
16311645
style=style,
16321646
soft_wrap=soft_wrap,
1647+
justify=justify,
16331648
emoji=emoji,
16341649
markup=markup,
16351650
highlight=highlight,
@@ -1644,6 +1659,7 @@ def ppaged(
16441659
style: StyleType | None = None,
16451660
chop: bool = False,
16461661
soft_wrap: bool = True,
1662+
justify: JustifyMethod | None = None,
16471663
emoji: bool = False,
16481664
markup: bool = False,
16491665
highlight: bool = False,
@@ -1700,6 +1716,7 @@ def ppaged(
17001716
sep=sep,
17011717
end=end,
17021718
style=style,
1719+
justify=justify,
17031720
soft_wrap=soft_wrap,
17041721
**(rich_print_kwargs if rich_print_kwargs is not None else {}),
17051722
)
@@ -1748,6 +1765,7 @@ def ppaged(
17481765
end=end,
17491766
style=style,
17501767
soft_wrap=soft_wrap,
1768+
justify=justify,
17511769
emoji=emoji,
17521770
markup=markup,
17531771
highlight=highlight,

cmd2/rich_utils.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,18 @@ def set_theme(styles: Mapping[str, StyleType] | None = None) -> None:
101101

102102

103103
class RichPrintKwargs(TypedDict, total=False):
104-
"""Keyword arguments that can be passed to rich.console.Console.print() via cmd2's print methods.
104+
"""Infrequently used Rich Console.print() keyword arguments.
105105
106-
See Rich's Console.print() documentation for full details on these parameters.
106+
These arguments are supported by cmd2's print methods (e.g., poutput())
107+
via their ``rich_print_kwargs`` parameter.
108+
109+
See Rich's Console.print() documentation for full details:
107110
https://rich.readthedocs.io/en/stable/reference/console.html#rich.console.Console.print
108111
109-
Note: All fields are optional (total=False). If a key is not present in the
110-
dictionary, Rich's default behavior for that argument will apply.
112+
Note: All fields are optional (total=False). If a key is not present,
113+
Rich's default behavior for that argument will apply.
111114
"""
112115

113-
justify: JustifyMethod | None
114116
overflow: OverflowMethod | None
115117
no_wrap: bool | None
116118
width: int | None

docs/features/generating_output.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ each line is aligned independently.
162162

163163
!!! tip "Advanced alignment customization"
164164

165-
You can also control output alignment using the `rich_print_kwargs.justify` member when calling
165+
You can also control output alignment using the `justify` parameter when calling
166166
`cmd2`'s print methods.
167167

168168
## Columnar Output

tests/test_cmd2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2466,11 +2466,12 @@ def test_poutput_emoji(outsim_app):
24662466

24672467
@with_ansi_style(ru.AllowStyle.ALWAYS)
24682468
def test_poutput_justify_and_width(outsim_app):
2469-
rich_print_kwargs = RichPrintKwargs(justify="right", width=10)
2469+
rich_print_kwargs = RichPrintKwargs(width=10)
24702470

24712471
# Use a styled-string when justifying to check if its display width is correct.
24722472
outsim_app.poutput(
24732473
su.stylize("Hello", style="blue"),
2474+
justify="right",
24742475
rich_print_kwargs=rich_print_kwargs,
24752476
)
24762477
out = outsim_app.stdout.getvalue()
@@ -2504,7 +2505,6 @@ def test_poutput_pretty_print(outsim_app):
25042505
def test_poutput_all_keyword_args(outsim_app):
25052506
"""Test that all fields in RichPrintKwargs are recognized by Rich's Console.print()."""
25062507
rich_print_kwargs = RichPrintKwargs(
2507-
justify="center",
25082508
overflow="ellipsis",
25092509
no_wrap=True,
25102510
width=40,

0 commit comments

Comments
 (0)