Skip to content

Commit 3bc1185

Browse files
committed
Add documentation about manipulating console output
1 parent 45ba13c commit 3bc1185

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed

console/manipulating_output.rst

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
How To Manipulate Console Output
2+
================================
3+
4+
The console comes with a powerful class to print information to the terminal. This
5+
information can be manipulated by clearing or overwriting the displayed content.
6+
7+
In order to manipulate the content, you need to create a new output section. An output section is
8+
a part in the terminal where information will be displayed from the console.
9+
10+
A section can be manipulated individually, and multiple sections can appended to the output.
11+
12+
To create a new output section, you need to use the
13+
:method:`Symfony\\Component\\Console\\Output\\ConsoleOutput::section` method::
14+
15+
class MyCommand extends Command
16+
{
17+
protected function execute(InputInterface $input, OutputInterface $output)
18+
{
19+
$section = $output->section();
20+
}
21+
}
22+
}
23+
24+
This will return an instance of of the :class:`Symfony\\Component\\Console\\Output\\ConsoleSectionOutput`
25+
26+
.. tip::
27+
28+
You can create multiple sections by calling the
29+
:method:`Symfony\\Component\\Console\\Output\\ConsoleOutput::section` method multiple times.
30+
This will append a new section after the previous one.
31+
32+
.. caution::
33+
34+
Displaying information in a section will always append a new line to the output.
35+
36+
37+
Overwriting Output
38+
------------------
39+
40+
When displaying information in the console, you can overwrite the output by using the
41+
:method:`Symfony\\Component\\Console\\Output\\ConsoleSectionOutput::overwrite` method::
42+
43+
$section->writeln('Hello');
44+
$section->overwrite('World!');
45+
46+
The only information displayed in the terminal will be ``World!`` as the first part will
47+
be overwritten.
48+
49+
Clearing s Section
50+
------------------
51+
52+
You can clear all the content in a section by using the
53+
:method:`Symfony\\Component\\Console\\Output\\ConsoleSectionOutput::overwrite` method.
54+
55+
Clearing a section will erase all the content that is displayed in that section::
56+
57+
$section->writeln('Hello World!');
58+
$section->clear();
59+
60+
This will leave your terminal clean without any output displayed.
61+
62+
You can also clear a specific number of lines from the output instead of clearing all the
63+
output::
64+
65+
$section->writeln('One!');
66+
$section->writeln('Two!');
67+
$section->writeln('Three!');
68+
$section->writeln('Four!');
69+
70+
$section->clear(2);
71+
72+
This will only leave the lines ``One!`` and ``Two!`` displaying in the terminal.
73+
74+
Modifying Content In Previous Sections
75+
--------------------------------------
76+
77+
When you append multiple sections to the terminal, you can manipulate the output of
78+
only a specific section, leaving the rest intact::
79+
80+
$section1 = $output->section();
81+
$section2 = $output->section();
82+
83+
$section1->writeln('Hello World!');
84+
$section2->writeln('This is comes second');
85+
86+
$section1->overwrite('This comes first');
87+
88+
This will result in the following output in the terminal:
89+
90+
.. code-block:: text
91+
92+
This comes first
93+
This comes second
94+
95+
Displaying Multiple Progress Bars
96+
---------------------------------
97+
98+
You can display multiple progress bars underneath each other, and changing the progress
99+
of one of the bars at a time::
100+
101+
$section1 = $output->section();
102+
$section2 = $output->section();
103+
104+
$progress1 = new ProgressBar($section1);
105+
$progress2 = new ProgressBar($section2);
106+
107+
$progress1->start(100);
108+
$progress2->start(100);
109+
110+
$c = 0;
111+
while (++$c < 100) {
112+
$progress1->advance();
113+
114+
if ($c % 2 === 0) {
115+
$progress2->advance(4);
116+
}
117+
118+
usleep(500000);
119+
}
120+
121+
After a couple of iterations, the output in the terminal will look like this:
122+
123+
.. code-block:: text
124+
125+
34/100 [=========>------------------] 34%
126+
68/100 [===================>--------] 68%
127+
128+
Appending Rows To a Table
129+
-------------------------
130+
131+
If you are displaying a table in the terminal, you can append rows to an already rendered table
132+
by using the :method:`Symfony\\Component\\Console\\Helper\\Table::appendRow` method.
133+
134+
This method takes the same arguments as the :method:`Symfony\\Component\\Console\\Helper\\Table::addRow`
135+
method, but if the table is already rendered, then it will append the row to the table.
136+
137+
$section = $output->section();
138+
$table = new Table($section);
139+
140+
$table->addRow(['Row 1']);
141+
$table->render();
142+
143+
$table->addRow(['Row 2']);
144+
145+
This will display the following table in the terminal:
146+
147+
.. code-block:: text
148+
149+
+-------+
150+
| Row 1 |
151+
| Row 2 |
152+
+-------+

0 commit comments

Comments
 (0)