-
Notifications
You must be signed in to change notification settings - Fork 107
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
margin out of contentWidth + add menu centering #103
Conversation
Of course, this messed up some tests. If this is accepted, I will take care of fixing those. |
Codecov Report
@@ Coverage Diff @@
## master #103 +/- ##
===========================================
+ Coverage 96.68% 96.7% +0.02%
- Complexity 293 297 +4
===========================================
Files 22 22
Lines 905 911 +6
===========================================
+ Hits 875 881 +6
Misses 30 30
Continue to review full report at Codecov.
|
src/MenuStyle.php
Outdated
} | ||
|
||
$this->width = $width; | ||
if ($this->margin === -1) { |
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.
If we want to center the menu we force recalculation of margin by setting it again each time the width is modified.
src/MenuStyle.php
Outdated
$this->margin = $margin; | ||
|
||
$this->calculateContentWidth(); | ||
if ($this->margin === -1) { |
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.
I think this is meant to be $margin === -1
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.
Indeed :)
Margin auto fix
Sadly this makes "responsive" layouts really complicated as the content doesn't auto fit any more if you add margins around the border. |
You can just do something like: $menu = ($builder = new CliMenuBuilder)
->setTitle('Basic CLI Menu')
->addItem('First Item', $itemCallable)
->addItem('Second Item', $itemCallable)
->addItem('Third Item', $itemCallable)
->addLineBreak('-')
->setBorder(1, 'green')
->setWidth($builder->getTerminal()->getWidth() - 2 * 2)
->setMargin(2)
->build(); For auto width with a margin of 2. I agree we could probably make it easier or mention in the docs. |
Thank you very much!
that would be great. It's not obvious what kind of box model you have 😄 |
Ok so this makes the whole "width" thing more consistent. It now works like border-box in CSS (it includes the padding and borders but not the margin):
This prevents PHP notices and improves rendered output when values are off the charts.
The following was made with width = 120, padding = 10 and margin = 120 on a 237 column terminal.
Before PR (this also throws notices because negative values are passed to str_repeat):
After PR:
It also adds the possibility to automatically center the whole menu by setting margins to
-1
. I wanted to set it to 'auto' instead but since it's already typehinted to int and all, I thought -1 was good enough. This can be changed though.Anyway let me know what you think.