Skip to content

Commit

Permalink
Merge pull request #66 from wp-cli/fix-66
Browse files Browse the repository at this point in the history
More table formatting wonkiness
  • Loading branch information
danielbachhuber committed Sep 10, 2014
2 parents 0fa663f + 52d77b1 commit c35014e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
18 changes: 11 additions & 7 deletions lib/cli/cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
12 changes: 10 additions & 2 deletions tests/test-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,24 @@ 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

}

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 ), '日本' );

}

Expand Down

0 comments on commit c35014e

Please sign in to comment.