Skip to content

Commit c35014e

Browse files
Merge pull request #66 from wp-cli/fix-66
More table formatting wonkiness
2 parents 0fa663f + 52d77b1 commit c35014e

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

lib/cli/cli.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,17 +195,21 @@ function safe_substr( $str, $start, $length = false ) {
195195
}
196196

197197
/**
198-
* An encoding-safe way of padding string length
198+
* An encoding-safe way of padding string length for display
199199
*
200200
* @param string $string The string to pad
201201
* @param int $length The length to pad it to
202202
* @return string
203203
*/
204204
function safe_str_pad( $string, $length ) {
205-
$real_length = safe_strlen($string);
206-
$show_length = Colors::length($string);
207-
$diff = strlen( $string ) - safe_strlen( $string );
208-
$length += $real_length - $show_length + $diff;
209-
210-
return str_pad($string, $length);
205+
// Hebrew vowel characters
206+
$cleaned_string = preg_replace( '#[\x{591}-\x{5C7}]+#u', '', $string );
207+
if ( function_exists( 'mb_strwidth' ) ) {
208+
$real_length = mb_strwidth( $cleaned_string, mb_detect_encoding( $string ) );
209+
} else {
210+
$real_length = safe_strlen( $cleaned_string );
211+
}
212+
$diff = strlen( $string ) - $real_length;
213+
$length += $diff;
214+
return str_pad( $string, $length );
211215
}

tests/test-cli.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,24 @@ function test_encoded_string_length() {
1818

1919
$this->assertEquals( \cli\Colors::length( 'hello' ), 5 );
2020
$this->assertEquals( \cli\Colors::length( 'óra' ), 3 );
21+
$this->assertEquals( \cli\Colors::length( '日本語' ), 3 );
2122

22-
$this->assertEquals( \cli\safe_strlen( \cli\Colors::pad( 'hello', 6 ) ), 6 );
23-
$this->assertEquals( \cli\safe_strlen( \cli\Colors::pad( 'óra', 6 ) ), 6 );
23+
}
24+
25+
function test_encoded_string_pad() {
26+
27+
$this->assertEquals( 6, strlen( \cli\Colors::pad( 'hello', 6 ) ) );
28+
$this->assertEquals( 7, strlen( \cli\Colors::pad( 'óra', 6 ) ) ); // special characters take one byte
29+
$this->assertEquals( 9, strlen( \cli\Colors::pad( '日本語', 6 ) ) ); // each character takes two bytes
30+
$this->assertEquals( 17, strlen( \cli\Colors::pad( 'עִבְרִית', 6 ) ) ); // process Hebrew vowels
2431

2532
}
2633

2734
function test_encoded_substr() {
2835

2936
$this->assertEquals( \cli\safe_substr( \cli\Colors::pad( 'hello', 6), 0, 2 ), 'he' );
3037
$this->assertEquals( \cli\safe_substr( \cli\Colors::pad( 'óra', 6), 0, 2 ), 'ór' );
38+
$this->assertEquals( \cli\safe_substr( \cli\Colors::pad( '日本語', 6), 0, 2 ), '日本' );
3139

3240
}
3341

0 commit comments

Comments
 (0)