diff --git a/lib/cli/cli.php b/lib/cli/cli.php index f449727..c20d47b 100755 --- a/lib/cli/cli.php +++ b/lib/cli/cli.php @@ -195,17 +195,21 @@ function safe_substr( $str, $start, $length = false ) { } /** - * An encoding-safe way of padding string length + * An encoding-safe way of padding string length for display * * @param string $string The string to pad * @param int $length The length to pad it to * @return string */ function safe_str_pad( $string, $length ) { - $real_length = safe_strlen($string); - $show_length = Colors::length($string); - $diff = strlen( $string ) - safe_strlen( $string ); - $length += $real_length - $show_length + $diff; - - return str_pad($string, $length); + // Hebrew vowel characters + $cleaned_string = preg_replace( '#[\x{591}-\x{5C7}]+#u', '', $string ); + if ( function_exists( 'mb_strwidth' ) ) { + $real_length = mb_strwidth( $cleaned_string, mb_detect_encoding( $string ) ); + } else { + $real_length = safe_strlen( $cleaned_string ); + } + $diff = strlen( $string ) - $real_length; + $length += $diff; + return str_pad( $string, $length ); } diff --git a/tests/test-cli.php b/tests/test-cli.php index 06540f3..c9b0f25 100644 --- a/tests/test-cli.php +++ b/tests/test-cli.php @@ -18,9 +18,16 @@ function test_encoded_string_length() { $this->assertEquals( \cli\Colors::length( 'hello' ), 5 ); $this->assertEquals( \cli\Colors::length( 'óra' ), 3 ); + $this->assertEquals( \cli\Colors::length( '日本語' ), 3 ); - $this->assertEquals( \cli\safe_strlen( \cli\Colors::pad( 'hello', 6 ) ), 6 ); - $this->assertEquals( \cli\safe_strlen( \cli\Colors::pad( 'óra', 6 ) ), 6 ); + } + + function test_encoded_string_pad() { + + $this->assertEquals( 6, strlen( \cli\Colors::pad( 'hello', 6 ) ) ); + $this->assertEquals( 7, strlen( \cli\Colors::pad( 'óra', 6 ) ) ); // special characters take one byte + $this->assertEquals( 9, strlen( \cli\Colors::pad( '日本語', 6 ) ) ); // each character takes two bytes + $this->assertEquals( 17, strlen( \cli\Colors::pad( 'עִבְרִית', 6 ) ) ); // process Hebrew vowels } @@ -28,6 +35,7 @@ function test_encoded_substr() { $this->assertEquals( \cli\safe_substr( \cli\Colors::pad( 'hello', 6), 0, 2 ), 'he' ); $this->assertEquals( \cli\safe_substr( \cli\Colors::pad( 'óra', 6), 0, 2 ), 'ór' ); + $this->assertEquals( \cli\safe_substr( \cli\Colors::pad( '日本語', 6), 0, 2 ), '日本' ); }