Skip to content

Commit c3b7f38

Browse files
committed
Patch up long lines in choices
1 parent eba010c commit c3b7f38

2 files changed

Lines changed: 28 additions & 6 deletions

File tree

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ usage: fractured-json [-h] [-V] [--output OUTPUT]
4040
[--max-total-line-length N]
4141
[--min-compact-array-row-items N]
4242
[--nested-bracket-padding]
43-
[--number-list-alignment {LEFT,RIGHT,DECIMAL,NORMALIZE}]
43+
[--number-list-alignment {LEFT,RIGHT,DECIMAL,
44+
NORMALIZE}]
4445
[--prefix-string PREFIX_STRING]
4546
[--preserve-blank-lines] [--simple-bracket-padding]
46-
[--table-comma-placement {BEFORE_PADDING,AFTER_PADDING,BEFORE_PADDING_EXCEPT_NUMBERS}]
47+
[--table-comma-placement {BEFORE_PADDING,AFTER_PADDING,
48+
BEFORE_PADDING_EXCEPT_NUMBERS}]
4749
[--use-tab-to-indent] [--east-asian-chars]
4850
[json ...]
4951

@@ -149,7 +151,8 @@ options:
149151
array/object has a complexity of 1. That is, if it
150152
only contains primitive elements and/or empty
151153
arrays/objects. (default=False)
152-
--table-comma-placement {BEFORE_PADDING,AFTER_PADDING,BEFORE_PADDING_EXCEPT_NUMBERS}
154+
--table-comma-placement {BEFORE_PADDING,AFTER_PADDING,
155+
BEFORE_PADDING_EXCEPT_NUMBERS}
153156
Where to place commas in table-formatted elements.
154157
(default=BEFORE_PADDING_EXCEPT_NUMBERS)
155158
--use-tab-to-indent If true, a single tab character is used per

src/build/generate_readme.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,39 @@
55
from pathlib import Path
66
from typing import Final
77

8+
MAX_COLUMNS = 76
9+
10+
11+
def last_comma_after_pos(s: str, start: int) -> int:
12+
"""Find index of last comma in s after a start position."""
13+
return s.rfind(",", start)
14+
815

916
def main() -> None:
1017
docs_readme: Final[Path] = Path("docs/README.md")
1118
out_readme: Final[Path] = Path("README.md")
1219

13-
environ["COLUMNS"] = "76"
20+
environ["COLUMNS"] = str(MAX_COLUMNS)
1421
result = subprocess.run(
1522
["fractured-json", "--help"], # noqa: S607
1623
check=True,
1724
capture_output=True,
1825
text=True,
19-
env={**subprocess.os.environ, "COLUMNS": "76"},
2026
)
21-
help_text = result.stdout
27+
28+
help_text = ""
29+
for line in result.stdout.splitlines():
30+
if len(line) > MAX_COLUMNS and "{" in line:
31+
# argparse doesn't break choices across lines so
32+
# find the last fitting choice and insert a bread
33+
# padding the next line to line up to the brace position
34+
choice_start = line.find("{") + 1
35+
choice_break = line.rfind(",", choice_start, MAX_COLUMNS + 1) + 1
36+
help_text += line[:choice_break] + "\n"
37+
help_text += " " * choice_start
38+
help_text += line[choice_break:] + "\n"
39+
else:
40+
help_text += line + "\n"
2241

2342
text = docs_readme.read_text(encoding="utf-8")
2443

0 commit comments

Comments
 (0)