Skip to content

Commit d0df69a

Browse files
committed
Added rich_utils.indent() to ensure indented text wraps instead of
truncating when soft_wrap is enabled.
1 parent 91e5bc2 commit d0df69a

File tree

3 files changed

+38
-21
lines changed

3 files changed

+38
-21
lines changed

cmd2/argparse_custom.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ def get_items(self) -> list[CompletionItems]:
284284
RenderableType,
285285
)
286286
from rich.protocol import is_renderable
287-
from rich.table import Column, Table
287+
from rich.table import Column
288288
from rich.text import Text
289289
from rich_argparse import (
290290
ArgumentDefaultsRichHelpFormatter,
@@ -295,6 +295,7 @@ def get_items(self) -> list[CompletionItems]:
295295
)
296296

297297
from . import constants
298+
from . import rich_utils as ru
298299
from .rich_utils import Cmd2RichArgparseConsole
299300
from .styles import Cmd2Style
300301

@@ -1377,17 +1378,10 @@ def __rich__(self) -> Group:
13771378
style=formatter.styles["argparse.groups"],
13781379
)
13791380

1380-
# Left pad the text like an argparse argument group does
1381-
left_padding = formatter._indent_increment
1382-
text_table = Table(
1383-
Column(overflow="fold"),
1384-
box=None,
1385-
show_header=False,
1386-
padding=(0, 0, 0, left_padding),
1387-
)
1388-
text_table.add_row(self.text)
1381+
# Indent text like an argparse argument group does
1382+
indented_text = ru.indent(self.text, formatter._indent_increment)
13891383

1390-
return Group(styled_title, text_table)
1384+
return Group(styled_title, indented_text)
13911385

13921386

13931387
class Cmd2ArgumentParser(argparse.ArgumentParser):

cmd2/cmd2.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565

6666
import rich.box
6767
from rich.console import Group
68-
from rich.padding import Padding
6968
from rich.rule import Rule
7069
from rich.style import Style, StyleType
7170
from rich.table import (
@@ -4076,9 +4075,8 @@ def do_help(self, args: argparse.Namespace) -> None:
40764075
# Indent doc_leader to align with the help tables.
40774076
self.poutput()
40784077
self.poutput(
4079-
Padding.indent(self.doc_leader, 1),
4078+
ru.indent(self.doc_leader, 1),
40804079
style=Cmd2Style.HELP_LEADER,
4081-
soft_wrap=False,
40824080
)
40834081
self.poutput()
40844082

@@ -4139,13 +4137,13 @@ def print_topics(self, header: str, cmds: list[str] | None, cmdlen: int, maxcol:
41394137
header_grid = Table.grid()
41404138
header_grid.add_row(header, style=Cmd2Style.HELP_HEADER)
41414139
header_grid.add_row(Rule(characters=self.ruler, style=Cmd2Style.TABLE_BORDER))
4142-
self.poutput(Padding.indent(header_grid, 1))
4140+
self.poutput(ru.indent(header_grid, 1))
41434141

41444142
# Print the topics in columns.
41454143
# Subtract 1 from maxcol to account for indentation.
41464144
maxcol = min(maxcol, ru.console_width()) - 1
41474145
columnized_cmds = self.render_columns(cmds, maxcol)
4148-
self.poutput(Padding.indent(columnized_cmds, 1), soft_wrap=False)
4146+
self.poutput(ru.indent(columnized_cmds, 1))
41494147
self.poutput()
41504148

41514149
def _print_documented_command_topics(self, header: str, cmds: list[str], verbose: bool) -> None:
@@ -4160,11 +4158,7 @@ def _print_documented_command_topics(self, header: str, cmds: list[str], verbose
41604158
return
41614159

41624160
# Indent header to align with the help tables.
4163-
self.poutput(
4164-
Padding.indent(header, 1),
4165-
style=Cmd2Style.HELP_HEADER,
4166-
soft_wrap=False,
4167-
)
4161+
self.poutput(ru.indent(header, 1), style=Cmd2Style.HELP_HEADER)
41684162
topics_table = Table(
41694163
Column("Name", no_wrap=True),
41704164
Column("Description", overflow="fold"),

cmd2/rich_utils.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
RenderableType,
1818
RichCast,
1919
)
20+
from rich.padding import Padding
2021
from rich.style import StyleType
22+
from rich.table import (
23+
Column,
24+
Table,
25+
)
2126
from rich.text import Text
2227
from rich.theme import Theme
2328
from rich_argparse import RichHelpFormatter
@@ -288,6 +293,30 @@ def string_to_rich_text(text: str) -> Text:
288293
return result
289294

290295

296+
def indent(renderable: RenderableType, level: int) -> Padding:
297+
"""Indent a Rich renderable.
298+
299+
When soft-wrapping is enabled, a Rich console is unable to properly print a
300+
Padding object of indented text, as it truncates long strings instead of wrapping
301+
them. This function provides a workaround for this issue, ensuring that indented
302+
text is printed correctly regardless of the soft-wrap setting.
303+
304+
For non-text objects, this function merely serves as a convenience
305+
wrapper around Padding.indent().
306+
307+
:param renderable: a Rich renderable to indent.
308+
:param level: number of characters to indent.
309+
:return: a Padding object containing the indented content.
310+
"""
311+
if isinstance(renderable, (str, Text)):
312+
# Wrap text in a grid to handle the wrapping.
313+
text_grid = Table.grid(Column(overflow="fold"))
314+
text_grid.add_row(renderable)
315+
renderable = text_grid
316+
317+
return Padding.indent(renderable, level)
318+
319+
291320
def prepare_objects_for_rich_print(*objects: Any) -> tuple[RenderableType, ...]:
292321
"""Prepare a tuple of objects for printing by Rich's Console.print().
293322

0 commit comments

Comments
 (0)