Improve styling of wrapped text in Console output.
Review Request #12393 — Created June 20, 2022 and submitted
Consoleattempts to nicely style and wrap text for display to the
user. There were a couple problems, though, in the appearance of wrapped
text, especially when containing styling.
When using a prefix for wrapped lines (such as "Warning:"), the prefix
would end up applying to each paragraph. This was noisy and made it hard
to differentiate one block from another. We now treat the "subsequent
indent" as the "initial indent" for all but the first paragraph,
ensuring only that first paragraph has the prefix. All other paragraphs
will properly align, and look correct.
The second issue is more tricky.
textwrapisn't aware of ANSI
character codes, so any styling we apply to the prefix ends up reducing
the available width of that line.
There are third-party modules available for this, but at this time it's
not worth introducing another dependency for this. The only things
textwrapcares about is the ability to call
len()on the string, and
to concatenate strings together.
We now use a string wrapper,
_StyledWrapperIndent, to wrap the string
and provide these capabilities.
__len__returns the non-styled length,
This is kind of a hack, but
textwrapdoesn't appear to change all that
often, and certainly has not between the versions of Python currently
supported. Unit tests will ensure it doesn't break unexpectedly.
Unit tests passed.
Manually ran the upgrade process and triggered errors. Saw the
correctly-formatted, wrapped text with consistent line widths.
local variable 'lines' is assigned to but never used Column: 13 Error code: F841
Removed an unused variable.
Revision 2 (+264 -14)
Checks run (2 succeeded)