Skip to content

Commit b4ac701

Browse files
committed
[Console] Update the table helper page
1 parent 6e0d1c3 commit b4ac701

File tree

1 file changed

+63
-36
lines changed

1 file changed

+63
-36
lines changed

components/console/helpers/table.rst

+63-36
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
1-
Table
2-
=====
1+
Table Helper
2+
============
33

4-
When building a console application it may be useful to display tabular data:
5-
6-
.. code-block:: terminal
7-
8-
+---------------+--------------------------+------------------+
9-
| ISBN | Title | Author |
10-
+---------------+--------------------------+------------------+
11-
| 99921-58-10-7 | Divine Comedy | Dante Alighieri |
12-
| 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
13-
| 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
14-
| 80-902734-1-6 | And Then There Were None | Agatha Christie |
15-
+---------------+--------------------------+------------------+
16-
17-
.. note::
18-
19-
As an alternative, consider using the
20-
:ref:`SymfonyStyle <symfony-style-content>` to display a table.
4+
When building console applications, Symfony provides several utilities for
5+
rendering tabular data. The simplest option is to use the table methods from
6+
:ref:`Symfony Style <symfony-style-content>`. While convenient, this approach
7+
doesn't allow customization of the table's design. For more control and advanced
8+
features, use the ``Table`` console helper explained in this article.
219

2210
To display a table, use :class:`Symfony\\Component\\Console\\Helper\\Table`,
2311
set the headers, set the rows and then render the table::
@@ -48,6 +36,22 @@ set the headers, set the rows and then render the table::
4836
}
4937
}
5038

39+
This outputs:
40+
41+
.. code-block:: terminal
42+
43+
+---------------+--------------------------+------------------+
44+
| ISBN | Title | Author |
45+
+---------------+--------------------------+------------------+
46+
| 99921-58-10-7 | Divine Comedy | Dante Alighieri |
47+
| 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
48+
| 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
49+
| 80-902734-1-6 | And Then There Were None | Agatha Christie |
50+
+---------------+--------------------------+------------------+
51+
52+
Adding Table Separators
53+
-----------------------
54+
5155
You can add a table separator anywhere in the output by passing an instance of
5256
:class:`Symfony\\Component\\Console\\Helper\\TableSeparator` as a row::
5357

@@ -61,6 +65,8 @@ You can add a table separator anywhere in the output by passing an instance of
6165
['80-902734-1-6', 'And Then There Were None', 'Agatha Christie'],
6266
]);
6367

68+
This outputs:
69+
6470
.. code-block:: terminal
6571
6672
+---------------+--------------------------+------------------+
@@ -73,13 +79,18 @@ You can add a table separator anywhere in the output by passing an instance of
7379
| 80-902734-1-6 | And Then There Were None | Agatha Christie |
7480
+---------------+--------------------------+------------------+
7581
82+
Adding Table Titles
83+
-------------------
84+
7685
You can optionally display titles at the top and the bottom of the table::
7786

7887
// ...
7988
$table->setHeaderTitle('Books');
8089
$table->setFooterTitle('Page 1/2');
8190
$table->render();
8291

92+
This outputs:
93+
8394
.. code-block:: terminal
8495
8596
+---------------+----------- Books --------+------------------+
@@ -92,6 +103,9 @@ You can optionally display titles at the top and the bottom of the table::
92103
| 80-902734-1-6 | And Then There Were None | Agatha Christie |
93104
+---------------+--------- Page 1/2 -------+------------------+
94105
106+
Setting the Column Widths Explicitly
107+
------------------------------------
108+
95109
By default, the width of the columns is calculated automatically based on their
96110
contents. Use the :method:`Symfony\\Component\\Console\\Helper\\Table::setColumnWidths`
97111
method to set the column widths explicitly::
@@ -114,7 +128,7 @@ argument is the column width::
114128
$table->setColumnWidth(2, 30);
115129
$table->render();
116130

117-
The output of this command will be:
131+
This outputs:
118132

119133
.. code-block:: terminal
120134
@@ -141,7 +155,7 @@ If you prefer to wrap long contents in multiple rows, use the
141155
$table->setColumnMaxWidth(1, 10);
142156
$table->render();
143157

