Skip to content

Finalize SelectableStyle for items #216

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 6 commits into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
7 changes: 5 additions & 2 deletions examples/draw.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PhpSchool\CliMenu\CliMenu;
use PhpSchool\CliMenu\Builder\CliMenuBuilder;
use PhpSchool\CliMenu\MenuItem\SplitItem;
use PhpSchool\CliMenu\Style\SelectableStyle;

require_once(__DIR__ . '/../vendor/autoload.php');

Expand Down Expand Up @@ -39,8 +40,10 @@
->setBorder(0)
->setMargin(2)
->setPadding(2, 5)
->setSelectedMarker('')
->setUnselectedMarker('')
->modifySelectableStyle(function (SelectableStyle $style) {
$style->setSelectedMarker('')
->setUnselectedMarker('');
})
->addAsciiArt('Draw your own art !')
->addLineBreak();

Expand Down
19 changes: 2 additions & 17 deletions src/Builder/CliMenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PhpSchool\CliMenu\MenuItem\RadioItem;
use PhpSchool\CliMenu\MenuItem\SelectableItem;
use PhpSchool\CliMenu\CliMenu;
use PhpSchool\CliMenu\MenuItem\SelectableStyleInterface;
use PhpSchool\CliMenu\MenuItem\SplitItem;
use PhpSchool\CliMenu\MenuItem\StaticItem;
use PhpSchool\CliMenu\MenuStyle;
Expand Down Expand Up @@ -394,22 +395,6 @@ public function setMargin(int $margin) : self
return $this;
}

public function setUnselectedMarker(string $marker) : self
{
$this->style->setUnselectedMarker($marker);
$this->menu->getSelectableStyle()->setUnselectedMarker($marker);

return $this;
}

public function setSelectedMarker(string $marker) : self
{
$this->style->setSelectedMarker($marker);
$this->menu->getSelectableStyle()->setSelectedMarker($marker);

return $this;
}

public function setItemExtra(string $extra) : self
{
$this->style->setItemExtra($extra);
Expand Down Expand Up @@ -608,7 +593,7 @@ private function propagateStyles(CliMenu $menu, array $items = [])
$item->setStyle(clone $menu->getRadioStyle());
}

if ($item instanceof SelectableItem
if ($item instanceof SelectableStyleInterface
&& !$item->getStyle()->hasChangedFromDefaults()
) {
$item->setStyle(clone $menu->getSelectableStyle());
Expand Down
107 changes: 101 additions & 6 deletions src/MenuItem/MenuMenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,80 @@

namespace PhpSchool\CliMenu\MenuItem;

use Assert\Assertion;
use PhpSchool\CliMenu\CliMenu;
use PhpSchool\CliMenu\MenuStyle;
use PhpSchool\CliMenu\Util\StringUtil;
use PhpSchool\CliMenu\Style\SelectableStyle;

/**
* @author Michael Woodward <[email protected]>
*/
class MenuMenuItem implements MenuItemInterface
class MenuMenuItem implements MenuItemInterface, SelectableStyleInterface
{
use SelectableTrait;
private $text = '';

private $showItemExtra = false;

private $disabled = false;

/**
* @var SelectableStyle;
*/
private $style;

/**
* @var CliMenu
*/
private $subMenu;

public function __construct(string $text, CliMenu $subMenu, bool $disabled = false)
{
public function __construct(
string $text,
CliMenu $subMenu,
bool $disabled = false
) {
$this->text = $text;
$this->subMenu = $subMenu;
$this->disabled = $disabled;

$this->style = new SelectableStyle();
}

/**
* The output text for the item
*/
public function getRows(MenuStyle $style, bool $selected = false) : array
{
$marker = sprintf("%s", $this->style->getMarker($selected));

$length = $this->style->getDisplaysExtra()
? $style->getContentWidth() - (mb_strlen($this->style->getItemExtra()) + 2)
: $style->getContentWidth();

$rows = explode(
"\n",
StringUtil::wordwrap(
sprintf('%s%s', $marker, $this->text),
$length,
sprintf("\n%s", str_repeat(' ', mb_strlen($marker)))
)
);

return array_map(function ($row, $key) use ($style, $length) {
$text = $this->disabled ? $style->getDisabledItemText($row) : $row;

if ($key === 0) {
return $this->showItemExtra
? sprintf(
'%s%s %s',
$text,
str_repeat(' ', $length - mb_strlen($row)),
$this->style->getItemExtra()
)
: $text;
}

return $text;
}, $rows, array_keys($rows));
}

/**
Expand All @@ -34,6 +88,18 @@ public function getSelectAction() : ?callable
};
}

public function getStyle() : SelectableStyle
{
return $this->style;
}

public function setStyle(SelectableStyle $style) : self
{
$this->style = $style;

return $this;
}

/**
* Return the raw string of text
*/
Expand All @@ -49,7 +115,36 @@ public function setText(string $text) : void
{
$this->text = $text;
}


/**
* Can the item be selected
*/
public function canSelect() : bool
{
return !$this->disabled;
}

public function showsItemExtra() : bool
{
return $this->showItemExtra;
}

/**
* Enable showing item extra
*/
public function showItemExtra() : void
{
$this->showItemExtra = true;
}

/**
* Disable showing item extra
*/
public function hideItemExtra() : void
{
$this->showItemExtra = false;
}

/**
* Returns the sub menu
*/
Expand Down
2 changes: 1 addition & 1 deletion src/MenuItem/SelectableItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* @author Michael Woodward <[email protected]>
*/
class SelectableItem implements MenuItemInterface
class SelectableItem implements MenuItemInterface, SelectableStyleInterface
{
/**
* @var callable
Expand Down
12 changes: 12 additions & 0 deletions src/MenuItem/SelectableStyleInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace PhpSchool\CliMenu\MenuItem;

use PhpSchool\CliMenu\Style\SelectableStyle;

interface SelectableStyleInterface
{
public function getStyle() : SelectableStyle;

public function setStyle(SelectableStyle $style);
}
89 changes: 0 additions & 89 deletions src/MenuItem/SelectableTrait.php

This file was deleted.

Loading