wp-cli / php-cli-tools

A collection of tools to help with PHP command line utilities
MIT License
672 stars 118 forks source link

PR 109 (double-width wrapping) and issue 106 (pre-colorization). #111

Closed gitlost closed 7 years ago

gitlost commented 7 years ago

PR https://github.com/wp-cli/php-cli-tools/pull/109 and issue https://github.com/wp-cli/php-cli-tools/issues/106

For PR https://github.com/wp-cli/php-cli-tools/pull/109 (incorrect wrapping of East Asian double-width chars in tables), adds an is_width arg to safe_substr() to make it interpret the length arg as a spacing width, and uses the East Asian Width regex (now refactored to load in a function) to adjust accordingly (a slow algorithm but couldn't think of anything better). Uses this in Ascii::row().

For issue https://github.com/wp-cli/php-cli-tools/issues/106 (related https://github.com/wp-cli/wp-cli/pull/2458), adds pre_colorized arg to various functions, and adds Ascii::set/isPreColorized() and Table::set/isAsciiPreColorize() (the latter to be used via a corresponding ascii_pre_colorized arg to be added to WP_CLI/Formatter, see https://github.com/gitlost/wp-cli/pull/15.)

Also centralizes decolorization into Colors::decolorize(), adding a keep arg to optionally keep either tokens or encodings.

Also makes sure Colors::colorize() only reads from cache after checking shouldColorize().

Also marks as unused the colored arg to Colors::cacheString(), and stops using the decolorized cache element in length() and width() as it never gets looked up if passed a colorized string (as they're indexed on the original string).

Also replicates the padding logic of safe_str_pad() in Colors::pad() and moves decolorization to Colors::width(), as it seems better that low-level funcs don't reference Colors.

Also makes length arg of safe_substr() cross PHP compat whether null or false.

Also adds an optional encoding arg to various functions to save some processing, and makes mb_detect_encoding() use strict.

danielbachhuber commented 7 years ago

Looks good on my end. Flagging a review from @miya0001 for a double-check

gitlost commented 7 years ago

Thanks @miya0001 - it's great to have a native kanji and kana user on board!

danielbachhuber commented 7 years ago

Thanks for your work on this, @gitlost and @miya0001. I've tagged a v0.11.4 release.