144-
The output of this command will be:
158+
This outputs:
145159

146160
.. code-block:: terminal
147161
@@ -154,14 +168,17 @@ The output of this command will be:
154168
| (the rest of the rows...) |
155169
+-------+------------+--------------------------------+
156170
171+
Rendering Vertical Tables
172+
-------------------------
173+
157174
By default, table contents are displayed horizontally. You can change this behavior
158175
via the :method:`Symfony\\Component\\Console\\Helper\\Table::setVertical` method::
159176

160177
// ...
161178
$table->setVertical();
162179
$table->render();
163180

164-
The output of this command will be:
181+
This outputs:
165182

166183
.. code-block:: terminal
167184
@@ -179,17 +196,24 @@ The output of this command will be:
179196

180197
Support for vertical rendering was introduced in Symfony 6.1.
181198

199+
Customizing the Table Style
200+
---------------------------
201+
182202
The table style can be changed to any built-in styles via
183203
:method:`Symfony\\Component\\Console\\Helper\\Table::setStyle`::
184204

185-
// same as calling nothing
205+
// this 'default' style is the one used when no style is specified
186206
$table->setStyle('default');
187207

188-
// changes the default style to compact
208+
Built-in Table Styles
209+
~~~~~~~~~~~~~~~~~~~~~
210+
211+
**Compact**::
212+
189213
$table->setStyle('compact');
190214
$table->render();
191215

192-
This code results in:
216+
This outputs:
193217

194218
.. code-block:: terminal
195219
@@ -199,12 +223,12 @@ This code results in:
199223
960-425-059-0 The Lord of the Rings J. R. R. Tolkien
200224
80-902734-1-6 And Then There Were None Agatha Christie
201225
202-
You can also set the style to ``borderless``::
226+
**Borderless**::
203227

204228
$table->setStyle('borderless');
205229
$table->render();
206230

207-
which outputs:
231+
This outputs:
208232

209233
.. code-block:: terminal
210234
@@ -217,12 +241,12 @@ which outputs:
217241
80-902734-1-6 And Then There Were None Agatha Christie
218242
=============== ========================== ==================
219243
220-
You can also set the style to ``box``::
244+
**Box**::
221245

222246
$table->setStyle('box');
223247
$table->render();
224248

225-
which outputs:
249+
This outputs:
226250

227251
.. code-block:: terminal
228252
@@ -235,12 +259,12 @@ which outputs:
235259
│ 80-902734-1-6 │ And Then There Were None │ Agatha Christie │
236260
└───────────────┴──────────────────────────┴──────────────────┘
237261
238-
You can also set the style to ``box-double``::
262+
**Double box**::
239263

240264
$table->setStyle('box-double');
241265
$table->render();
242266

243-
which outputs:
267+
This outputs:
244268

245269
.. code-block:: terminal
246270
@@ -253,7 +277,10 @@ which outputs:
253277
║ 80-902734-1-6 │ And Then There Were None │ Agatha Christie ║
254278
╚═══════════════╧══════════════════════════╧══════════════════╝
255279
256-
If the built-in styles do not fit your need, define your own::
280+
Making a Custom Table Style
281+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
282+
283+
If the built-in styles do not fit your needs, define your own::
257284

258285
use Symfony\Component\Console\Helper\TableStyle;
259286

@@ -343,7 +370,7 @@ To make a table cell that spans multiple columns you can use a :class:`Symfony\\
343370
;
344371
$table->render();
345372

346-
This results in:
373+
This outputs:
347374

348375
.. code-block:: terminal
349376
@@ -366,7 +393,7 @@ This results in:
366393
]);
367394
// ...
368395

369-
This generates:
396+
This outputs:
370397

371398
.. code-block:: terminal
372399
@@ -445,7 +472,7 @@ The only requirement to append rows is that the table must be rendered inside a
445472
}
446473
}
447474

448-
This will display the following table in the terminal:
475+
This outputs:
449476

450477
.. code-block:: terminal
451478
@@ -466,7 +493,7 @@ This will display the following table in the terminal:
466493
$table->render();
467494
// ...
468495

469-
This will display:
496+
This outputs:
470497

471498
.. code-block:: terminal
472499

0 commit comments

Comments
 (0)