Skip to content

Commit

Permalink
PHP 8.2: strwidth() & Colors::pad()/decolorize() should always work w…
Browse files Browse the repository at this point in the history
…ith a string (#163)
  • Loading branch information
slaFFik authored Sep 29, 2023
1 parent d788a2c commit b3457a8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
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

0 comments on commit b3457a8

Please sign in to comment.