Skip to content

Commit 48bb9ad

Browse files
Merge pull request #53 from wp-cli/fix-53
Fix table padding for accented characters
2 parents ee739f6 + 8e3901a commit 48bb9ad

5 files changed

Lines changed: 62 additions & 3 deletions

File tree

lib/cli/Colors.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ static public function colorize($string, $colored = null) {
167167
* @return string
168168
*/
169169
static public function length($string) {
170-
return strlen(self::colorize($string, false));
170+
return safe_strlen(self::colorize($string, false));
171171
}
172172

173173
/**
@@ -178,9 +178,10 @@ static public function length($string) {
178178
* @return string
179179
*/
180180
static public function pad($string, $length) {
181-
$real_length = strlen($string);
181+
$real_length = safe_strlen($string);
182182
$show_length = self::length($string);
183-
$length += $real_length - $show_length;
183+
$diff = strlen( $string ) - safe_strlen( $string );
184+
$length += $real_length - $show_length + $diff;
184185

185186
return str_pad($string, $length);
186187
}

lib/cli/cli.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,13 @@ function confirm( $question, $default = false ) {
150150
function menu( $items, $default = null, $title = 'Choose an item' ) {
151151
return Streams::menu( $items, $default, $title );
152152
}
153+
154+
/**
155+
* An encoding-safe way of getting string length.
156+
*
157+
* @param string The string to check
158+
* @return int Numeric value that represents the string's length
159+
*/
160+
function safe_strlen( $str ) {
161+
return mb_strlen( $str, mb_detect_encoding( $str ) );
162+
}

phpunit.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<phpunit
2+
bootstrap="tests/bootstrap.php"
3+
colors="true"
4+
>
5+
<testsuites>
6+
<testsuite>
7+
<directory prefix="test-" suffix=".php">tests/</directory>
8+
</testsuite>
9+
</testsuites>
10+
</phpunit>

tests/bootstrap.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
require dirname( dirname( __FILE__ ) ) . '/lib/cli/cli.php';
4+
5+
function cli_autoload( $className ) {
6+
$className = ltrim($className, '\\');
7+
$fileName = '';
8+
$namespace = '';
9+
if ($lastNsPos = strrpos($className, '\\')) {
10+
$namespace = substr($className, 0, $lastNsPos);
11+
$className = substr($className, $lastNsPos + 1);
12+
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
13+
}
14+
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
15+
16+
if ( 'cli' !== substr( $fileName, 0, 3 ) ) {
17+
return;
18+
}
19+
20+
require dirname( dirname( __FILE__ ) ) . '/lib/' . $fileName;
21+
}
22+
23+
spl_autoload_register( 'cli_autoload' );

tests/test-cli.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
class testsCli extends PHPUnit_Framework_TestCase {
4+
5+
function test_encoded_string_length() {
6+
7+
$this->assertEquals( \cli\Colors::length( 'hello' ), 5 );
8+
$this->assertEquals( \cli\Colors::length( 'óra' ), 3 );
9+
10+
$this->assertEquals( \cli\safe_strlen( \cli\Colors::pad( 'hello', 6 ) ), 6 );
11+
$this->assertEquals( \cli\safe_strlen( \cli\Colors::pad( 'óra', 6 ) ), 6 );
12+
13+
}
14+
15+
}

0 commit comments

Comments
 (0)