Skip to content

Commit d31a359

Browse files
committed
do not count colors in strlengths
1 parent d7ccbb0 commit d31a359

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/TableFormatter.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ protected function pad($string, $len)
239239
*/
240240
protected function strlen($string)
241241
{
242+
// don't count color codes
243+
$string = preg_replace("/\33\\[\\d+(;\\d+)?m/", '', $string);
244+
242245
if (function_exists('mb_strlen')) {
243246
return mb_strlen($string, 'utf-8');
244247
}

tests/TableFormatterTest.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace splitbrain\phpcli\tests;
44

5+
use splitbrain\phpcli\Colors;
6+
57
class TableFormatter extends \splitbrain\phpcli\TableFormatter
68
{
79
public function calculateColLengths($columns)
@@ -95,15 +97,28 @@ public function test_wrap()
9597

9698
}
9799

98-
public function test_lenght()
100+
public function test_length()
99101
{
100102
$text = "this is häppy ☺";
101-
$expect = "this is häppy ☺ |test";
103+
$expect = "$text |test";
102104

103105
$tf = new TableFormatter();
104106
$tf->setBorder('|');
105107
$result = $tf->format([20, '*'], [$text, 'test']);
106108

107109
$this->assertEquals($expect, trim($result));
108110
}
111+
112+
public function test_colorlength(){
113+
$color = new Colors();
114+
115+
$text = 'this is '.$color->wrap('green', Colors::C_GREEN);
116+
$expect = "$text |test";
117+
118+
$tf = new TableFormatter();
119+
$tf->setBorder('|');
120+
$result = $tf->format([20, '*'], [$text, 'test']);
121+
122+
$this->assertEquals($expect, trim($result));
123+
}
109124
}

0 commit comments

Comments
 (0)