Skip to content

Commit a16b1a0

Browse files
marek-pietrzak-tgfabpot
authored andcommitted
[console] Add truncate method to FormatterHelper
1 parent 0d3bde8 commit a16b1a0

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
3.1.0
5+
-----
6+
7+
* added truncate method to FormatterHelper
8+
49
2.8.0
510
-----
611

src/Symfony/Component/Console/Helper/FormatterHelper.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,30 @@ public function formatBlock($messages, $style, $large = false)
7272
return implode("\n", $messages);
7373
}
7474

75+
/**
76+
* Truncates a message to the given length.
77+
*
78+
* @param string $message
79+
* @param int $length
80+
* @param string $suffix
81+
*
82+
* @return string
83+
*/
84+
public function truncate($message, $length, $suffix = '...')
85+
{
86+
$computedLength = $length - $this->strlen($suffix);
87+
88+
if ($computedLength > $this->strlen($message)) {
89+
return $message;
90+
}
91+
92+
if (false === $encoding = mb_detect_encoding($message, null, true)) {
93+
return substr($message, 0, $length).$suffix;
94+
}
95+
96+
return mb_substr($message, 0, $length, $encoding).$suffix;
97+
}
98+
7599
/**
76100
* {@inheritdoc}
77101
*/

src/Symfony/Component/Console/Tests/Helper/FormatterHelperTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,40 @@ public function testFormatBlockLGEscaping()
8989
'::formatBlock() escapes \'<\' chars'
9090
);
9191
}
92+
93+
public function testTruncatingWithShorterLengthThanMessageWithSuffix()
94+
{
95+
$formatter = new FormatterHelper();
96+
$message = 'testing truncate';
97+
98+
$this->assertSame('test...', $formatter->truncate($message, 4));
99+
$this->assertSame('testing truncat...', $formatter->truncate($message, 15));
100+
$this->assertSame('testing truncate...', $formatter->truncate($message, 16));
101+
$this->assertSame('zażółć gęślą...', $formatter->truncate('zażółć gęślą jaźń', 12));
102+
}
103+
104+
public function testTruncatingMessageWithCustomSuffix()
105+
{
106+
$formatter = new FormatterHelper();
107+
$message = 'testing truncate';
108+
109+
$this->assertSame('test!', $formatter->truncate($message, 4, '!'));
110+
}
111+
112+
public function testTruncatingWithLongerLengthThanMessageWithSuffix()
113+
{
114+
$formatter = new FormatterHelper();
115+
$message = 'test';
116+
117+
$this->assertSame($message, $formatter->truncate($message, 10));
118+
}
119+
120+
public function testTruncatingWithNegativeLength()
121+
{
122+
$formatter = new FormatterHelper();
123+
$message = 'testing truncate';
124+
125+
$this->assertSame('testing tru...', $formatter->truncate($message, -5));
126+
$this->assertSame('...', $formatter->truncate($message, -100));
127+
}
92128
}

0 commit comments

Comments
 (0)