Skip to content

Commit c650b8f

Browse files
committed
feature symfony#16652 [console] Add truncate method to FormatterHelper (mheki)
This PR was submitted for the 2.8 branch but it was merged into the 3.1-dev branch instead (closes symfony#16652). Discussion ---------- [console] Add truncate method to FormatterHelper | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#11977 | License | MIT | Doc PR | - This PR adds a `truncate` method to `FormatterHelper`. Message is truncated to the given length, then the suffix is appended to end of that string. If the length is negative, number of letters to truncate is counted from the end of the message. Suffix is always appended, unless truncate length is longer than message and suffix length. Commits ------- a16b1a0 [console] Add truncate method to FormatterHelper
2 parents 0d3bde8 + a16b1a0 commit c650b8f

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)