Skip to content

[Console] Tweak the tree helper #20825

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

Merged
merged 1 commit into from
Mar 26, 2025
Merged
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
250 changes: 129 additions & 121 deletions components/console/helpers/tree.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ This exampe would output the following:
├── templates/
└── tests/

The given contents can be defined in a multi-dimensional array:
The given contents can be defined in a multi-dimensional array::

$tree = TreeHelper::createTree($io, null, [
'src' => [
Expand Down Expand Up @@ -125,126 +125,132 @@ Built-in Tree Styles
~~~~~~~~~~~~~~~~~~~~

The tree helper provides a few built-in styles that you can use to customize the
output of the tree.

:method:`Symfony\\Component\\Console\\Helper\\TreeStyle::default`

.. code-block:: terminal

├── config
│ ├── packages
│ └── routes
│ ├── framework.yaml
│ └── web_profiler.yaml
├── src
│ ├── Command
│ ├── Controller
│ │ └── DefaultController.php
│ └── Kernel.php
└── templates
└── base.html.twig

:method:`Symfony\\Component\\Console\\Helper\\TreeStyle::box`

.. code-block:: terminal

┃╸ config
┃ ┃╸ packages
┃ ┗╸ routes
┃ ┃╸ framework.yaml
┃ ┗╸ web_profiler.yaml
┃╸ src
┃ ┃╸ Command
┃ ┃╸ Controller
┃ ┃ ┗╸ DefaultController.php
┃ ┗╸ Kernel.php
┗╸ templates
┗╸ base.html.twig

:method:`Symfony\\Component\\Console\\Helper\\TreeStyle::doubleBox`

.. code-block:: terminal

╠═ config
║ ╠═ packages
║ ╚═ routes
║ ╠═ framework.yaml
║ ╚═ web_profiler.yaml
╠═ src
║ ╠═ Command
║ ╠═ Controller
║ ║ ╚═ DefaultController.php
║ ╚═ Kernel.php
╚═ templates
╚═ base.html.twig

:method:`Symfony\\Component\\Console\\Helper\\TreeStyle::compact`

.. code-block:: terminal

├ config
│ ├ packages
│ └ routes
│ ├ framework.yaml
│ └ web_profiler.yaml
├ src
│ ├ Command
│ ├ Controller
│ │ └ DefaultController.php
│ └ Kernel.php
└ templates
└ base.html.twig

:method:`Symfony\\Component\\Console\\Helper\\TreeStyle::light`

.. code-block:: terminal

|-- config
| |-- packages
| `-- routes
| |-- framework.yaml
| `-- web_profiler.yaml
|-- src
| |-- Command
| |-- Controller
| | `-- DefaultController.php
| `-- Kernel.php
`-- templates
`-- base.html.twig

:method:`Symfony\\Component\\Console\\Helper\\TreeStyle::minimal`

.. code-block:: terminal

. config
. . packages
. . routes
. . framework.yaml
. . web_profiler.yaml
. src
. . Command
. . Controller
. . . DefaultController.php
. . Kernel.php
. templates
. base.html.twig

:method:`Symfony\\Component\\Console\\Helper\\TreeStyle::rounded`

.. code-block:: terminal

├─ config
│ ├─ packages
│ ╰─ routes
│ ├─ framework.yaml
│ ╰─ web_profiler.yaml
├─ src
│ ├─ Command
│ ├─ Controller
│ │ ╰─ DefaultController.php
│ ╰─ Kernel.php
╰─ templates
╰─ base.html.twig
output of the tree::

use Symfony\Component\Console\Helper\TreeStyle;
// ...

$tree = TreeHelper::createTree($io, $node, [], TreeStyle::compact());
$tree->render();

``TreeHelper::createTree($io, $node, [], TreeStyle::default())`` (`details`_)

.. code-block:: terminal

├── config
│ ├── packages
│ └── routes
│ ├── framework.yaml
│ └── web_profiler.yaml
├── src
│ ├── Command
│ ├── Controller
│ │ └── DefaultController.php
│ └── Kernel.php
└── templates
└── base.html.twig

``TreeHelper::createTree($io, $node, [], TreeStyle::box())`` (`details`_)

.. code-block:: terminal

┃╸ config
┃ ┃╸ packages
┃ ┗╸ routes
┃ ┃╸ framework.yaml
┃ ┗╸ web_profiler.yaml
┃╸ src
┃ ┃╸ Command
┃ ┃╸ Controller
┃ ┃ ┗╸ DefaultController.php
┃ ┗╸ Kernel.php
┗╸ templates
┗╸ base.html.twig

``TreeHelper::createTree($io, $node, [], TreeStyle::doubleBox())`` (`details`_)

.. code-block:: terminal

╠═ config
║ ╠═ packages
║ ╚═ routes
║ ╠═ framework.yaml
║ ╚═ web_profiler.yaml
╠═ src
║ ╠═ Command
║ ╠═ Controller
║ ║ ╚═ DefaultController.php
║ ╚═ Kernel.php
╚═ templates
╚═ base.html.twig

``TreeHelper::createTree($io, $node, [], TreeStyle::compact())`` (`details`_)

.. code-block:: terminal

├ config
│ ├ packages
│ └ routes
│ ├ framework.yaml
│ └ web_profiler.yaml
├ src
│ ├ Command
│ ├ Controller
│ │ └ DefaultController.php
│ └ Kernel.php
└ templates
└ base.html.twig

``TreeHelper::createTree($io, $node, [], TreeStyle::light())`` (`details`_)

.. code-block:: terminal

|-- config
| |-- packages
| `-- routes
| |-- framework.yaml
| `-- web_profiler.yaml
|-- src
| |-- Command
| |-- Controller
| | `-- DefaultController.php
| `-- Kernel.php
`-- templates
`-- base.html.twig

``TreeHelper::createTree($io, $node, [], TreeStyle::minimal())`` (`details`_)

.. code-block:: terminal

. config
. . packages
. . routes
. . framework.yaml
. . web_profiler.yaml
. src
. . Command
. . Controller
. . . DefaultController.php
. . Kernel.php
. templates
. base.html.twig

``TreeHelper::createTree($io, $node, [], TreeStyle::rounded())`` (`details`_)

.. code-block:: terminal

├─ config
│ ├─ packages
│ ╰─ routes
│ ├─ framework.yaml
│ ╰─ web_profiler.yaml
├─ src
│ ├─ Command
│ ├─ Controller
│ │ ╰─ DefaultController.php
│ ╰─ Kernel.php
╰─ templates
╰─ base.html.twig

Making a Custom Tree Style
~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -285,3 +291,5 @@ The above code will output the following tree:
🔵 🟢 🟠 🟡 Kernel.php
🔵 🟠 🟡 templates
🔵 🔴 🟠 🟡 base.html.twig

.. _`details`: https://github.com/symfony/symfony/blob/7.3/src/Symfony/Component/Console/Helper/TreeStyle.php