Skip to content
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
21 changes: 8 additions & 13 deletions src/MenuItem/AsciiArtItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public function __construct(string $text, string $position = self::POSITION_CENT
{
Assertion::inArray($position, [self::POSITION_CENTER, self::POSITION_RIGHT, self::POSITION_LEFT]);

$this->text = $text;
$this->text = implode("\n", array_map(function (string $line) {
return rtrim($line, ' ');
}, explode("\n", $text)));
$this->position = $position;
$this->alternateText = $alt;
$this->artLength = max(array_map('mb_strlen', explode("\n", $text)));
Expand All @@ -57,26 +59,19 @@ public function getRows(MenuStyle $style, bool $selected = false) : array
return $alternate->getRows($style, false);
}

return array_map(function ($row) use ($style) {
$length = mb_strlen($row);

$padding = $style->getContentWidth() - $length;

$padding = $style->getContentWidth() - $this->artLength;

return array_map(function ($row) use ($padding) {
switch ($this->position) {
case self::POSITION_LEFT:
return $row;
break;
case self::POSITION_RIGHT:
$row = rtrim($row);
$padding = $padding - ($this->artLength - mb_strlen($row));
$row = sprintf('%s%s', str_repeat(' ', $padding), $row);
break;
case self::POSITION_CENTER:
default:
$padding = $padding - ($this->artLength - $length);
$left = ceil($padding/2);
$right = $padding - $left;
$row = sprintf('%s%s%s', str_repeat(' ', $left), $row, str_repeat(' ', $right));
$left = ceil($padding / 2);
$row = sprintf('%s%s', str_repeat(' ', $left), $row);
break;
}

Expand Down
123 changes: 100 additions & 23 deletions test/MenuItem/AsciiArtItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,8 @@ public function testGetRowsCenterAligned() : void
$item = new AsciiArtItem("//\n//", AsciiArtItem::POSITION_CENTER);
$this->assertEquals(
[
" // ",
" // ",
],
$item->getRows($menuStyle)
);

$item = new AsciiArtItem(" // \n//////////", AsciiArtItem::POSITION_CENTER);
$this->assertEquals(
[
" // ",
"//////////",
" //",
" //",
],
$item->getRows($menuStyle)
);
Expand All @@ -119,17 +110,8 @@ public function testGetRowsCenterAlignedWithOddWidth() : void
$item = new AsciiArtItem("//\n//", AsciiArtItem::POSITION_CENTER);
$this->assertEquals(
[
" // ",
" // ",
],
$item->getRows($menuStyle)
);

$item = new AsciiArtItem(" // \n//////////", AsciiArtItem::POSITION_CENTER);
$this->assertEquals(
[
" // ",
" //////////",
" //",
" //",
],
$item->getRows($menuStyle)
);
Expand All @@ -146,7 +128,7 @@ public function testHideAndShowItemExtraHasNoEffect() : void
$this->assertFalse($item->showsItemExtra());
}

public function testGetRowsReturnsStaticAltItemWhenWidthIsTooSmall()
public function testGetRowsReturnsStaticAltItemWhenWidthIsTooSmall() : void
{
$menuStyle = $this->createMock(MenuStyle::class);

Expand All @@ -159,4 +141,99 @@ public function testGetRowsReturnsStaticAltItemWhenWidthIsTooSmall()

self::assertSame(['my alt'], $item->getRows($menuStyle));
}

public function testWithRealAsciiArtCenterAligned() : void
{
$menuStyle = $this->createMock(MenuStyle::class);

$menuStyle
->expects($this->any())
->method('getContentWidth')
->will($this->returnValue(30));

$art = <<<ART
_ __ _
/ |..| \
\/ || \/
|_''_|
PHP SCHOOL
LEARNING FOR ELEPHANTS
ART;

$item = new AsciiArtItem($art, AsciiArtItem::POSITION_CENTER);
$this->assertEquals(
[
' _ __ _',
' / |..| \\',
' \/ || \/',
" |_''_|",
' PHP SCHOOL',
' LEARNING FOR ELEPHANTS'
],
$item->getRows($menuStyle)
);
}

public function testWithRealAsciiArtRightAligned() : void
{
$menuStyle = $this->createMock(MenuStyle::class);

$menuStyle
->expects($this->any())
->method('getContentWidth')
->will($this->returnValue(30));

$art = <<<ART
_ __ _
/ |..| \
\/ || \/
|_''_|
PHP SCHOOL
LEARNING FOR ELEPHANTS
ART;

$item = new AsciiArtItem($art, AsciiArtItem::POSITION_RIGHT);
$this->assertEquals(
[
' _ __ _',
' / |..| \\',
' \/ || \/',
" |_''_|",
' PHP SCHOOL',
' LEARNING FOR ELEPHANTS'
],
$item->getRows($menuStyle)
);
}

public function testWithRealAsciiArtCenterAlignedWithWhiteSpaceAtTheEndOfEachLine() : void
{
$menuStyle = $this->createMock(MenuStyle::class);

$menuStyle
->expects($this->any())
->method('getContentWidth')
->will($this->returnValue(30));

$art = <<<ART
_ __ _
/ |..| \
\/ || \/
|_''_|
PHP SCHOOL
LEARNING FOR ELEPHANTS
ART;
$item = new AsciiArtItem($art, AsciiArtItem::POSITION_CENTER);
$this->assertEquals(
[
' _ __ _',
' / |..| \\',
' \/ || \/',
" |_''_|",
' PHP SCHOOL',
' LEARNING FOR ELEPHANTS'
],
$item->getRows($menuStyle)
);
}
}