Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions cmd2/argparse_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ def get_items(self) -> list[CompletionItems]:
RenderableType,
)
from rich.protocol import is_renderable
from rich.table import Column, Table
from rich.table import Column
from rich.text import Text
from rich_argparse import (
ArgumentDefaultsRichHelpFormatter,
Expand All @@ -295,6 +295,7 @@ def get_items(self) -> list[CompletionItems]:
)

from . import constants
from . import rich_utils as ru
from .rich_utils import Cmd2RichArgparseConsole
from .styles import Cmd2Style

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

# Left pad the text like an argparse argument group does
left_padding = formatter._indent_increment
text_table = Table(
Column(overflow="fold"),
box=None,
show_header=False,
padding=(0, 0, 0, left_padding),
)
text_table.add_row(self.text)
# Indent text like an argparse argument group does
indented_text = ru.indent(self.text, formatter._indent_increment)

return Group(styled_title, text_table)
return Group(styled_title, indented_text)


class Cmd2ArgumentParser(argparse.ArgumentParser):
Expand Down
14 changes: 4 additions & 10 deletions cmd2/cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@

import rich.box
from rich.console import Group
from rich.padding import Padding
from rich.rule import Rule
from rich.style import Style, StyleType
from rich.table import (
Expand Down Expand Up @@ -4076,9 +4075,8 @@ def do_help(self, args: argparse.Namespace) -> None:
# Indent doc_leader to align with the help tables.
self.poutput()
self.poutput(
Padding.indent(self.doc_leader, 1),
ru.indent(self.doc_leader, 1),
style=Cmd2Style.HELP_LEADER,
soft_wrap=False,
)
self.poutput()

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

# Print the topics in columns.
# Subtract 1 from maxcol to account for indentation.
maxcol = min(maxcol, ru.console_width()) - 1
columnized_cmds = self.render_columns(cmds, maxcol)
self.poutput(Padding.indent(columnized_cmds, 1), soft_wrap=False)
self.poutput(ru.indent(columnized_cmds, 1))
self.poutput()

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

# Indent header to align with the help tables.
self.poutput(
Padding.indent(header, 1),
style=Cmd2Style.HELP_HEADER,
soft_wrap=False,
)
self.poutput(ru.indent(header, 1), style=Cmd2Style.HELP_HEADER)
topics_table = Table(
Column("Name", no_wrap=True),
Column("Description", overflow="fold"),
Expand Down
29 changes: 29 additions & 0 deletions cmd2/rich_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
RenderableType,
RichCast,
)
from rich.padding import Padding
from rich.style import StyleType
from rich.table import (
Column,
Table,
)
from rich.text import Text
from rich.theme import Theme
from rich_argparse import RichHelpFormatter
Expand Down Expand Up @@ -288,6 +293,30 @@ def string_to_rich_text(text: str) -> Text:
return result


def indent(renderable: RenderableType, level: int) -> Padding:
"""Indent a Rich renderable.

When soft-wrapping is enabled, a Rich console is unable to properly print a
Padding object of indented text, as it truncates long strings instead of wrapping
them. This function provides a workaround for this issue, ensuring that indented
text is printed correctly regardless of the soft-wrap setting.

For non-text objects, this function merely serves as a convenience
wrapper around Padding.indent().

:param renderable: a Rich renderable to indent.
:param level: number of characters to indent.
:return: a Padding object containing the indented content.
"""
if isinstance(renderable, (str, Text)):
# Wrap text in a grid to handle the wrapping.
text_grid = Table.grid(Column(overflow="fold"))
text_grid.add_row(renderable)
renderable = text_grid

return Padding.indent(renderable, level)


def prepare_objects_for_rich_print(*objects: Any) -> tuple[RenderableType, ...]:
"""Prepare a tuple of objects for printing by Rich's Console.print().

Expand Down
Loading