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

margin out of contentWidth + add menu centering #103

Merged
merged 13 commits into from
May 7, 2018

Conversation

Lynesth
Copy link
Collaborator

@Lynesth Lynesth commented May 5, 2018

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):
firefox_2018-05-05_19-05-08

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):
mintty_2018-05-05_19-12-33
After PR:
mintty_2018-05-05_19-13-09

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.

@Lynesth
Copy link
Collaborator Author

Lynesth commented May 5, 2018

Of course, this messed up some tests. If this is accepted, I will take care of fixing those.

@codecov-io
Copy link

codecov-io commented May 5, 2018

Codecov Report

Merging #103 into master will increase coverage by 0.02%.
The diff coverage is 100%.

Impacted file tree graph

@@             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
Impacted Files Coverage Δ Complexity Δ
src/CliMenuBuilder.php 98.65% <100%> (+0.05%) 47 <1> (+2) ⬆️
src/Dialogue/Dialogue.php 90% <100%> (ø) 8 <0> (ø) ⬇️
src/MenuStyle.php 94.78% <100%> (+0.23%) 37 <1> (+2) ⬆️
src/Input/InputIO.php 98.44% <100%> (ø) 24 <0> (ø) ⬇️
src/MenuItem/AsciiArtItem.php 100% <0%> (ø) 14% <0%> (ø) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update dc3a2b7...4b40002. Read the comment docs.

}

$this->width = $width;
if ($this->margin === -1) {
Copy link
Collaborator Author

@Lynesth Lynesth May 5, 2018

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.

@AydinHassan AydinHassan requested a review from mikeymike May 7, 2018 08:19
$this->margin = $margin;

$this->calculateContentWidth();
if ($this->margin === -1) {
Copy link
Member

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Indeed :)

@AydinHassan AydinHassan merged commit 06534f7 into php-school:master May 7, 2018
@AydinHassan AydinHassan added this to the 3.0 milestone May 8, 2018
@simonschaufi
Copy link

Sadly this makes "responsive" layouts really complicated as the content doesn't auto fit any more if you add margins around the border.

@AydinHassan
Copy link
Member

AydinHassan commented May 22, 2018

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.

@simonschaufi
Copy link

Thank you very much!

mention in the docs

that would be great. It's not obvious what kind of box model you have 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants