Skip to content

Commit 8504381

Browse files
committed
feature #6437 Improved the documentation about the explicit column widths (javiereguiluz)
This PR was squashed before being merged into the master branch (closes #6437). Discussion ---------- Improved the documentation about the explicit column widths | Q | A | ------------- | --- | Doc fix? | yes | New docs? | no | Applies to | master (3.1+) | Fixed tickets | - In addition to the rewording, the `setColumnWidth()` explanation was missing. Commits ------- 88ecd20 Improved the documentation about the explicit column widths
2 parents fd255e9 + 88ecd20 commit 8504381

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

components/console/helpers/table.rst

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,17 @@ You can add a table separator anywhere in the output by passing an instance of
6666
| 80-902734-1-6 | And Then There Were None | Agatha Christie |
6767
+---------------+--------------------------+------------------+
6868
69-
The width of the columns are automatically set using the width of their contents
70-
by default. You can control the minimum widths via :method:`Symfony\\Component\\Console\\Helper\\Table::setColumnWidths`::
69+
By default the width of the columns is calculated automatically based on their
70+
contents. Use the :method:`Symfony\\Component\\Console\\Helper\\Table::setColumnWidths`
71+
method to set the column widths explicitly::
7172

72-
// the widths will expand to be bigger if necessary
73-
// here, the left two columns will be 13 & 24 characters to fit their content
73+
// ...
7474
$table->setColumnWidths(array(10, 0, 30));
7575
$table->render();
76-
77-
This code results in:
76+
77+
In this example, the first column width will be ``10``, the last column width
78+
will be ``30`` and the second column width will be calculated automatically
79+
because of the ``0`` value. The output of this command will be:
7880

7981
.. code-block:: text
8082
@@ -88,6 +90,21 @@ This code results in:
8890
| 80-902734-1-6 | And Then There Were None | Agatha Christie |
8991
+---------------+--------------------------+--------------------------------+
9092
93+
Note that the defined column widths are always considered as the minimum column
94+
widths. If the contents don't fit, the given column width is increased up to the
95+
longest content length. That's why in the previous example the first column has
96+
a ``13`` character length although the user defined ``10`` as its width.
97+
98+
You can also set the width individually for each column with the
99+
:method:`Symfony\\Component\\Console\\Helper\\Table::setColumnWidth` method.
100+
Its first argument is the column index (starting from ``0``) and the second
101+
argument is the column width::
102+
103+
// ...
104+
$table->setColumnWidth(0, 10);
105+
$table->setColumnWidth(2, 30);
106+
$table->render();
107+
91108
The table style can be changed to any built-in styles via
92109
:method:`Symfony\\Component\\Console\\Helper\\Table::setStyle`::
93110

0 commit comments

Comments
 (0)