Skip to content

Commit fe3209e

Browse files
committed
Fix margin auto when updating width
1 parent 7161561 commit fe3209e

File tree

5 files changed

+144
-12
lines changed

5 files changed

+144
-12
lines changed

examples/basic-centered.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212
$menu = (new CliMenuBuilder)
1313
->setTitle('Basic CLI Menu')
1414
->addItem('First Item', $itemCallable)
15-
->addItem('Second Item', $itemCallable)
15+
->addItem('Make menu wider', function (CliMenu $menu) {
16+
$menu->getStyle()->setWidth($menu->getStyle()->getWidth() + 10);
17+
$menu->redraw();
18+
})
1619
->addItem('Third Item', $itemCallable)
1720
->addLineBreak('-')
1821
->setWidth(70)
19-
->setMargin(-1)
22+
->setMarginAuto()
2023
->build();
2124

2225
$menu->open();

src/CliMenuBuilder.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,18 @@ public function setPadding(int $padding) : self
230230
return $this;
231231
}
232232

233-
public function setMargin(int $margin) : self
233+
public function setMarginAuto() : self
234234
{
235+
$this->style['marginAuto'] = true;
236+
237+
return $this;
238+
}
239+
240+
public function setMargin(int $margin) : self
241+
{
242+
Assertion::greaterOrEqualThan($margin, 0);
243+
244+
$this->style['marginAuto'] = false;
235245
$this->style['margin'] = $margin;
236246

237247
return $this;
@@ -320,7 +330,7 @@ private function getMenuStyle() : MenuStyle
320330

321331
private function buildStyle() : MenuStyle
322332
{
323-
return (new MenuStyle($this->terminal))
333+
$style = (new MenuStyle($this->terminal))
324334
->setFg($this->style['fg'])
325335
->setBg($this->style['bg'])
326336
->setWidth($this->style['width'])
@@ -331,6 +341,10 @@ private function buildStyle() : MenuStyle
331341
->setItemExtra($this->style['itemExtra'])
332342
->setDisplaysExtra($this->style['displaysExtra'])
333343
->setTitleSeparator($this->style['titleSeparator']);
344+
345+
$this->style['marginAuto'] ? $style->setMarginAuto() : $style->setMargin($this->style['margin']);
346+
347+
return $style;
334348
}
335349

336350
/**

src/MenuStyle.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ class MenuStyle
7373
*/
7474
private $titleSeparator;
7575

76+
/**
77+
* @var bool
78+
*/
79+
private $marginAuto = false;
80+
7681
/**
7782
* Default Values
7883
*
@@ -89,6 +94,7 @@ class MenuStyle
8994
'itemExtra' => '',
9095
'displaysExtra' => false,
9196
'titleSeparator' => '=',
97+
'marginAuto' => false,
9298
];
9399

94100
public static function getDefaultStyleValues() : array
@@ -272,8 +278,8 @@ public function setWidth(int $width) : self
272278
}
273279

274280
$this->width = $width;
275-
if ($this->margin === -1) {
276-
$this->setMargin(-1);
281+
if ($this->marginAuto) {
282+
$this->setMarginAuto();
277283
}
278284
$this->calculateContentWidth();
279285

@@ -299,13 +305,18 @@ public function getMargin() : int
299305
return $this->margin;
300306
}
301307

308+
public function setMarginAuto() : self
309+
{
310+
$this->marginAuto = true;
311+
$this->margin = floor(($this->terminal->getWidth() - $this->width) / 2);
312+
313+
return $this;
314+
}
315+
302316
public function setMargin(int $margin) : self
303317
{
304-
if ($margin === -1) {
305-
$this->margin = floor(($this->terminal->getWidth() - $this->width) / 2);
306-
} else {
307-
$this->margin = $margin;
308-
}
318+
$this->marginAuto = false;
319+
$this->margin = $margin;
309320

310321
return $this;
311322
}

test/CliMenuBuilderTest.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,93 @@ public function testThrowsExceptionWhenDisablingRootMenu() : void
437437

438438
(new CliMenuBuilder)->disableMenu();
439439
}
440+
441+
/**
442+
* @dataProvider marginBelowZeroProvider
443+
*/
444+
public function testSetMarginThrowsExceptionIfValueIsNotZeroOrAbove(int $value) : void
445+
{
446+
self::expectException(\Assert\InvalidArgumentException::class);
447+
448+
449+
(new CliMenuBuilder)->setMargin($value);
450+
}
451+
452+
public function marginBelowZeroProvider() : array
453+
{
454+
return [[-1], [-2], [-10]];
455+
}
456+
457+
/**
458+
* @dataProvider marginAboveZeroProvider
459+
*/
460+
public function testSetMarginAcceptsZeroAndPositiveIntegers(int $value) : void
461+
{
462+
$menu = (new CliMenuBuilder)->setMargin($value)->build();
463+
464+
self::assertSame($value, $menu->getStyle()->getMargin());
465+
}
466+
467+
public function marginAboveZeroProvider() : array
468+
{
469+
return [[0], [1], [10], [50]];
470+
}
471+
472+
public function testSetMarginAutoAutomaticallyCalculatesMarginToCenter() : void
473+
{
474+
$terminal = self::createMock(Terminal::class);
475+
$terminal
476+
->expects($this->any())
477+
->method('getWidth')
478+
->will($this->returnValue(200));
479+
480+
$builder = new CliMenuBuilder;
481+
$menu = $builder
482+
->setTerminal($terminal)
483+
->setMarginAuto()
484+
->setWidth(100)
485+
->build();
486+
487+
self::assertSame(50, $menu->getStyle()->getMargin());
488+
}
489+
490+
public function testSetMarginAutoOverwritesSetMargin() : void
491+
{
492+
$terminal = self::createMock(Terminal::class);
493+
$terminal
494+
->expects($this->any())
495+
->method('getWidth')
496+
->will($this->returnValue(200));
497+
498+
$builder = new CliMenuBuilder;
499+
$menu = $builder
500+
->setTerminal($terminal)
501+
->setMargin(10)
502+
->setMarginAuto()
503+
->setWidth(100)
504+
->build();
505+
506+
self::assertSame(50, $menu->getStyle()->getMargin());
507+
}
508+
509+
public function testSetMarginManuallyOverwritesSetMarginAuto() : void
510+
{
511+
$terminal = self::createMock(Terminal::class);
512+
$terminal
513+
->expects($this->any())
514+
->method('getWidth')
515+
->will($this->returnValue(200));
516+
517+
$builder = new CliMenuBuilder;
518+
$menu = $builder
519+
->setTerminal($terminal)
520+
->setMarginAuto()
521+
->setMargin(10)
522+
->setWidth(100)
523+
->build();
524+
525+
self::assertSame(10, $menu->getStyle()->getMargin());
526+
}
440527

441528
private function checkItems(CliMenu $menu, array $expected) : void
442529
{

test/MenuStyleTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,26 @@ public function testMarginAutoCenters() : void
191191

192192
$style->setWidth(300);
193193
$style->setPadding(5);
194-
$style->setMargin(-1);
194+
$style->setMarginAuto();
195195

196196
self::assertSame(100, $style->getMargin());
197197
self::assertSame(290, $style->getContentWidth());
198198
}
199+
200+
public function testModifyWithWhenMarginAutoIsEnabledRecalculatesMargin() : void
201+
{
202+
$style = $this->getMenuStyle();
203+
204+
$style->setWidth(300);
205+
$style->setPadding(5);
206+
$style->setMarginAuto();
207+
208+
self::assertSame(100, $style->getMargin());
209+
self::assertSame(290, $style->getContentWidth());
210+
211+
$style->setWidth(400);
212+
213+
self::assertSame(50, $style->getMargin());
214+
self::assertSame(390, $style->getContentWidth());
215+
}
199216
}

0 commit comments

Comments
 (0)