Skip to content

Commit e29ce7c

Browse files
authored
Fix whitespace formatting in CLI help. (#917)
1 parent 5bacf10 commit e29ce7c

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

traitlets/config/configurable.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ def class_get_trait_help(
300300
if helptext is None:
301301
helptext = trait.help
302302
if helptext != "":
303-
helptext = "\n".join(wrap_paragraphs(helptext, 76))
303+
helptext = "\n\n".join(wrap_paragraphs(helptext, 76))
304304
lines.append(indent(helptext))
305305

306306
if "Enum" in trait.__class__.__name__:

traitlets/utils/text.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import re
77
import textwrap
8-
from textwrap import dedent
98
from textwrap import indent as _indent
109
from typing import List
1110

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

1615

16+
def _dedent(text: str) -> str:
17+
"""Equivalent of textwrap.dedent that ignores unindented first line."""
18+
19+
if text.startswith("\n"):
20+
# text starts with blank line, don't ignore the first line
21+
return textwrap.dedent(text)
22+
23+
# split first line
24+
splits = text.split("\n", 1)
25+
if len(splits) == 1:
26+
# only one line
27+
return textwrap.dedent(text)
28+
29+
first, rest = splits
30+
# dedent everything but the first line
31+
rest = textwrap.dedent(rest)
32+
return "\n".join([first, rest])
33+
34+
1735
def wrap_paragraphs(text: str, ncols: int = 80) -> List[str]:
1836
"""Wrap multiple paragraphs to fit a specified width.
1937
@@ -26,7 +44,7 @@ def wrap_paragraphs(text: str, ncols: int = 80) -> List[str]:
2644
list of complete paragraphs, wrapped to fill `ncols` columns.
2745
"""
2846
paragraph_re = re.compile(r"\n(\s*\n)+", re.MULTILINE)
29-
text = dedent(text).strip()
47+
text = _dedent(text).strip()
3048
paragraphs = paragraph_re.split(text)[::2] # every other entry is space
3149
out_ps = []
3250
indent_re = re.compile(r"\n\s+", re.MULTILINE)

0 commit comments

Comments
 (0)