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

Commit dc34388

Browse filesBrowse 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
Copy full SHA for dc34388

File tree

1 file changed

+52
-0
lines changed
Filter options

1 file changed

+52
-0
lines changed

‎components/console/helpers/formatterhelper.rst

Copy file name to clipboardExpand all lines: components/console/helpers/formatterhelper.rst
+52Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.