Skip to content

Adds alternateText to AsciiArtItem #93

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
May 2, 2018
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
8 changes: 2 additions & 6 deletions src/CliMenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,9 @@ public function addLineBreak(string $breakChar = ' ', int $lines = 1) : self
return $this;
}

public function addAsciiArt(string $art, string $position = AsciiArtItem::POSITION_CENTER) : self
public function addAsciiArt(string $art, string $position = AsciiArtItem::POSITION_CENTER, string $alt) : self
{
$asciiArtItem = new AsciiArtItem($art, $position);

if ($asciiArtItem->getArtLength() <= $this->getMenuStyle()->getContentWidth()) {
$this->addMenuItem($asciiArtItem);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be cleaner to just add a static item here instead? Then we wouldn't have to modify AsciiArtItem

Copy link
Collaborator Author

@Lynesth Lynesth May 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then it wouldn't work if you first create the AsciiArtItem and then add it using addMenuItem().

Edit: That's why I went with this method.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok makes sense - I begin to think that the CliMenuBuilder was not such a good idea.

}
$this->addMenuItem(new AsciiArtItem($art, $position, $alt));

return $this;
}
Expand Down
13 changes: 12 additions & 1 deletion src/MenuItem/AsciiArtItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,23 @@ class AsciiArtItem implements MenuItemInterface
*/
private $position;

/**
* @var string
*/
private $alternateText;

/**
* @var int
*/
private $artLength;

public function __construct(string $text, string $position = self::POSITION_CENTER)
public function __construct(string $text, string $position = self::POSITION_CENTER, string $alt = '')
{
Assertion::inArray($position, [self::POSITION_CENTER, self::POSITION_RIGHT, self::POSITION_LEFT]);

$this->text = $text;
$this->position = $position;
$this->alternateText = $alt;
$this->artLength = max(array_map('mb_strlen', explode("\n", $text)));
}

Expand All @@ -46,6 +52,11 @@ public function __construct(string $text, string $position = self::POSITION_CENT
*/
public function getRows(MenuStyle $style, bool $selected = false) : array
{
if ($this->artLength > $style->getContentWidth()) {
$alternate = new StaticItem($this->alternateText);
return $alternate->getRows($style, false);
}

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

Expand Down