Skip to content

Remove ANSI color sequences when calculating width of display text#4066

Closed
rhysd wants to merge 1 commit intocli:trunkfrom
rhysd:issue-4065
Closed

Remove ANSI color sequences when calculating width of display text#4066
rhysd wants to merge 1 commit intocli:trunkfrom
rhysd:issue-4065

Conversation

@rhysd
Copy link

@rhysd rhysd commented Jul 30, 2021

Fixes #4065

This PR fixes the example shown in the issue as follows:

スクリーンショット 2021-07-31 3 01 26

The start positions of issue date column are aligned correctly.


// DisplayWidth calculates what the rendered width of a string may be
func DisplayWidth(s string) int {
s = reColorSequence.ReplaceAllString(s, "")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the straightforward fix!

Note that an older PR https://github.com/cli/cli/pull/3519/files#diff-7cd07c3defa4823b12484e616fc8cb5f63dc01e5a9df23544634b111a8d20547R15 also proposes ignoring ANSI escape sequences when measuring width of text, but uses a library that does this without a regular expression. I would personally vote for that approach since it might be computationally lighter. However, that PR isn't reviewed yet. Please allow us some time to decide how to proceed.

@mislav mislav closed this in #4079 Aug 4, 2021
@rhysd rhysd deleted the issue-4065 branch August 4, 2021 14:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong calculation of width of text including ANSI color sequences

2 participants