Skip to content

PHP 8.2: strwidth() & Colors::pad()/decolorize() should always work with a string #163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions lib/cli/Colors.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ static public function color($color) {
* Colorize a string using helpful string formatters. If the `Streams::$out` points to a TTY coloring will be enabled,
* otherwise disabled. You can control this check with the `$colored` parameter.
*
* @param string $string
* @param string $string
* @param boolean $colored Force enable or disable the colorized output. If left as `null` the TTY will control coloring.
* @return string
* @return string
*/
static public function colorize($string, $colored = null) {
$passed = $string;
Expand Down Expand Up @@ -146,6 +146,8 @@ static public function colorize($string, $colored = null) {
* @return string A string with color information removed.
*/
static public function decolorize( $string, $keep = 0 ) {
$string = (string) $string;

if ( ! ( $keep & 1 ) ) {
// Get rid of color tokens if they exist
$string = str_replace('%%', '%¾', $string);
Expand Down Expand Up @@ -182,7 +184,7 @@ static public function cacheString( $passed, $colorized, $deprecated = null ) {
* Return the length of the string without color codes.
*
* @param string $string the string to measure
* @return int
* @return int
*/
static public function length($string) {
return safe_strlen( self::decolorize( $string ) );
Expand All @@ -194,7 +196,7 @@ static public function length($string) {
* @param string $string The string to measure.
* @param bool $pre_colorized Optional. Set if the string is pre-colorized. Default false.
* @param string|bool $encoding Optional. The encoding of the string. Default false.
* @return int
* @return int
*/
static public function width( $string, $pre_colorized = false, $encoding = false ) {
return strwidth( $pre_colorized || self::shouldColorize() ? self::decolorize( $string, $pre_colorized ? 1 /*keep_tokens*/ : 0 ) : $string, $encoding );
Expand All @@ -208,9 +210,11 @@ static public function width( $string, $pre_colorized = false, $encoding = false
* @param bool $pre_colorized Optional. Set if the string is pre-colorized. Default false.
* @param string|bool $encoding Optional. The encoding of the string. Default false.
* @param int $pad_type Optional. Can be STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH. If pad_type is not specified it is assumed to be STR_PAD_RIGHT.
* @return string
* @return string
*/
static public function pad( $string, $length, $pre_colorized = false, $encoding = false, $pad_type = STR_PAD_RIGHT ) {
$string = (string) $string;

$real_length = self::width( $string, $pre_colorized, $encoding );
$diff = strlen( $string ) - $real_length;
$length += $diff;
Expand Down
2 changes: 2 additions & 0 deletions lib/cli/cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ function safe_str_pad( $string, $length, $encoding = false ) {
* @return int The string's width.
*/
function strwidth( $string, $encoding = false ) {
$string = (string) $string;

// Set the East Asian Width and Mark regexs.
list( $eaw_regex, $m_regex ) = get_unicode_regexs();

Expand Down