Skip to content

Commit f08d25e

Browse files
authored
Merge pull request #63 from robertboloc/feature-5
Detect Ascii art that does not fit in the menu
2 parents 9e7c884 + 1eee3d6 commit f08d25e

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

src/CliMenuBuilder.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,11 @@ public function addAsciiArt($art, $position = AsciiArtItem::POSITION_CENTER)
180180
Assertion::string($art);
181181
Assertion::string($position);
182182

183-
$this->addMenuItem(new AsciiArtItem($art, $position));
183+
$asciiArtItem = new AsciiArtItem($art, $position);
184+
185+
if ($asciiArtItem->getArtLength() <= $this->getMenuStyle()->getContentWidth()) {
186+
$this->addMenuItem($asciiArtItem);
187+
}
184188

185189
return $this;
186190
}

src/MenuItem/AsciiArtItem.php

+10
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ public function getText()
116116
return $this->text;
117117
}
118118

119+
/**
120+
* Return the length of the art
121+
*
122+
* @return int
123+
*/
124+
public function getArtLength()
125+
{
126+
return $this->artLength;
127+
}
128+
119129
/**
120130
* Whether or not the menu item is showing the menustyle extra value
121131
*

test/CliMenuBuilderTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,18 @@ public function testAsciiArtWithSpecificPosition()
247247
$this->checkItems($menu, $expected);
248248
}
249249

250+
public function testAddAsciiArtDetectsArtThatDoesNotFitAndSkipsIt()
251+
{
252+
$builder = new CliMenuBuilder;
253+
$builder->setWidth(1);
254+
$builder->addAsciiArt("//\n//", AsciiArtItem::POSITION_LEFT);
255+
$menu = $builder->build();
256+
257+
foreach ($menu->getItems() as $menuItem) {
258+
$this->assertNotInstanceOf(AsciiArtItem::class, $menuItem);
259+
}
260+
}
261+
250262
public function testEndThrowsExceptionIfNoParentBuilder()
251263
{
252264
$builder = new CliMenuBuilder;

test/MenuItem/AsciiArtItemTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ public function testGetText()
5050
$this->assertEquals('////\\\\', $item->getText());
5151
}
5252

53+
public function testGetArtLength()
54+
{
55+
$item = new AsciiArtItem("//\n//\n///");
56+
$this->assertEquals(3, $item->getArtLength());
57+
}
58+
5359
public function testGetRowsLeftAligned()
5460
{
5561
$menuStyle = $this->createMock(MenuStyle::class);

0 commit comments

Comments
 (0)