@@ -87,9 +87,13 @@ After configuring and registering the command, you can execute it in the termina
8787 $ php bin/console app:create-user
8888
8989 As you might expect, this command will do nothing as you didn't write any logic
90- yet. Add your own logic inside the ``execute() `` method, which has access to the
91- input stream (e.g. options and arguments) and the output stream (to write
92- messages to the console)::
90+ yet. Add your own logic inside the ``execute() `` method.
91+
92+ Console Output
93+ --------------
94+
95+ The ``execute() `` method has access to the output stream to write messages to
96+ the console::
9397
9498 // ...
9599 protected function execute(InputInterface $input, OutputInterface $output)
@@ -128,6 +132,57 @@ Now, try executing the command:
128132 Whoa!
129133 You are about to create a user.
130134
135+ .. _console-output-sections :
136+
137+ Output Sections
138+ ~~~~~~~~~~~~~~~
139+
140+ .. versionadded :: 4.1
141+ Output sections were introduced in Symfony 4.1.
142+
143+ The regular console output can be divided into multiple independent regions
144+ called "output sections". Create one or more of these sections when you need to
145+ clear and overwrite the output information.
146+
147+ Sections are created with the
148+ :method: `Symfony\\ Component\\ Console\\ Output\\ ConsoleOutput::section ` method,
149+ which returns an instance of
150+ :class: `Symfony\\ Component\\ Console\\ Output\\ ConsoleSectionOutput `::
151+
152+ class MyCommand extends Command
153+ {
154+ protected function execute(InputInterface $input, OutputInterface $output)
155+ {
156+ $section1 = $output->section();
157+ $section2 = $output->section();
158+ $section1->writeln('Hello');
159+ $section2->writeln('World!');
160+ // Output displays "Hello\nWorld!\n"
161+
162+ // overwrite() replaces all the existing section contents with the given content
163+ $section1->overwrite('Goodbye');
164+ // Output now displays "Goodbye\nWorld!\n"
165+
166+ // clear() deletes all the section contents...
167+ $section2->clear();
168+ // Output now displays "Goodbye\n"
169+
170+ // ...but you can also delete a given number of lines
171+ // (this example deletes the last two lines of the section)
172+ $section1->clear(2);
173+ // Output is now completely empty!
174+ }
175+ }
176+
177+ .. note ::
178+
179+ A new line is appended automatically when displaying information in a section.
180+
181+ Output sections let you manipulate the Console output in advanced ways, such as
182+ :ref: `displaying multiple progress bars <console-multiple-progress-bars >` which
183+ are updated independently and :ref: `appending rows to tables <console-modify-rendered-tables >`
184+ that have already been rendered.
185+
131186Console Input
132187-------------
133188
0 commit comments