-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
added docs for the new Table console helper #3627
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
* :doc:`/components/console/helpers/dialoghelper` | ||
* :doc:`/components/console/helpers/formatterhelper` | ||
* :doc:`/components/console/helpers/progresshelper` | ||
* :doc:`/components/console/helpers/table` | ||
* :doc:`/components/console/helpers/tablehelper` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
.. index:: | ||
single: Console Helpers; Table | ||
|
||
Table | ||
===== | ||
|
||
.. versionadded:: 2.5 | ||
The ``Table`` class was introduced in Symfony 2.5 as a replacement for the | ||
:doc:`Table Helper </components/console/helpers/tablehelper>`. | ||
|
||
When building a console application it may be useful to display tabular data: | ||
|
||
.. code-block:: text | ||
|
||
+---------------+--------------------------+------------------+ | ||
| ISBN | Title | Author | | ||
+---------------+--------------------------+------------------+ | ||
| 99921-58-10-7 | Divine Comedy | Dante Alighieri | | ||
| 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens | | ||
| 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien | | ||
| 80-902734-1-6 | And Then There Were None | Agatha Christie | | ||
+---------------+--------------------------+------------------+ | ||
|
||
To display a table, use :class:`Symfony\\Component\\Console\\Helper\\Table`, | ||
set the headers, set the rows and then render the table:: | ||
|
||
use Symfony\Component\Helper\Table; | ||
|
||
$table = new Table($output); | ||
$table | ||
->setHeaders(array('ISBN', 'Title', 'Author')) | ||
->setRows(array( | ||
array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'), | ||
array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'), | ||
array('960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'), | ||
array('80-902734-1-6', 'And Then There Were None', 'Agatha Christie'), | ||
)) | ||
; | ||
$table->render(); | ||
|
||
You can add a table separator anywhere in the output by passing an instance of | ||
:class:`Symfony\\Component\\Console\\Helper\\TableSeparator` as a row:: | ||
|
||
use Symfony\Component\Helper\TableSeparator; | ||
|
||
$table->setRows(array( | ||
array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'), | ||
array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'), | ||
new TableSeparator(), | ||
array('960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'), | ||
array('80-902734-1-6', 'And Then There Were None', 'Agatha Christie'), | ||
)); | ||
|
||
.. code-block:: text | ||
|
||
+---------------+--------------------------+------------------+ | ||
| ISBN | Title | Author | | ||
+---------------+--------------------------+------------------+ | ||
| 99921-58-10-7 | Divine Comedy | Dante Alighieri | | ||
| 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens | | ||
+---------------+--------------------------+------------------+ | ||
| 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien | | ||
| 80-902734-1-6 | And Then There Were None | Agatha Christie | | ||
+---------------+--------------------------+------------------+ | ||
|
||
The table style can be changed to any built-in styles via | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style (singular) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "any of the build-in styles via" |
||
:method:`Symfony\\Component\\Console\\Helper\\Table::setStyle`:: | ||
|
||
// same as calling nothing | ||
$table->setStyle('default'); | ||
|
||
// changes the default style to compact | ||
$table->setStyle('compact'); | ||
$table->render(); | ||
|
||
This code results in: | ||
|
||
.. code-block:: text | ||
|
||
ISBN Title Author | ||
99921-58-10-7 Divine Comedy Dante Alighieri | ||
9971-5-0210-0 A Tale of Two Cities Charles Dickens | ||
960-425-059-0 The Lord of the Rings J. R. R. Tolkien | ||
80-902734-1-6 And Then There Were None Agatha Christie | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tables leave a line break at the bottom i believe, do you think we should show this in the docu? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so the table does not add a line break? or no to add that explicitly? wonder why negative 👯 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is here is exactly what is displayed on my console. Nothing to remove, nothing to add. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i see it is just that I see tests with that line at the bottom, is all 👶 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cordoval you mean there is an empty line break at the bottom? That's not something that should be shown in the docs imo. That's why I commented with "no" |
||
|
||
You can also set the style to ``borderless``:: | ||
|
||
$table->setStyle('borderless'); | ||
$table->render(); | ||
|
||
which outputs: | ||
|
||
.. code-block:: text | ||
|
||
=============== ========================== ================== | ||
ISBN Title Author | ||
=============== ========================== ================== | ||
99921-58-10-7 Divine Comedy Dante Alighieri | ||
9971-5-0210-0 A Tale of Two Cities Charles Dickens | ||
960-425-059-0 The Lord of the Rings J. R. R. Tolkien | ||
80-902734-1-6 And Then There Were None Agatha Christie | ||
=============== ========================== ================== | ||
|
||
If the built-in styles do not fit your need, define your own:: | ||
|
||
.. code-block:: php | ||
|
||
use Symfony\Component\Helper\TableStyle; | ||
|
||
// by default, this is based on the default style | ||
$style = new TableStyle(); | ||
|
||
// customize the style | ||
$style | ||
->setHorizontalBorderChar('<fg=magenta>|</>') | ||
->setVerticalBorderChar('<fg=magenta>-</>') | ||
->setCrossingChar(' ') | ||
; | ||
|
||
// use the style for this table | ||
$table->setStyle($style); | ||
|
||
Here is a full list of things you can customize: | ||
|
||
* :method:`Symfony\\Component\\Console\\Helper\\TableStyle::setPaddingChar` | ||
* :method:`Symfony\\Component\\Console\\Helper\\TableStyle::setHorizontalBorderChar` | ||
* :method:`Symfony\\Component\\Console\\Helper\\TableStyle::setVerticalBorderChar` | ||
* :method:`Symfony\\Component\\Console\\Helper\\TableStyle::setCrossingChar` | ||
* :method:`Symfony\\Component\\Console\\Helper\\TableStyle::setCellHeaderFormat` | ||
* :method:`Symfony\\Component\\Console\\Helper\\TableStyle::setCellRowFormat` | ||
* :method:`Symfony\\Component\\Console\\Helper\\TableStyle::setBorderFormat` | ||
* :method:`Symfony\\Component\\Console\\Helper\\TableStyle::setPadType` | ||
|
||
.. tip:: | ||
|
||
You can also register a style globally:: | ||
|
||
// register the style under the colorful name | ||
Table::setStyleDefinition('colorful', $style); | ||
|
||
// use it for a table | ||
$table->setStyle('colorful'); | ||
|
||
This method can also be used to override a built-in style. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing
// ...
(same in example below)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what do you mean?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
he is using use statement and does not need a namespace, so the // ... is not needed @wouterj
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fabpot well, you left out the complete class + execute method definition (which is good!). People are used that we put a
// ...
in places we've left out some stuff.