Skip to content

Commit

Permalink
Merge pull request #63 from robertboloc/feature-5
Browse files Browse the repository at this point in the history
Detect Ascii art that does not fit in the menu
  • Loading branch information
mikeymike authored Mar 1, 2017
2 parents 9e7c884 + 1eee3d6 commit f08d25e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/CliMenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 10 additions & 0 deletions src/MenuItem/AsciiArtItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
12 changes: 12 additions & 0 deletions test/CliMenuBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions test/MenuItem/AsciiArtItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f08d25e

Please sign in to comment.