Reflow text to a nice width for terminals. WARNING: does not handle gracefully things like .. versionadded::
(docstring, has_first_line=True, width=75)
| 35 | |
| 36 | |
| 37 | def _reflow_param_docstring(docstring, has_first_line=True, width=75): |
| 38 | """Reflow text to a nice width for terminals. |
| 39 | |
| 40 | WARNING: does not handle gracefully things like .. versionadded:: |
| 41 | """ |
| 42 | maxsplit = docstring.count("\n") - 1 if has_first_line else -1 |
| 43 | merged = " ".join( |
| 44 | line.strip() for line in docstring.rsplit("\n", maxsplit=maxsplit) |
| 45 | ) |
| 46 | reflowed = "\n ".join(re.findall(rf".{{1,{width}}}(?:\s+|$)", merged)) |
| 47 | if has_first_line: |
| 48 | reflowed = reflowed.replace("\n \n", "\n", 1) |
| 49 | return reflowed |
| 50 | |
| 51 | |
| 52 | ############################################################################## |