Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[console] Add truncate method to FormatterHelper #16652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions 20 src/Symfony/Component/Console/Helper/FormatterHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,26 @@ public function formatBlock($messages, $style, $large = false)
return implode("\n", $messages);
}

/**
* Truncates a message to the given length.
*
* @param string $message
* @param int $length
* @param string $suffix
*
* @return string
*/
public function truncate($message, $length, $suffix = '...')
{
$computedLength = $length - $this->strlen($suffix);

if ($computedLength > $this->strlen($message)) {
return $message;
}

return mb_substr($message, 0, $length).$suffix;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the $charset attribute is missing, which makes this function reliant on a global config option. Other places in this component use mb_detect_encoding($string, null, true).

}

/**
* {@inheritdoc}
*/
Expand Down
36 changes: 36 additions & 0 deletions 36 src/Symfony/Component/Console/Tests/Helper/FormatterHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,40 @@ public function testFormatBlockLGEscaping()
'::formatBlock() escapes \'<\' chars'
);
}

public function testTruncatingWithShorterLengthThanMessageWithSuffix()
{
$formatter = new FormatterHelper();
$message = 'testing truncate';

$this->assertSame('test...', $formatter->truncate($message, 4));
$this->assertSame('testing truncat...', $formatter->truncate($message, 15));
$this->assertSame('testing truncate...', $formatter->truncate($message, 16));
$this->assertSame('zażółć gęślą...', $formatter->truncate('zażółć gęślą jaźń', 12));
}

public function testTruncatingMessageWithCustomSuffix()
{
$formatter = new FormatterHelper();
$message = 'testing truncate';

$this->assertSame('test!', $formatter->truncate($message, 4, '!'));
}

public function testTruncatingWithLongerLengthThanMessageWithSuffix()
{
$formatter = new FormatterHelper();
$message = 'test';

$this->assertSame($message, $formatter->truncate($message, 10));
}

public function testTruncatingWithNegativeLength()
{
$formatter = new FormatterHelper();
$message = 'testing truncate';

$this->assertSame('testing tru...', $formatter->truncate($message, -5));
$this->assertSame('...', $formatter->truncate($message, -100));
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.