Ignore ANSI sequences when measuring string width #147
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Hello! This PR updates the
computeNumberOfLinesNeededToPrintStringInternal
function to ignore ANSI sequences from the string width measurement since these are not visibly printed to the terminal.Currently, ANSI sequences are included in this measurement which can cause the computed number of lines to be greater than the actual number of lines needed to print a string. When this happens, previous lines are unexpectedly erased from the screen.
Demo
Before changes
before.mov
After changes
after.mov
Reviewers
The following snippet can be used to demonstrate the erasure behavior on a terminal of width 80:
To test these changes, checkout this branch and update your
go.mod
to include:Notes
computeNumberOfLinesNeededToPrintStringInternal
function was updated to measure the line count in an iterative manner.computeNumberOfLinesNeededToPrintStringInternal
function were updated to use a table-driven testing approach. New tests were also added for checking expected behavior of strings containing ANSI sequences.