Skip to content

Commit dc34388

Browse files
committed
feature #6186 [Console] Add FormatterHelper::truncate docs (mheki)
This PR was squashed before being merged into the master branch (closes #6186). Discussion ---------- [Console] Add FormatterHelper::truncate docs | Q | A | ------------- | --- | Doc fix? | no | New docs? | yes (symfony/symfony#16652) | Applies to | 3.1 | Fixed tickets | - Commits ------- 60548b1 [Console] Add FormatterHelper::truncate docs
2 parents b60f3b8 + 60548b1 commit dc34388

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

components/console/helpers/formatterhelper.rst

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,55 @@ messages and 2 spaces on the left and right).
6363
The exact "style" you use in the block is up to you. In this case, you're using
6464
the pre-defined ``error`` style, but there are other styles, or you can create
6565
your own. See :ref:`components-console-coloring`.
66+
67+
Print Truncated Messages
68+
------------------------
69+
70+
.. versionadded:: 3.1
71+
The ``truncate`` method was introduced in Symfony 3.1.
72+
73+
Sometimes you want to print a message truncated to an explicit character length.
74+
This is possible with the
75+
:method:`Symfony\\Component\\Console\\Helper\\FormatterHelper::truncate` method.
76+
77+
If you would like to truncate a very long message, for example, to 7 characters,
78+
you can write::
79+
80+
$message = "This is a very long message, which should be truncated";
81+
$truncatedMessage = $formatter->truncate($message, 7);
82+
$output->writeln($truncatedMessage);
83+
84+
And the output will be::
85+
86+
This is...
87+
88+
The message is truncated to the given length, then the suffix is appended to end
89+
of that string.
90+
91+
Negative String Length
92+
~~~~~~~~~~~~~~~~~~~~~~
93+
94+
If the length is negative, the number of characters to truncate is counted
95+
from the end of the string::
96+
97+
$truncatedMessage = $formatter->truncate($message, -5);
98+
99+
This will result in::
100+
101+
This is a very long message, which should be trun...
102+
103+
Custom Suffix
104+
~~~~~~~~~~~~~
105+
106+
By default, the ``...`` suffix is used. If you wish to use a different suffix,
107+
simply pass it as the third argument to the method.
108+
The suffix is always appended, unless truncate length is longer than a message
109+
and a suffix length.
110+
If you don't want to use suffix at all, just pass an empty string::
111+
112+
$truncatedMessage = $formatter->truncate($message, 7, '!!'); // result: This is!!
113+
$truncatedMessage = $formatter->truncate($message, 7, ''); // result: This is
114+
$truncatedMessage = $formatter->truncate('test', 10));
115+
/* result: test
116+
because length of the "test..." string is shorter than 10 */
117+

0 commit comments

Comments
 (0)