-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add contrib module with ParagraphRichHelpFormatter (#147)
Closes #140
- Loading branch information
Showing
16 changed files
with
399 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Source code: https://github.com/hamdanal/rich-argparse | ||
# MIT license: Copyright (c) Ali Hamdan <ali.hamdan.dev@gmail.com> | ||
|
||
# for internal use only | ||
from __future__ import annotations | ||
|
||
import rich_argparse._lazy_rich as r | ||
from rich_argparse._argparse import RichHelpFormatter | ||
from rich_argparse._common import rich_strip, rich_wrap | ||
|
||
|
||
class ParagraphRichHelpFormatter(RichHelpFormatter): | ||
"""Rich help message formatter which retains paragraph separation.""" | ||
|
||
def _rich_split_lines(self, text: r.Text, width: int) -> r.Lines: | ||
text = rich_strip(text) | ||
lines = r.Lines() | ||
for paragraph in text.split("\n\n"): | ||
# Normalize whitespace in the paragraph | ||
paragraph = self._rich_whitespace_sub(paragraph) | ||
# Wrap the paragraph to the specified width | ||
paragraph_lines = rich_wrap(self.console, paragraph, width) | ||
# Add the wrapped lines to the output | ||
lines.extend(paragraph_lines) | ||
# Add a blank line between paragraphs | ||
lines.append(r.Text("\n")) | ||
if lines: # pragma: no cover | ||
lines.pop() # Remove trailing newline | ||
return lines | ||
|
||
def _rich_fill_text(self, text: r.Text, width: int, indent: r.Text) -> r.Text: | ||
lines = self._rich_split_lines(text, width) | ||
return r.Text("\n").join(indent + line for line in lines) + "\n" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Source code: https://github.com/hamdanal/rich-argparse | ||
# MIT license: Copyright (c) Ali Hamdan <ali.hamdan.dev@gmail.com> | ||
"""Extra formatters for rich help messages. | ||
The rich_argparse.contrib module contains optional, standard implementations of common patterns of | ||
rich help message formatting. These formatters are not included in the main rich_argparse module | ||
because they do not translate directly to argparse formatters. | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
from rich_argparse._contrib import ParagraphRichHelpFormatter | ||
|
||
__all__ = [ | ||
"ParagraphRichHelpFormatter", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.