Skip to content

Commit 7e124ed

Browse files
Merge pull request #91 from wp-cli/86-escape-percent
Don't unnecessarily clobber `%` characters passed through colorize
2 parents f8005f5 + f21abd4 commit 7e124ed

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/cli/Colors.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ static public function colorize($string, $colored = null) {
120120
}
121121

122122
if (!self::shouldColorize($colored)) {
123-
$return = preg_replace('/%((%)|.)/', '$2', $string);
123+
$colors = self::getColors();
124+
$search = array_keys( $colors );
125+
$return = str_replace( $search, '', $string );
124126
self::cacheString($passed, $return, $colored);
125127
return $return;
126128
}

tests/test-table-ascii.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,25 @@ public function testTableWithMultibyteStrings() {
127127
| Schätzen | Apprécier | Оценить | 欣賞 |
128128
+----------+-----------+---------+---------+
129129

130+
OUT;
131+
$this->assertInOutEquals(array($headers, $rows), $output);
132+
}
133+
134+
/**
135+
* Test that % gets escaped correctly.
136+
*/
137+
public function testTableWithPercentCharacters() {
138+
$headers = array( 'Heading', 'Heading2', 'Heading3' );
139+
$rows = array(
140+
array( '% at start', 'at end %', 'in % middle' )
141+
);
142+
$output = <<<'OUT'
143+
+------------+----------+-------------+
144+
| Heading | Heading2 | Heading3 |
145+
+------------+----------+-------------+
146+
| % at start | at end % | in % middle |
147+
+------------+----------+-------------+
148+
130149
OUT;
131150
$this->assertInOutEquals(array($headers, $rows), $output);
132151
}

0 commit comments

Comments
 (0)