Skip to content

Removes styles from SplitItem #209

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

Closed
wants to merge 12 commits into from
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -817,10 +817,13 @@ You may also change the marker for `\PhpSchool\CliMenu\MenuItem\CheckboxItem`:
<?php

use PhpSchool\CliMenu\Builder\CliMenuBuilder;
use PhpSchool\CliMenu\Style\CheckboxStyle;

$menu = (new CliMenuBuilder)
->setUncheckedMarker('[○] ')
->setCheckedMarker('[●] ')
->modifyCheckboxStyle(function (CheckboxStyle $style) {
$style->setMarkerOff('[○] ')
->setMarkerOn('[●] ');
})
->build();
```

Expand All @@ -830,10 +833,13 @@ and for `\PhpSchool\CliMenu\MenuItem\RadioItem`:
<?php

use PhpSchool\CliMenu\Builder\CliMenuBuilder;
use PhpSchool\CliMenu\Style\RadioStyle;

$menu = (new CliMenuBuilder)
->setUnradioMarker('[ ] ')
->setRadioMarker('[✔] ')
->modifyRadioStyle(function (RadioStyle $style) {
$style->setMarkerOff('[ ] ')
->setMarkerOn('[✔] ');
})
->build();
```

Expand Down
7 changes: 5 additions & 2 deletions examples/checkable-item.php → examples/checkbox-item.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use PhpSchool\CliMenu\CliMenu;
use PhpSchool\CliMenu\Builder\CliMenuBuilder;
use PhpSchool\CliMenu\Style\CheckboxStyle;

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

Expand All @@ -22,8 +23,10 @@
})
->addSubMenu('Interpreted', function (CliMenuBuilder $b) use ($itemCallable) {
$b->setTitle('Interpreted Languages')
->setUncheckedMarker('[○] ')
->setCheckedMarker('[●] ')
->modifyCheckboxStyle(function (CheckboxStyle $style) {
$style->setMarkerOff('[○] ')
->setMarkerOn('[●] ');
})
->addCheckboxItem('PHP', $itemCallable)
->addCheckboxItem('Javascript', $itemCallable)
->addCheckboxItem('Ruby', $itemCallable)
Expand Down
7 changes: 5 additions & 2 deletions examples/radio-item.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use PhpSchool\CliMenu\CliMenu;
use PhpSchool\CliMenu\Builder\CliMenuBuilder;
use PhpSchool\CliMenu\Style\RadioStyle;

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

Expand All @@ -22,8 +23,10 @@
})
->addSubMenu('Interpreted', function (CliMenuBuilder $b) use ($itemCallable) {
$b->setTitle('Interpreted Languages')
->setUnradioMarker('[ ] ')
->setRadioMarker('[✔] ')
->modifyRadioStyle(function (RadioStyle $style) {
$style->setMarkerOff('[ ] ')
->setMarkerOn('[✔] ');
})
->addRadioItem('PHP', $itemCallable)
->addRadioItem('Javascript', $itemCallable)
->addRadioItem('Ruby', $itemCallable)
Expand Down
File renamed without changes.
104 changes: 63 additions & 41 deletions src/Builder/CliMenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use PhpSchool\CliMenu\MenuItem\SplitItem;
use PhpSchool\CliMenu\MenuItem\StaticItem;
use PhpSchool\CliMenu\MenuStyle;
use PhpSchool\CliMenu\Style\CheckboxStyle;
use PhpSchool\CliMenu\Style\RadioStyle;
use PhpSchool\CliMenu\Terminal\TerminalFactory;
use PhpSchool\Terminal\Terminal;

Expand Down Expand Up @@ -187,12 +189,6 @@ public function addSubMenu(string $text, \Closure $callback) : self

$menu = $builder->build();
$menu->setParent($this->menu);

//we apply the parent theme if nothing was changed
//if no styles were changed in this sub-menu
if (!$menu->getStyle()->hasChangedFromDefaults()) {
$menu->setStyle($this->menu->getStyle());
}

$this->menu->addItem($item = new MenuMenuItem(
$text,
Expand All @@ -210,12 +206,6 @@ public function addSubMenuFromBuilder(string $text, CliMenuBuilder $builder) : s
$menu = $builder->build();
$menu->setParent($this->menu);

//we apply the parent theme if nothing was changed
//if no styles were changed in this sub-menu
if (!$menu->getStyle()->hasChangedFromDefaults()) {
$menu->setStyle($this->menu->getStyle());
}

$this->menu->addItem($item = new MenuMenuItem(
$text,
$menu,
Expand Down Expand Up @@ -302,7 +292,7 @@ public function addSplitItem(\Closure $callback) : self
}

$callback($builder);

$this->menu->addItem($splitItem = $builder->build());

$this->processSplitItemShortcuts($splitItem);
Expand Down Expand Up @@ -417,34 +407,6 @@ public function setSelectedMarker(string $marker) : self
return $this;
}

public function setUncheckedMarker(string $marker) : self
{
$this->style->setUncheckedMarker($marker);

return $this;
}

public function setCheckedMarker(string $marker) : self
{
$this->style->setCheckedMarker($marker);

return $this;
}

public function setUnradioMarker(string $marker) : self
{
$this->style->setUnradioMarker($marker);

return $this;
}

public function setRadioMarker(string $marker) : self
{
$this->style->setRadioMarker($marker);

return $this;
}

public function setItemExtra(string $extra) : self
{
$this->style->setItemExtra($extra);
Expand Down Expand Up @@ -556,6 +518,66 @@ public function build() : CliMenu
$this->style->setDisplaysExtra($this->itemsHaveExtra($this->menu->getItems()));
}

if (!$this->subMenu) {
$this->propagateStyles($this->menu);
}

return $this->menu;
}

public function getCheckboxStyle() : CheckboxStyle
{
return $this->menu->getCheckboxStyle();
}

public function setCheckboxStyle(CheckboxStyle $style) : self
{
$this->menu->setCheckboxStyle($style);

return $this;
}

public function modifyCheckboxStyle(callable $itemCallable) : self
{
$itemCallable($this->menu->getCheckboxStyle());

return $this;
}

public function getRadioStyle() : RadioStyle
{
return $this->menu->getRadioStyle();
}

public function setRadioStyle(RadioStyle $style) : self
{
$this->menu->setRadioStyle($style);

return $this;
}

public function modifyRadioStyle(callable $itemCallable) : self
{
$itemCallable($this->menu->getRadioStyle());

return $this;
}

/**
* Pass styles from current menu to sub-menu
* only if sub-menu style has not be customized
*/
private function propagateStyles(CliMenu $menu)
{
foreach ($menu->getItems() as $item) {
if ($item instanceof MenuMenuItem) {
$subMenu = $item->getSubMenu();

!$subMenu->getStyle()->hasChangedFromDefaults()
&& $subMenu->setStyle(clone $menu->getStyle());

$this->propagateStyles($subMenu);
}
}
}
}
24 changes: 19 additions & 5 deletions src/Builder/SplitItemBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ public function addCheckboxItem(
bool $showItemExtra = false,
bool $disabled = false
) : self {
$this->splitItem->addItem(new CheckboxItem($text, $itemCallable, $showItemExtra, $disabled));
$item = (new CheckboxItem($text, $itemCallable, $showItemExtra, $disabled))
->setStyle($this->menu->getCheckboxStyle());

$this->splitItem->addItem($item);

return $this;
}
Expand All @@ -76,7 +79,10 @@ public function addRadioItem(
bool $showItemExtra = false,
bool $disabled = false
) : self {
$this->splitItem->addItem(new RadioItem($text, $itemCallable, $showItemExtra, $disabled));
$item = (new RadioItem($text, $itemCallable, $showItemExtra, $disabled))
->setStyle($this->menu->getRadioStyle());

$this->splitItem->addItem($item);

return $this;
}
Expand Down Expand Up @@ -108,19 +114,27 @@ public function addSubMenu(string $text, \Closure $callback) : self
$menu = $builder->build();
$menu->setParent($this->menu);

if (!$menu->getCheckboxStyle()->hasChangedFromDefaults()) {
$menu->setCheckboxStyle(clone $this->menu->getCheckboxStyle());
}

if (!$menu->getRadioStyle()->hasChangedFromDefaults()) {
$menu->setRadioStyle(clone $this->menu->getRadioStyle());
}

$this->splitItem->addItem(new MenuMenuItem(
$text,
$menu,
$builder->isMenuDisabled()
));

return $this;
}

public function setGutter(int $gutter) : self
{
$this->splitItem->setGutter($gutter);

return $this;
}

Expand All @@ -134,7 +148,7 @@ public function enableAutoShortcuts(string $regex = null) : self

return $this;
}

