Skip to content

Commit

Permalink
Merge pull request #69 from armab/bug/colorized-strings-in-ascii-table
Browse files Browse the repository at this point in the history
Fixed incorrect lengths for colorized strings in ASCII table
  • Loading branch information
danielbachhuber committed Nov 12, 2014
2 parents d75d532 + b57396d commit 98db678
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/cli/cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,14 @@ function safe_substr( $str, $start, $length = false ) {
*/
function safe_str_pad( $string, $length ) {
// Hebrew vowel characters
$cleaned_string = preg_replace( '#[\x{591}-\x{5C7}]+#u', '', $string );
$cleaned_string = preg_replace( '#[\x{591}-\x{5C7}]+#u', '', Colors::decolorize( $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 );
}
4 changes: 4 additions & 0 deletions tests/test-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ function test_encoded_string_pad() {
$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_colorized_string_pad() {
$this->assertEquals( 22, strlen( \cli\Colors::pad( \cli\Colors::colorize( "%Gx%n", true ), 11 ))); // colorized `x` string
$this->assertEquals( 23, strlen( \cli\Colors::pad( \cli\Colors::colorize( "%Góra%n", true ), 11 ))); // colorized `óra` string
}

function test_encoded_substr() {
Expand Down
30 changes: 30 additions & 0 deletions tests/test-table-ascii.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,36 @@ public function testDrawOneColumnTable() {
| x |
+-------------+

OUT;
$this->assertInOutEquals(array($headers, $rows), $output);
}

/**
* Draw simple One column table with colored string
* Output should look like:
* +-------------+
* | Test Header |
* +-------------+
* | x |
* +-------------+
*
* where `x` character has green color.
* At the same time it checks that `green` defined in `cli\Colors` really looks as `green`.
*/
public function testDrawOneColumnColoredTable() {
$headers = array('Test Header');
$rows = array(
array(Colors::colorize('%Gx%n', true)),
);
// green `x`
$x = "\x1B\x5B\x33\x32\x3B\x31\x6Dx\x1B\x5B\x30\x6D";
$output = <<<OUT
+-------------+
| Test Header |
+-------------+
| $x |
+-------------+
OUT;
$this->assertInOutEquals(array($headers, $rows), $output);
}
Expand Down

0 comments on commit 98db678

Please sign in to comment.