Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved the documentation about the explicit column widths #6437

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions components/console/helpers/table.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,17 @@ You can add a table separator anywhere in the output by passing an instance of
| 80-902734-1-6 | And Then There Were None | Agatha Christie |
+---------------+--------------------------+------------------+

The width of the columns are automatically set using the width of their contents
by default. You can control the minimum widths via :method:`Symfony\\Component\\Console\\Helper\\Table::setColumnWidths`::
By default the width of the columns is calculated automatically based on their
contents. Use the :method:`Symfony\\Component\\Console\\Helper\\Table::setColumnWidths`
method to set the column widths explicitly::

// the widths will expand to be bigger if necessary
// here, the left two columns will be 13 & 24 characters to fit their content
// ...
$table->setColumnWidths(array(10, 0, 30));
$table->render();

This code results in:

In this example, the first column width will be ``10``, the last column width
will be ``30`` and the second column width will be calculated automatically
because of the ``0`` value. The output of this command will be:

.. code-block:: text

Expand All @@ -88,6 +90,21 @@ This code results in:
| 80-902734-1-6 | And Then There Were None | Agatha Christie |
+---------------+--------------------------+--------------------------------+

Note that the defined column widths are always considered as the minimum column
widths. If the contents don't fit, the given column width is increased up to the
longest content length. That's why in the previous example the first column has
a ``13`` character length although the user defined ``10`` as its width.

You can also set the width individually for each column with the
:method:`Symfony\\Component\\Console\\Helper\\Table::setColumnWidth` method.
Its first argument is the column index (starting from ``0``) and the second
argument is the column width::

// ...
$table->setColumnWidth(0, 10);
$table->setColumnWidth(2, 30);
$table->render();

The table style can be changed to any built-in styles via
:method:`Symfony\\Component\\Console\\Helper\\Table::setStyle`::

Expand Down