From 2d1412b9fd196afad75eb59fcd4db26e8f0509bf Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Mon, 7 May 2018 19:44:22 +0200 Subject: [PATCH 1/2] Fix right padding when row too long --- src/CliMenu.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/CliMenu.php b/src/CliMenu.php index 29eb5749..2575419c 100644 --- a/src/CliMenu.php +++ b/src/CliMenu.php @@ -376,6 +376,13 @@ protected function drawMenuItem(MenuItemInterface $item, bool $selected = false) } return array_map(function ($row) use ($invertedColour, $notInvertedColour, $borderColour) { + + $rightPadding = $this->style->getRightHandPadding(mb_strlen(s::stripAnsiEscapeSequence($row))); + + if ($rightPadding < 0) { + $rightPadding = 0; + } + return sprintf( "%s%s%s%s%s%s%s%s%s%s%s%s%s\n", str_repeat(' ', $this->style->getMargin()), @@ -385,7 +392,7 @@ protected function drawMenuItem(MenuItemInterface $item, bool $selected = false) $invertedColour, str_repeat(' ', $this->style->getPadding()), $row, - str_repeat(' ', $this->style->getRightHandPadding(mb_strlen(s::stripAnsiEscapeSequence($row)))), + str_repeat(' ', $rightPadding), $notInvertedColour, $borderColour, str_repeat(' ', $this->style->getBorderRightWidth()), From aa4c9c81a69e002fcf879f211beea4fc1b751cad Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Tue, 8 May 2018 09:26:43 +0200 Subject: [PATCH 2/2] Move right hand padding calculation to MenuStyle and add tests --- src/CliMenu.php | 9 +-------- src/MenuStyle.php | 8 +++++++- test/MenuStyleTest.php | 29 +++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/CliMenu.php b/src/CliMenu.php index 2575419c..29eb5749 100644 --- a/src/CliMenu.php +++ b/src/CliMenu.php @@ -376,13 +376,6 @@ protected function drawMenuItem(MenuItemInterface $item, bool $selected = false) } return array_map(function ($row) use ($invertedColour, $notInvertedColour, $borderColour) { - - $rightPadding = $this->style->getRightHandPadding(mb_strlen(s::stripAnsiEscapeSequence($row))); - - if ($rightPadding < 0) { - $rightPadding = 0; - } - return sprintf( "%s%s%s%s%s%s%s%s%s%s%s%s%s\n", str_repeat(' ', $this->style->getMargin()), @@ -392,7 +385,7 @@ protected function drawMenuItem(MenuItemInterface $item, bool $selected = false) $invertedColour, str_repeat(' ', $this->style->getPadding()), $row, - str_repeat(' ', $rightPadding), + str_repeat(' ', $this->style->getRightHandPadding(mb_strlen(s::stripAnsiEscapeSequence($row)))), $notInvertedColour, $borderColour, str_repeat(' ', $this->style->getBorderRightWidth()), diff --git a/src/MenuStyle.php b/src/MenuStyle.php index a5b01ae2..5ae37bcf 100644 --- a/src/MenuStyle.php +++ b/src/MenuStyle.php @@ -409,7 +409,13 @@ public function getContentWidth() : int */ public function getRightHandPadding(int $contentLength) : int { - return $this->getContentWidth() - $contentLength + $this->getPadding(); + $rightPadding = $this->getContentWidth() - $contentLength + $this->getPadding(); + + if ($rightPadding < 0) { + $rightPadding = 0; + } + + return $rightPadding; } public function getSelectedMarker() : string diff --git a/test/MenuStyleTest.php b/test/MenuStyleTest.php index b7f95cd4..08c4fb5e 100644 --- a/test/MenuStyleTest.php +++ b/test/MenuStyleTest.php @@ -301,6 +301,35 @@ public function testRightHandPaddingCalculation() : void static::assertSame(241, $style->getRightHandPadding(50)); } + public function testRightHandPaddingReturnsZeroWhenContentLengthTooLong() : void + { + $style = $this->getMenuStyle(); + $style->setPadding(0); + $style->setMargin(0); + $style->setBorder(0); + + $style->setWidth(100); + + self::assertEquals(0, $style->getRightHandPadding(100)); + self::assertEquals(0, $style->getRightHandPadding(150)); + } + + public function testRightHandPaddingReturnsZeroWhenContentLengthTooLongBecauseOfBorder() : void + { + $style = $this->getMenuStyle(); + $style->setPadding(10); + $style->setMargin(0); + $style->setBorder(10); + + $style->setWidth(100); + + self::assertEquals(11, $style->getRightHandPadding(59)); + self::assertEquals(10, $style->getRightHandPadding(60)); + self::assertEquals(0, $style->getRightHandPadding(70)); + self::assertEquals(0, $style->getRightHandPadding(71)); + self::assertEquals(0, $style->getRightHandPadding(100)); + } + public function testMargin() : void { $style = $this->getMenuStyle();