Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Console] Add a Tree Helper + multiple Styles#59588

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fabpot merged 1 commit intosymfony:7.3fromsmnandre:sa/tree-node-console
Feb 26, 2025

Conversation

smnandre
Copy link
Member

@smnandresmnandre commentedJan 23, 2025
edited
Loading

QA
Branch?7.3
Bug fix?no
New feature?yes
Deprecations?no
IssuesFix #...
LicenseMIT

Important

Code examples below arenot up to date, documentation incoming.


Feature

The goal of the Tree helper is to ease renderinghierarchical data in the console:

  • file system
  • configuration
  • workflows
  • ...

Usage

Two methods available, manual or via SymfonyStyle

SymfonyStyle (easiest)

$io =newSymfonyStyle($input,$ouput);$io->tree(['A' => ['A1','A2',    ],'B' => ['B1' => ['B11',        ],'B2',    ],]);

Will render

root├── A│   ├── A1│   └── A2├── B│   ├── B1│   │   ├── B11│   │   └── B12│   └── B2└── C

You also can pass a custom TreeStyle here, or one of the one availbes (see below)

Manually

You can build the Tree the way you want thanks to the TreeNode class.

$root = (newTreeNode('root'))    ->addChild((newTreeNode('A'))        ->addChild(newTreeNode('A1'))// ...$tree = TreeHelper::create($output,$root, style:$style);$tree->render();

Styles

Various styles are available (based on the styles of Table .. and some new ones), or you can also build your own Style from scratch.

Default

root├── A│   ├── A1│   └── A2├── B│   ├── B1│   │   ├── B11│   │   └── B12│   └── B2└── C

Box

root┃╸ A┃  ┃╸ A1┃  ┗╸ A2┃╸ B┃  ┃╸ B1┃  ┃  ┃╸ B11┃  ┃  ┗╸ B12┃  ┗╸ B2┗╸ C

Box Double

root╠═ A║  ╠═ A1║  ╚═ A2╠═ B║  ╠═ B1║  ║  ╠═ B11║  ║  ╚═ B12║  ╚═ B2╚═ C

Rounded

root├─ A│  ├─ A1│  ╰─ A2├─ B│  ├─ B1│  │  ├─ B11│  │  ╰─ B12│  ╰─ B2╰─ C

Compact

root├ A│ ├ A1│ └ A2├ B│ ├ B1│ │ ├ B11│ │ └ B12│ └ B2└ C

Light

root|-- A|   |-- A1|   `-- A2|-- B|   |-- B1|   |   |-- B11|   |   `-- B12|   `-- B2`-- C

Minimal

root A.  A1. . A2 B.  B1. .  B11. . . B12. . B2. C

OskarStark and pyrech reacted with hooray emojialexandre-daubois, yceruto, GromNaN, OskarStark, chalasr, kaznovac, PabloKowalczyk, Aerendir, IAmBod, pyrech, and ging-dev reacted with rocket emoji
@carsonbot

This comment has been minimized.

@carsonbotcarsonbot added this to the7.3 milestoneJan 23, 2025
Copy link
Member

@chalasrchalasr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I like it!
Memory consumption should be considered as this deals with potentially large directories, ideally nodes should be yielded as they come.
Regarding the public API, we should remove or internalize any getter/setter for which we don't have a strong use case.

@smnandre
Copy link
MemberAuthor

I like it! Memory consumption should be considered as this deals with potentially large directories, ideally nodes should be yielded as they come.

Yep! I'll make the changes depending on the following questions.

Regarding the public API, we should remove or internalize any getter/setter for which we don't have a strong use case.

Do you have some examples in mind ?

Do we need bothcreateTree andtree in SymfonyStyle (i used Table as base/inspiration)...
Not sure there is a real need for the ->tree() method..

@chalasr
Copy link
Member

Do you have some examples in mind ?

I was refering toTree::getStyle()/setStyle() andTreeStyle::getPrefix*()

@smnandre
Copy link
MemberAuthor

smnandre commentedJan 26, 2025
edited by OskarStark
Loading

I've removed the getters and added a "appy" method on TreeStyle (we can considerRecursiveTreeIterator is part of the contract?)

publicfunctionapplyPrefixes(RecursiveTreeIterator$iterator):void{$iterator->setPrefixPart(RecursiveTreeIterator::PREFIX_LEFT,$this->prefixLeft);$iterator->setPrefixPart(RecursiveTreeIterator::PREFIX_MID_HAS_NEXT,$this->prefixMidHasNext);$iterator->setPrefixPart(RecursiveTreeIterator::PREFIX_MID_LAST,$this->prefixMidLast);$iterator->setPrefixPart(RecursiveTreeIterator::PREFIX_END_HAS_NEXT,$this->prefixEndHasNext);$iterator->setPrefixPart(RecursiveTreeIterator::PREFIX_END_LAST,$this->prefixEndLast);$iterator->setPrefixPart(RecursiveTreeIterator::PREFIX_RIGHT,$this->prefixRight);}

Also removed getStyle / setStyle

Will refactor as lazy iterator but i'm not sure how to handle the cycles yet

chalasr reacted with rocket emoji

@chadyred
Copy link
Contributor

chadyred commentedJan 27, 2025
edited
Loading

@smnandre It will be really useful 🙏 great job

Copy link
Contributor

@chadyredchadyred left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

💯

@smnandre
Copy link
MemberAuthor

smnandre commentedFeb 9, 2025
edited
Loading

I've fusionedTree andTreeBuilder intoTreeHelper, keeping in line with existing ones.

Removed the Finder integration for now (i'd rather have separated PR as it will require some discussion)

chadyred reacted with heart emoji

@smnandresmnandre changed the title[Console] Add a Tree Helper + multiple Styles (wip/feedback)[Console] Add a Tree Helper + multiple StylesFeb 9, 2025
@smnandre
Copy link
MemberAuthor

Open to review :)

@@ -369,6 +373,24 @@ private function getProgressBar(): ProgressBar
?? throw new RuntimeException('The ProgressBar is not started.');
}

