@@ -5,33 +5,41 @@ Formatter Helper
5
5
================
6
6
7
7
The Formatter helpers provides functions to format the output with colors.
8
- You can do more advanced things with this helper than the things in
8
+ You can do more advanced things with this helper than you can in
9
9
:ref: `components-console-coloring `.
10
10
11
- The Formatter Helper is included in the default helper set, which you can
11
+ The `` formatter `` helper is included in the default helper set, which you can
12
12
get by calling
13
13
:method: `Symfony\\ Component\\ Console\\ Command\\ Command::getHelperSet `::
14
14
15
15
$formatter = $this->getHelperSet()->get('formatter');
16
16
17
- The methods return a string which in most cases you want to write
18
- that data to the console with
19
- :method: `Symfony\\ Component\\ Console\\ Output\\ Output ::writeln ` .
17
+ The methods return a string, which you'll usually render to the console by
18
+ passing it to the
19
+ :method: `OutputInterface::writeln< Symfony\\ Component\\ Console\\ Output\\ OutputInterface ::writeln> ` method .
20
20
21
- Print messages in a section
21
+ Print Messages in a Section
22
22
---------------------------
23
23
24
- Symfony uses a defined style when printing to the command line.
25
- It prints the section in color and with brackets around and the
26
- actual message behind this section indication.
24
+ Symfony offers a defined style when printing a message that belongs to some
25
+ "section". It prints the section in color and with brackets around it and the
26
+ actual message to the right of this. Minus the color, it looks like this:
27
+
28
+ .. code-block :: text
29
+
30
+ [SomeSection] Here is some message related to that section
27
31
28
32
To reproduce this style, you can use the
29
- :method: `Symfony\\ Component\\ Console\\ Helper\\ FormatterHelper::formatSection `::
33
+ :method: `Symfony\\ Component\\ Console\\ Helper\\ FormatterHelper::formatSection `
34
+ method::
30
35
31
- $formattedLine = $formatter->formatSection('SomeCommand', 'Some output from within the SomeCommand');
36
+ $formattedLine = $formatter->formatSection(
37
+ 'SomeSection',
38
+ 'Here is some message related to that section'
39
+ );
32
40
$output->writeln($formattedLine);
33
41
34
- Print messages in a block
42
+ Print Messages in a Block
35
43
-------------------------
36
44
37
45
Sometimes you want to be able to print a whole block of text with a background
@@ -40,13 +48,18 @@ color. Symfony uses this when printing error messages.
40
48
If you print your error message on more than one line manually, you will
41
49
notice that the background is only as long as each individual line. Use the
42
50
:method: `Symfony\\ Component\\ Console\\ Helper\\ FormatterHelper::formatBlock `
43
- togenerate a block output::
51
+ to generate a block output::
44
52
45
53
$errorMessages = array('Error!', 'Something went wrong');
46
54
$formattedBlock = $formatter->formatBlock($errorMessages, 'error');
55
+ $output->writeln($formattedBlock);
47
56
48
57
As you can see, passing an array of messages to the
49
58
:method: `Symfony\\ Component\\ Console\\ Helper\\ FormatterHelper::formatBlock `
50
59
method creates the desired output. If you pass ``true `` as third parameter, the
51
- block will be formatted with more padding (one blank line above and below the
52
- messages and 2 spaces on the left and right).
60
+ block will be formatted with more padding (one blank line above and below the
61
+ messages and 2 spaces on the left and right).
62
+
63
+ The exact "style" you use in the block is up to you. In this case, you're using
64
+ the pre-defined ``error `` style, but there are other styles, or you can create
65
+ your own. See :ref: `components-console-coloring `.
0 commit comments