René's URL Explorer Experiment


Title: Colors::length returns incorrect lengths for colorized strings · Issue #59 · wp-cli/php-cli-tools · GitHub

Open Graph Title: Colors::length returns incorrect lengths for colorized strings · Issue #59 · wp-cli/php-cli-tools

X Title: Colors::length returns incorrect lengths for colorized strings · Issue #59 · wp-cli/php-cli-tools

Description: When you pass a string to Colors::length that has already been colorized with Colors::colorized, the wrong result is given. This issue resulted in incorrectly formatted tables using \cli\Table::display(). To reproduce: Add the following ...

Open Graph Description: When you pass a string to Colors::length that has already been colorized with Colors::colorized, the wrong result is given. This issue resulted in incorrectly formatted tables using \cli\Table::dis...

X Description: When you pass a string to Colors::length that has already been colorized with Colors::colorized, the wrong result is given. This issue resulted in incorrectly formatted tables using \cli\Table::dis...

Opengraph URL: https://github.com/wp-cli/php-cli-tools/issues/59

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Colors::length returns incorrect lengths for colorized strings","articleBody":"When you pass a string to `Colors::length` that has already been colorized with `Colors::colorized`, the wrong result is given. This issue resulted in incorrectly formatted tables using `\\cli\\Table::display()`.\n\nTo reproduce:\n1. Add the following WP CLI command:\n   \n   ``` php\n   \u003c?php\n   \n   class ReducedTestCase extends WP_CLI_Command {\n     function test() {\n         $success = \\cli\\Colors::colorize( \"%Gx%n\" );\n   \n         // Display results\n         $table = new \\cli\\Table();\n         $table->setHeaders( array( 'Test Header' ) );\n         $table->setRows( array( array( $success )) );\n         $table->display();\n     }\n   }\n   \n   WP_CLI::add_command( 'failwhale', 'ReducedTestCase' );\n   ```\n2. The output produced is:\n   \n   ``` bash\n   vagrant@vvv:/srv/www/wordpress-develop/src$ wp failwhale test\n   +--------------+\n   | Test Header  |\n   +--------------+\n   | x |\n   +--------------+\n   ```\n   \n   Note that the output _is colorized_ even though I cannot indicate that in the code above.\n3. The expected output is:\n   \n   ``` bash\n   vagrant@vvv:/srv/www/wordpress-develop/src$ wp failwhale test\n   +--------------+\n   | Test Header  |\n   +--------------+\n   | x            |\n   +--------------+\n   ```\n\nThis problem is occurring because before the content is passed to the table methods, it is colorized. Colorizing the string turns it from:\n\n`%Gx%n` to something like `\\033[32;1mx\\033[0m`\n\nThe string length counts are thusly messed up. In fact, I'd argue that the string lengths, for any version of colorized values have always been messed up. The first value above would return a length of `5` and the second a value of `18` when really, the string length should be `1` as the only non-color value is `x`.\n\nI was able to implement a really sloppy proof of concept fix for the issue:\n\n``` php\nstatic public function length($string) {\n    // Convert the string back to it's original state\n    $conversions = array(\n        '%y' => array('color' => 'yellow'),\n        '%g' => array('color' => 'green'),\n        '%b' => array('color' => 'blue'),\n        '%r' => array('color' => 'red'),\n        '%p' => array('color' => 'magenta'),\n        '%m' => array('color' => 'magenta'),\n        '%c' => array('color' => 'cyan'),\n        '%w' => array('color' => 'grey'),\n        '%k' => array('color' => 'black'),\n        '%n' => array('color' => 'reset'),\n        '%Y' => array('color' => 'yellow', 'style' => 'bright'),\n        '%G' => array('color' => 'green', 'style' => 'bright'),\n        '%B' => array('color' => 'blue', 'style' => 'bright'),\n        '%R' => array('color' => 'red', 'style' => 'bright'),\n        '%P' => array('color' => 'magenta', 'style' => 'bright'),\n        '%M' => array('color' => 'magenta', 'style' => 'bright'),\n        '%C' => array('color' => 'cyan', 'style' => 'bright'),\n        '%W' => array('color' => 'grey', 'style' => 'bright'),\n        '%K' => array('color' => 'black', 'style' => 'bright'),\n        '%N' => array('color' => 'reset', 'style' => 'bright'),\n        '%3' => array('background' => 'yellow'),\n        '%2' => array('background' => 'green'),\n        '%4' => array('background' => 'blue'),\n        '%1' => array('background' => 'red'),\n        '%5' => array('background' => 'magenta'),\n        '%6' => array('background' => 'cyan'),\n        '%7' => array('background' => 'grey'),\n        '%0' => array('background' => 'black'),\n        '%F' => array('style' => 'blink'),\n        '%U' => array('style' => 'underline'),\n        '%8' => array('style' => 'inverse'),\n        '%9' => array('style' => 'bright'),\n        '%_' => array('style' => 'bright')\n    );\n\n    foreach ($conversions as $key => $value) {\n        $string = str_replace(self::color($value), '', self::colorize($string, true));\n    }\n\n    return safe_strlen(self::colorize($string, true));\n}\n```\n\nAs a proof of concept, this simply converts the colorized string to the original string without any color information. This gets the _true_ string length value. Furthermore, this gives the expected results in the table display. This is a sloppy fix and it produced noticeable performance issues.\n\nI think there are a lot of different approaches to fixing this issue. An approach that I think could work would be too:\n1. Whenever a new value is passed to `Colors::colorize`, store that value in memory and specifically track:\n   - String without _any color information_\n   - The original string value\n   - The colorized value\n2. Change the `Colors::length` method to do a simple `strlen` call on the _string without any color information_.\n\nI think this method would be backwards compatible, and provide a nice performance boost by removing redundant color conversions.\n\nAdditionally, I think some unit testing should be added to show how broken these methods are.\n","author":{"url":"https://github.com/tollmanz","@type":"Person","name":"tollmanz"},"datePublished":"2014-08-03T07:11:37Z","url":"https://github.com/wp-cli/php-cli-tools/issues/59"}

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf
route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:698c4f60-e4c9-f5eb-d125-c41864bded5c
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idB3F8:2B0961:3C2F293:4EEF12C:6AFA1786
html-safe-noncead1965db80bd2deeed6cc22c4d035901d9398eea548d10e3601135d16fda660e
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCM0Y4OjJCMDk2MTozQzJGMjkzOjRFRUYxMkM6NkFGQTE3ODYiLCJ2aXNpdG9yX2lkIjoiODI5MzQxMzIwMzE1ODAzNTI2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac69825a3849a8df61e837e1ad1c1cccea25c4b1f8ee7bf8ad6611550f20265d06
hovercard-subject-tagissue:39365096
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/wp-cli/php-cli-tools/59/issue_layout
twitter:imagehttps://opengraph.githubassets.com/c5c95027b00f72024623f3d57f607f1ec40a554b66edbb3ee8d4c32b2146b7c0/wp-cli/php-cli-tools/issues/59
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/c5c95027b00f72024623f3d57f607f1ec40a554b66edbb3ee8d4c32b2146b7c0/wp-cli/php-cli-tools/issues/59
og:image:altWhen you pass a string to Colors::length that has already been colorized with Colors::colorized, the wrong result is given. This issue resulted in incorrectly formatted tables using \cli\Table::dis...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernametollmanz
hostnamegithub.com
expected-hostnamegithub.com
None612d3e6ffd693e7ea8d9fcf452370995872baef31d825b1fedef704cd3d24ac7
turbo-cache-controlno-preview
go-importgithub.com/wp-cli/php-cli-tools git https://github.com/wp-cli/php-cli-tools.git
octolytics-dimension-user_id1570774
octolytics-dimension-user_loginwp-cli
octolytics-dimension-repository_id587642
octolytics-dimension-repository_nwowp-cli/php-cli-tools
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id587642
octolytics-dimension-repository_network_root_nwowp-cli/php-cli-tools
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release072e34035c13c31543fba9f025cbbbda4210dae4
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/wp-cli/php-cli-tools/issues/59#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fwp-cli%2Fphp-cli-tools%2Fissues%2F59
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fwp-cli%2Fphp-cli-tools%2Fissues%2F59
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=wp-cli%2Fphp-cli-tools
Reloadhttps://github.com/wp-cli/php-cli-tools/issues/59
Reloadhttps://github.com/wp-cli/php-cli-tools/issues/59
Reloadhttps://github.com/wp-cli/php-cli-tools/issues/59
wp-clihttps://github.com/wp-cli
php-cli-toolshttps://github.com/wp-cli/php-cli-tools
Codehttps://github.com/wp-cli/php-cli-tools
Issueshttps://github.com/wp-cli/php-cli-tools/issues
Pull requestshttps://github.com/wp-cli/php-cli-tools/pulls
Actionshttps://github.com/wp-cli/php-cli-tools/actions
Security and qualityhttps://github.com/wp-cli/php-cli-tools/security
Insightshttps://github.com/wp-cli/php-cli-tools/pulse
https://github.com/wp-cli/php-cli-tools
New issuehttps://github.com/login?return_to=https://github.com/wp-cli/php-cli-tools/issues/59
New issuehttps://github.com/login?return_to=https://github.com/wp-cli/php-cli-tools/issues/59
Colors::length returns incorrect lengths for colorized stringshttps://github.com/wp-cli/php-cli-tools/issues/59#top
bughttps://github.com/wp-cli/php-cli-tools/issues?q=state%3Aopen%20label%3A%22bug%22
https://github.com/tollmanz
tollmanzhttps://github.com/tollmanz
on Aug 3, 2014https://github.com/wp-cli/php-cli-tools/issues/59#issue-39365096
https://github.com/danielbachhuber
danielbachhuberhttps://github.com/danielbachhuber
on Aug 3, 2014https://github.com/wp-cli/php-cli-tools/issues/59#issuecomment-50997743
#53https://github.com/wp-cli/php-cli-tools/pull/53
danielbachhuberhttps://github.com/danielbachhuber
bughttps://github.com/wp-cli/php-cli-tools/issues?q=state%3Aopen%20label%3A%22bug%22
on Aug 3, 2014https://github.com/wp-cli/php-cli-tools/issues/59#event-148548561
https://github.com/tollmanz
tollmanzhttps://github.com/tollmanz
on Aug 3, 2014https://github.com/wp-cli/php-cli-tools/issues/59#issuecomment-50998665
#60https://github.com/wp-cli/php-cli-tools/pull/60
tollmanzhttps://github.com/tollmanz
completedhttps://github.com/wp-cli/php-cli-tools/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on Aug 3, 2014https://github.com/wp-cli/php-cli-tools/issues/59#event-148551145
https://github.com/arm4b
arm4bhttps://github.com/arm4b
on Oct 20, 2014https://github.com/wp-cli/php-cli-tools/issues/59#issuecomment-59736722
@tollmanzhttps://github.com/tollmanz
https://github.com/danielbachhuber
danielbachhuberhttps://github.com/danielbachhuber
on Oct 20, 2014https://github.com/wp-cli/php-cli-tools/issues/59#issuecomment-59768399
https://github.com/arm4b
arm4bhttps://github.com/arm4b
on Oct 20, 2014https://github.com/wp-cli/php-cli-tools/issues/59#issuecomment-59770275
Sign up for freehttps://github.com/signup?return_to=https://github.com/wp-cli/php-cli-tools/issues/59
Sign in to commenthttps://github.com/login?return_to=https://github.com/wp-cli/php-cli-tools/issues/59
bughttps://github.com/wp-cli/php-cli-tools/issues?q=state%3Aopen%20label%3A%22bug%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.