Improve styling of wrapped text in Console output.
Review Request #12393 — Created June 20, 2022 and submitted — Latest diff uploaded
Console
attempts 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.
textwrap
isn'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
textwrap
cares about is the ability to calllen()
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,
and__add__
helps concatenate.This is kind of a hack, but
textwrap
doesn'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.