Skip to content
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

Closed
Changes from 3 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
67 changes: 67 additions & 0 deletions components/console/helpers/formatterhelper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

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

The message [...]

of that string.

Negative String Length
~~~~~~~~~~~~~~~~~~~~~~

If the length is negative, number of characters to truncate is counted
Copy link
Member

Choose a reason for hiding this comment

The 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::
Copy link
Member

Choose a reason for hiding this comment

The 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,
Copy link
Member

Choose a reason for hiding this comment

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

By default, the ... suffix [...]

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::
Copy link
Member

Choose a reason for hiding this comment

The 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.