_contrib.py 1.3 KB

123456789101112131415161718192021222324252627282930313233
  1. # Source code: https://github.com/hamdanal/rich-argparse
  2. # MIT license: Copyright (c) Ali Hamdan <ali.hamdan.dev@gmail.com>
  3. # for internal use only
  4. from __future__ import annotations
  5. import rich_argparse._lazy_rich as r
  6. from rich_argparse._argparse import RichHelpFormatter
  7. from rich_argparse._common import rich_strip, rich_wrap
  8. class ParagraphRichHelpFormatter(RichHelpFormatter):
  9. """Rich help message formatter which retains paragraph separation."""
  10. def _rich_split_lines(self, text: r.Text, width: int) -> r.Lines:
  11. text = rich_strip(text)
  12. lines = r.Lines()
  13. for paragraph in text.split("\n\n"):
  14. # Normalize whitespace in the paragraph
  15. paragraph = self._rich_whitespace_sub(paragraph)
  16. # Wrap the paragraph to the specified width
  17. paragraph_lines = rich_wrap(self.console, paragraph, width)
  18. # Add the wrapped lines to the output
  19. lines.extend(paragraph_lines)
  20. # Add a blank line between paragraphs
  21. lines.append(r.Text("\n"))
  22. if lines: # pragma: no cover
  23. lines.pop() # Remove trailing newline
  24. return lines
  25. def _rich_fill_text(self, text: r.Text, width: int, indent: r.Text) -> r.Text:
  26. lines = self._rich_split_lines(text, width)
  27. return r.Text("\n").join(indent + line for line in lines) + "\n"