Skip to content

[Console] Add FormatterHelper::truncate docs #6186

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

Choose a reason for hiding this comment

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

I know it's a minor thing, but we require it for all the documentation: contents should be wrapped around the column 80. Otherwise sentences are too long to read. Thanks!

: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 letters to truncate is counted from the end of the message::
Copy link
Member

Choose a reason for hiding this comment

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

I'd replace letters by characters to be more precise. And I'd replace end of the message by end of the string to be more "agnostic".


$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, 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.