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 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
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
21 changes: 4 additions & 17 deletions src/Builder/CliMenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,22 +394,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 +592,10 @@ private function propagateStyles(CliMenu $menu, array $items = [])
$item->setStyle(clone $menu->getRadioStyle());
}

if ($item instanceof SelectableItem
if (($item instanceof MenuMenuItem
|| $item instanceof SelectableItem
|| $item instanceof StaticItem
)
&& !$item->getStyle()->hasChangedFromDefaults()
) {
$item->setStyle(clone $menu->getSelectableStyle());
Expand Down
105 changes: 100 additions & 5 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
{
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
89 changes: 0 additions & 89 deletions src/MenuItem/SelectableTrait.php

This file was deleted.

53 changes: 38 additions & 15 deletions src/MenuItem/SplitItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,35 +117,35 @@ public function getRows(MenuStyle $style, bool $selected = false) : array
$this->setDefaultSelectedItem();
}

$length = $style->getDisplaysExtra()
? floor($style->getContentWidth() / $numberOfItems) - (mb_strlen($style->getItemExtra()) + 2)
$largestItemExtra = $this->calculateItemExtra();

$length = $largestItemExtra > 0
? floor($style->getContentWidth() / $numberOfItems) - ($largestItemExtra + 2)
: floor($style->getContentWidth() / $numberOfItems);

$length -= $this->gutter;
$length = (int) $length;

$missingLength = $style->getContentWidth() % $numberOfItems;

return $this->buildRows(
array_map(function ($index, $item) use ($selected, $length, $style) {
$isSelected = $selected && $index === $this->selectedItemIndex;

if ($item instanceof CheckboxItem || $item instanceof RadioItem) {
$markerType = $item->getStyle()->getMarker($item->getChecked());
$displaysExtra = $item->getStyle()->getDisplaysExtra();
$itemExtraVal = $item->getStyle()->getItemExtra();
$markerType = $item->getStyle()->getMarker($item->getChecked());
} else {
$markerType = $style->getMarker($isSelected);
$displaysExtra = $style->getDisplaysExtra();
$itemExtraVal = $style->getItemExtra();
/** @var MenuMenuItem|SelectableItem|StaticItem $item */
$markerType = $item->getStyle()->getMarker($isSelected);
}

$marker = $item->canSelect()
? sprintf('%s', $markerType)
: '';

$itemExtra = '';
if ($displaysExtra) {
if ($item->getStyle()->getDisplaysExtra()) {
$itemExtraVal = $item->getStyle()->getItemExtra();
$itemExtra = $item->showsItemExtra()
? sprintf(' %s', $itemExtraVal)
: sprintf(' %s', str_repeat(' ', mb_strlen($itemExtraVal)));
Expand All @@ -166,15 +166,15 @@ public function getRows(MenuStyle $style, bool $selected = false) : array
$itemExtra
);
}, array_keys($this->items), $this->items),
$style,
$missingLength,
$length
$length,
$largestItemExtra
);
}

private function buildRows(array $cells, MenuStyle $style, int $missingLength, int $length) : array
private function buildRows(array $cells, int $missingLength, int $length, int $largestItemExtra) : array
{
$extraPadLength = $style->getDisplaysExtra() ? 2 + mb_strlen($style->getItemExtra()) : 0;
$extraPadLength = $largestItemExtra > 0 ? 2 + $largestItemExtra : 0;

return array_map(
function ($i) use ($cells, $length, $missingLength, $extraPadLength) {
Expand Down Expand Up @@ -325,4 +325,27 @@ public function getText() : string
{
throw new \BadMethodCallException(sprintf('Not supported on: %s', __CLASS__));
}

/**
* Finds largest itemExtra value in items
*/
private function calculateItemExtra() : int
{
$largestItemExtra = 0;

/** @var CheckboxItem|RadioItem|MenuMenuItem|SelectableItem|StaticItem $item */
foreach ($this->items as $item) {
if (!$item->getStyle()->getDisplaysExtra()) {
continue;
}

if (mb_strlen($item->getStyle()->getItemExtra()) < $largestItemExtra) {
continue;
}

$largestItemExtra = mb_strlen($item->getStyle()->getItemExtra());
}

return $largestItemExtra;
}
}
Loading