public function build() : SplitItem
{
return $this->splitItem;
Expand Down
46 changes: 42 additions & 4 deletions src/CliMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use PhpSchool\CliMenu\MenuItem\StaticItem;
use PhpSchool\CliMenu\Dialogue\Confirm;
use PhpSchool\CliMenu\Dialogue\Flash;
use PhpSchool\CliMenu\Style\CheckboxStyle;
use PhpSchool\CliMenu\Style\RadioStyle;
use PhpSchool\CliMenu\Terminal\TerminalFactory;
use PhpSchool\CliMenu\Util\StringUtil as s;
use PhpSchool\Terminal\InputCharacter;
Expand All @@ -35,6 +37,16 @@ class CliMenu
*/
protected $style;

/**
* @var CheckboxStyle
*/
private $checkboxStyle;

/**
* @var RadioStyle
*/
private $radioStyle;

/**
* @var ?string
*/
Expand Down Expand Up @@ -90,10 +102,12 @@ public function __construct(
Terminal $terminal = null,
MenuStyle $style = null
) {
$this->title = $title;
$this->items = $items;
$this->terminal = $terminal ?: TerminalFactory::fromSystem();
$this->style = $style ?: new MenuStyle($this->terminal);
$this->title = $title;
$this->items = $items;
$this->terminal = $terminal ?: TerminalFactory::fromSystem();
$this->style = $style ?: new MenuStyle($this->terminal);
$this->checkboxStyle = new CheckboxStyle();
$this->radioStyle = new RadioStyle();

$this->selectFirstItem();
}
Expand Down Expand Up @@ -640,6 +654,30 @@ public function setStyle(MenuStyle $style) : void
$this->style = $style;
}

public function getCheckboxStyle() : CheckboxStyle
{
return $this->checkboxStyle;
}

public function setCheckboxStyle(CheckboxStyle $style) : self
{
$this->checkboxStyle = $style;

return $this;
}

public function getRadioStyle() : RadioStyle
{
return $this->radioStyle;
}

public function setRadioStyle(RadioStyle $style) : self
{
$this->radioStyle = $style;

return $this;
}

public function getCurrentFrame() : Frame
{
return $this->currentFrame;
Expand Down
Loading