Skip to content

Commit 5ee4f23

Browse files
committed
Add Colors::width() instead of changing Colors::length() for api BC.
1 parent 1d22a24 commit 5ee4f23

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

lib/cli/Colors.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ static public function cacheString($passed, $colorized, $colored) {
176176
* Return the length of the string without color codes.
177177
*
178178
* @param string $string the string to measure
179-
* @return string
179+
* @return int
180180
*/
181181
static public function length($string) {
182182
if (isset(self::$_string_cache[md5($string)]['decolorized'])) {
@@ -185,6 +185,22 @@ static public function length($string) {
185185
$test_string = self::decolorize($string);
186186
}
187187

188+
return safe_strlen($test_string);
189+
}
190+
191+
/**
192+
* Return the width (length in characters) of the string without color codes.
193+
*
194+
* @param string $string the string to measure
195+
* @return int
196+
*/
197+
static public function width($string) {
198+
if (isset(self::$_string_cache[md5($string)]['decolorized'])) {
199+
$test_string = self::$_string_cache[md5($string)]['decolorized'];
200+
} else {
201+
$test_string = self::decolorize($string);
202+
}
203+
188204
return strwidth($test_string);
189205
}
190206

lib/cli/Table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function setRenderer(Renderer $renderer) {
102102
*/
103103
protected function checkRow(array $row) {
104104
foreach ($row as $column => $str) {
105-
$width = Colors::shouldColorize() ? Colors::length($str) : strwidth($str);
105+
$width = Colors::shouldColorize() ? Colors::width($str) : strwidth($str);
106106
if (!isset($this->_width[$column]) || $width > $this->_width[$column]) {
107107
$this->_width[$column] = $width;
108108
}

lib/cli/table/Ascii.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public function row( array $row ) {
133133
$value = str_replace( PHP_EOL, ' ', $value );
134134

135135
$col_width = $this->_widths[ $col ];
136-
$original_val_width = Colors::shouldColorize() ? Colors::length( $value ) : \cli\strwidth( $value );
136+
$original_val_width = Colors::shouldColorize() ? Colors::width( $value ) : \cli\strwidth( $value );
137137
if ( $original_val_width > $col_width ) {
138138
$row[ $col ] = \cli\safe_substr( $value, 0, $col_width );
139139
$value = \cli\safe_substr( $value, $col_width, $original_val_width );

tests/test-cli.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,27 @@ function setUp() {
1212

1313
function test_string_length() {
1414
$this->assertEquals( \cli\Colors::length( 'x' ), 1 );
15+
$this->assertEquals( \cli\Colors::length( '' ), 1 );
16+
}
17+
18+
function test_string_width() {
19+
$this->assertEquals( \cli\Colors::width( 'x' ), 1 );
20+
$this->assertEquals( \cli\Colors::width( '' ), 2 ); // Double-width char.
1521
}
1622

1723
function test_encoded_string_length() {
1824

1925
$this->assertEquals( \cli\Colors::length( 'hello' ), 5 );
2026
$this->assertEquals( \cli\Colors::length( 'óra' ), 3 );
21-
$this->assertEquals( \cli\Colors::length( '日本語' ), 6 ); // 3 double-width chars.
27+
$this->assertEquals( \cli\Colors::length( '日本語' ), 3 );
28+
29+
}
30+
31+
function test_encoded_string_width() {
32+
33+
$this->assertEquals( \cli\Colors::width( 'hello' ), 5 );
34+
$this->assertEquals( \cli\Colors::width( 'óra' ), 3 );
35+
$this->assertEquals( \cli\Colors::width( '日本語' ), 6 ); // 3 double-width chars.
2236

2337
}
2438

@@ -45,6 +59,12 @@ function test_encoded_substr() {
4559

4660
function test_colorized_string_length() {
4761
$this->assertEquals( \cli\Colors::length( \cli\Colors::colorize( '%Gx%n', true ) ), 1 );
62+
$this->assertEquals( \cli\Colors::length( \cli\Colors::colorize( '%G日%n', true ) ), 1 );
63+
}
64+
65+
function test_colorized_string_width() {
66+
$this->assertEquals( \cli\Colors::width( \cli\Colors::colorize( '%Gx%n', true ) ), 1 );
67+
$this->assertEquals( \cli\Colors::width( \cli\Colors::colorize( '%G日%n', true ) ), 2 ); // Double-width char.
4868
}
4969

5070
function test_colorize_string_is_colored() {

0 commit comments

Comments
 (0)