diff --git a/lib/cli/cli.php b/lib/cli/cli.php index c20d47b..9ea4e21 100755 --- a/lib/cli/cli.php +++ b/lib/cli/cli.php @@ -203,7 +203,7 @@ 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 { @@ -211,5 +211,6 @@ function safe_str_pad( $string, $length ) { } $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 c9b0f25..5c94ef8 100644 --- a/tests/test-cli.php +++ b/tests/test-cli.php @@ -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() { diff --git a/tests/test-table-ascii.php b/tests/test-table-ascii.php index e1fb588..c8d558c 100644 --- a/tests/test-table-ascii.php +++ b/tests/test-table-ascii.php @@ -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 = <<assertInOutEquals(array($headers, $rows), $output); }