Skip to content

Commit

Permalink
Fix whitespace formatting in CLI help. (#917)
Browse files Browse the repository at this point in the history
  • Loading branch information
Carreau authored Feb 4, 2025
1 parent 5bacf10 commit e29ce7c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion traitlets/config/configurable.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ def class_get_trait_help(
if helptext is None:
helptext = trait.help
if helptext != "":
helptext = "\n".join(wrap_paragraphs(helptext, 76))
helptext = "\n\n".join(wrap_paragraphs(helptext, 76))
lines.append(indent(helptext))

if "Enum" in trait.__class__.__name__:
Expand Down
22 changes: 20 additions & 2 deletions traitlets/utils/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import re
import textwrap
from textwrap import dedent
from textwrap import indent as _indent
from typing import List

Expand All @@ -14,6 +13,25 @@ def indent(val: str) -> str:
return _indent(val, " ")


def _dedent(text: str) -> str:
"""Equivalent of textwrap.dedent that ignores unindented first line."""

if text.startswith("\n"):
# text starts with blank line, don't ignore the first line
return textwrap.dedent(text)

# split first line
splits = text.split("\n", 1)
if len(splits) == 1:
# only one line
return textwrap.dedent(text)

first, rest = splits
# dedent everything but the first line
rest = textwrap.dedent(rest)
return "\n".join([first, rest])


def wrap_paragraphs(text: str, ncols: int = 80) -> List[str]:
"""Wrap multiple paragraphs to fit a specified width.
Expand All @@ -26,7 +44,7 @@ def wrap_paragraphs(text: str, ncols: int = 80) -> List[str]:
list of complete paragraphs, wrapped to fill `ncols` columns.
"""
paragraph_re = re.compile(r"\n(\s*\n)+", re.MULTILINE)
text = dedent(text).strip()
text = _dedent(text).strip()
paragraphs = paragraph_re.split(text)[::2] # every other entry is space
out_ps = []
indent_re = re.compile(r"\n\s+", re.MULTILINE)
Expand Down

0 comments on commit e29ce7c

Please sign in to comment.