/**
* @param iterable<string, iterable|string|TreeNode> $nodes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Some words or example about what can be passed would be nice either here or in TreeHelper or both

Copy link
Contributor

@chadyredchadyredFeb 10, 2025
edited by OskarStark
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

HA ! It is to avoid back and forth (with a future link to the doc) :), a sample like :

$treeHelper =newTreeHelper($output, TreeNode::fromValues(['a' => ['a1','a2'],'b','c' => ['c1','c2' => ['c21' => ['c211','c212',        ],    ],],],newTreeNode($rootNode),), TreeStyle::rounded());$treeHelper->render();

Copy link
Member

@alexandre-dauboisalexandre-daubois left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

A few comments but looks good otherwise 🙂

$parent->addChild($this);
}

foreach ($children as $child) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

iterating in the constructor breaks lazy evaluation of the iterable, is that justified?


public static function box(): self
{
return new self(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

on one line things would be better visually - not consuming my screen's space for nothing ;)

chadyred and smnandre reacted with laugh emoji
@chadyred
Copy link
Contributor

Last comments are none blocking suggestion

@fabpot
Copy link
Member

Thank you@smnandre.

@fabpotfabpot merged commit5c29eec intosymfony:7.3Feb 26, 2025
8 of 11 checks passed
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull requestMar 24, 2025
This PR was squashed before being merged into the 7.3 branch.Discussion----------[Console] Document the `TreeHelper`| Q | A || - | - || Feature PR | [symfony/symfony#59588](symfony/symfony#59588) || PR author(s) | [`@smnandre`](https://github.com/smnandre) || Merged in | 7.3 || Doc Issue |Fix#20692 |fix#20692Commits-------d9a48be [Console] Document the `TreeHelper`
@fabpotfabpot mentioned this pull requestMay 2, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@nicolas-grekasnicolas-grekasnicolas-grekas left review comments

@chadyredchadyredchadyred left review comments

@fabpotfabpotfabpot approved these changes

@OskarStarkOskarStarkOskarStark approved these changes

@alexandre-dauboisalexandre-dauboisalexandre-daubois approved these changes

@chalasrchalasrchalasr approved these changes

@stofstofAwaiting requested review from stof

Assignees
No one assigned
Projects
None yet
Milestone
7.3
Development

Successfully merging this pull request may close these issues.

9 participants
@smnandre@carsonbot@chalasr@chadyred@fabpot@nicolas-grekas@stof@OskarStark@alexandre-daubois

[8]ページ先頭

©2009-2025 Movatter.jp