Skip to content

Commit aa4c9c8

Browse files
committed
Move right hand padding calculation to MenuStyle and add tests
1 parent 2d1412b commit aa4c9c8

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

src/CliMenu.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -376,13 +376,6 @@ protected function drawMenuItem(MenuItemInterface $item, bool $selected = false)
376376
}
377377

378378
return array_map(function ($row) use ($invertedColour, $notInvertedColour, $borderColour) {
379-
380-
$rightPadding = $this->style->getRightHandPadding(mb_strlen(s::stripAnsiEscapeSequence($row)));
381-
382-
if ($rightPadding < 0) {
383-
$rightPadding = 0;
384-
}
385-
386379
return sprintf(
387380
"%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
388381
str_repeat(' ', $this->style->getMargin()),
@@ -392,7 +385,7 @@ protected function drawMenuItem(MenuItemInterface $item, bool $selected = false)
392385
$invertedColour,
393386
str_repeat(' ', $this->style->getPadding()),
394387
$row,
395-
str_repeat(' ', $rightPadding),
388+
str_repeat(' ', $this->style->getRightHandPadding(mb_strlen(s::stripAnsiEscapeSequence($row)))),
396389
$notInvertedColour,
397390
$borderColour,
398391
str_repeat(' ', $this->style->getBorderRightWidth()),

src/MenuStyle.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,13 @@ public function getContentWidth() : int
409409
*/
410410
public function getRightHandPadding(int $contentLength) : int
411411
{
412-
return $this->getContentWidth() - $contentLength + $this->getPadding();
412+
$rightPadding = $this->getContentWidth() - $contentLength + $this->getPadding();
413+
414+
if ($rightPadding < 0) {
415+
$rightPadding = 0;
416+
}
417+
418+
return $rightPadding;
413419
}
414420

415421
public function getSelectedMarker() : string

test/MenuStyleTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,35 @@ public function testRightHandPaddingCalculation() : void
301301
static::assertSame(241, $style->getRightHandPadding(50));
302302
}
303303

304+
public function testRightHandPaddingReturnsZeroWhenContentLengthTooLong() : void
305+
{
306+
$style = $this->getMenuStyle();
307+
$style->setPadding(0);
308+
$style->setMargin(0);
309+
$style->setBorder(0);
310+
311+
$style->setWidth(100);
312+
313+
self::assertEquals(0, $style->getRightHandPadding(100));
314+
self::assertEquals(0, $style->getRightHandPadding(150));
315+
}
316+
317+
public function testRightHandPaddingReturnsZeroWhenContentLengthTooLongBecauseOfBorder() : void
318+
{
319+
$style = $this->getMenuStyle();
320+
$style->setPadding(10);
321+
$style->setMargin(0);
322+
$style->setBorder(10);
323+
324+
$style->setWidth(100);
325+
326+
self::assertEquals(11, $style->getRightHandPadding(59));
327+
self::assertEquals(10, $style->getRightHandPadding(60));
328+
self::assertEquals(0, $style->getRightHandPadding(70));
329+
self::assertEquals(0, $style->getRightHandPadding(71));
330+
self::assertEquals(0, $style->getRightHandPadding(100));
331+
}
332+
304333
public function testMargin() : void
305334
{
306335
$style = $this->getMenuStyle();

0 commit comments

Comments
 (0)