-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
[Console] Add FormatterHelper::truncate docs #6186
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,3 +63,70 @@ messages and 2 spaces on the left and right). | |
The exact "style" you use in the block is up to you. In this case, you're using | ||
the pre-defined ``error`` style, but there are other styles, or you can create | ||
your own. See :ref:`components-console-coloring`. | ||
|
||
Print Truncated Messages | ||
------------------------ | ||
|
||
.. versionadded:: 3.1 | ||
The ``truncate`` method was introduced in Symfony 3.1. | ||
|
||
Sometimes you want to print a message truncated to an explicit character length. | ||
This is possible with the | ||
:method:`Symfony\\Component\\Console\\Helper\\FormatterHelper::truncate` method. | ||
|
||
If you would like to truncate a very long message, for example, to 7 characters, | ||
you can write:: | ||
|
||
$message = "This is a very long message, which should be truncated"; | ||
$truncatedMessage = $formatter->truncate($message, 7); | ||
$output->writeln($truncatedMessage); | ||
|
||
And the output will be:: | ||
|
||
This is... | ||
|
||
Message is truncated to the given length, then the suffix is appended to end | ||
of that string. | ||
|
||
Negative String Length | ||
~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
If the length is negative, number of characters to truncate is counted | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [...] is negative, the number of [...] |
||
from the end of the string:: | ||
|
||
$truncatedMessage = $formatter->truncate($message, -5); | ||
|
||
Will result with:: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will result in |
||
|
||
This is a very long message, which should be trun... | ||
|
||
Custom Suffix | ||
~~~~~~~~~~~~~ | ||
|
||
By default ``...`` suffix is used. If you wish to use a different suffix, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By default, the |
||
simply pass it as the third argument to the method:: | ||
|
||
$truncatedMessage = $formatter->truncate($message, 7, '!!'); | ||
|
||
Will result with:: | ||
|
||
This is!! | ||
|
||
Or if you don't want to use suffix at all, just pass an empty string:: | ||
|
||
$truncatedMessage = $formatter->truncate($message, 7, ''); | ||
|
||
Which will result with:: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [...] result in |
||
|
||
This is | ||
|
||
Suffix is always appended, unless truncate length is longer than a message | ||
and a suffix length:: | ||
|
||
$output->writeln($formatter->truncate('test', 10)); | ||
|
||
will output:: | ||
|
||
test | ||
|
||
because length of the ``test...`` string is shorter than 10. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The message [...]