Skip to content

Commit 98db678

Browse files
Merge pull request #69 from armab/bug/colorized-strings-in-ascii-table
Fixed incorrect lengths for colorized strings in ASCII table
2 parents d75d532 + b57396d commit 98db678

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

lib/cli/cli.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,14 @@ function safe_substr( $str, $start, $length = false ) {
203203
*/
204204
function safe_str_pad( $string, $length ) {
205205
// Hebrew vowel characters
206-
$cleaned_string = preg_replace( '#[\x{591}-\x{5C7}]+#u', '', $string );
206+
$cleaned_string = preg_replace( '#[\x{591}-\x{5C7}]+#u', '', Colors::decolorize( $string ) );
207207
if ( function_exists( 'mb_strwidth' ) ) {
208208
$real_length = mb_strwidth( $cleaned_string, mb_detect_encoding( $string ) );
209209
} else {
210210
$real_length = safe_strlen( $cleaned_string );
211211
}
212212
$diff = strlen( $string ) - $real_length;
213213
$length += $diff;
214+
214215
return str_pad( $string, $length );
215216
}

tests/test-cli.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ function test_encoded_string_pad() {
2828
$this->assertEquals( 7, strlen( \cli\Colors::pad( 'óra', 6 ) ) ); // special characters take one byte
2929
$this->assertEquals( 9, strlen( \cli\Colors::pad( '日本語', 6 ) ) ); // each character takes two bytes
3030
$this->assertEquals( 17, strlen( \cli\Colors::pad( 'עִבְרִית', 6 ) ) ); // process Hebrew vowels
31+
}
3132

33+
function test_colorized_string_pad() {
34+
$this->assertEquals( 22, strlen( \cli\Colors::pad( \cli\Colors::colorize( "%Gx%n", true ), 11 ))); // colorized `x` string
35+
$this->assertEquals( 23, strlen( \cli\Colors::pad( \cli\Colors::colorize( "%Góra%n", true ), 11 ))); // colorized `óra` string
3236
}
3337

3438
function test_encoded_substr() {

tests/test-table-ascii.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,36 @@ public function testDrawOneColumnTable() {
5757
| x |
5858
+-------------+
5959

60+
OUT;
61+
$this->assertInOutEquals(array($headers, $rows), $output);
62+
}
63+
64+
/**
65+
* Draw simple One column table with colored string
66+
* Output should look like:
67+
* +-------------+
68+
* | Test Header |
69+
* +-------------+
70+
* | x |
71+
* +-------------+
72+
*
73+
* where `x` character has green color.
74+
* At the same time it checks that `green` defined in `cli\Colors` really looks as `green`.
75+
*/
76+
public function testDrawOneColumnColoredTable() {
77+
$headers = array('Test Header');
78+
$rows = array(
79+
array(Colors::colorize('%Gx%n', true)),
80+
);
81+
// green `x`
82+
$x = "\x1B\x5B\x33\x32\x3B\x31\x6Dx\x1B\x5B\x30\x6D";
83+
$output = <<<OUT
84+
+-------------+
85+
| Test Header |
86+
+-------------+
87+
| $x |
88+
+-------------+
89+
6090
OUT;
6191
$this->assertInOutEquals(array($headers, $rows), $output);
6292
}

0 commit comments

Comments
 (0)