diff --git a/src/CliMenuBuilder.php b/src/CliMenuBuilder.php index 240c4b76..91c921e6 100644 --- a/src/CliMenuBuilder.php +++ b/src/CliMenuBuilder.php @@ -180,7 +180,11 @@ public function addAsciiArt($art, $position = AsciiArtItem::POSITION_CENTER) Assertion::string($art); Assertion::string($position); - $this->addMenuItem(new AsciiArtItem($art, $position)); + $asciiArtItem = new AsciiArtItem($art, $position); + + if ($asciiArtItem->getArtLength() <= $this->getMenuStyle()->getContentWidth()) { + $this->addMenuItem($asciiArtItem); + } return $this; } diff --git a/src/MenuItem/AsciiArtItem.php b/src/MenuItem/AsciiArtItem.php index 5b3b4193..d1807b4b 100644 --- a/src/MenuItem/AsciiArtItem.php +++ b/src/MenuItem/AsciiArtItem.php @@ -116,6 +116,16 @@ public function getText() return $this->text; } + /** + * Return the length of the art + * + * @return int + */ + public function getArtLength() + { + return $this->artLength; + } + /** * Whether or not the menu item is showing the menustyle extra value * diff --git a/test/CliMenuBuilderTest.php b/test/CliMenuBuilderTest.php index 7dc1efa9..09b33785 100644 --- a/test/CliMenuBuilderTest.php +++ b/test/CliMenuBuilderTest.php @@ -247,6 +247,18 @@ public function testAsciiArtWithSpecificPosition() $this->checkItems($menu, $expected); } + public function testAddAsciiArtDetectsArtThatDoesNotFitAndSkipsIt() + { + $builder = new CliMenuBuilder; + $builder->setWidth(1); + $builder->addAsciiArt("//\n//", AsciiArtItem::POSITION_LEFT); + $menu = $builder->build(); + + foreach ($menu->getItems() as $menuItem) { + $this->assertNotInstanceOf(AsciiArtItem::class, $menuItem); + } + } + public function testEndThrowsExceptionIfNoParentBuilder() { $builder = new CliMenuBuilder; diff --git a/test/MenuItem/AsciiArtItemTest.php b/test/MenuItem/AsciiArtItemTest.php index 7bdb6767..2747ec00 100644 --- a/test/MenuItem/AsciiArtItemTest.php +++ b/test/MenuItem/AsciiArtItemTest.php @@ -50,6 +50,12 @@ public function testGetText() $this->assertEquals('////\\\\', $item->getText()); } + public function testGetArtLength() + { + $item = new AsciiArtItem("//\n//\n///"); + $this->assertEquals(3, $item->getArtLength()); + } + public function testGetRowsLeftAligned() { $menuStyle = $this->createMock(MenuStyle::class);