Skip to content

Detect Ascii art that does not fit in the menu #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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