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
63 changes: 29 additions & 34 deletions src/CliMenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class CliMenuBuilder
/**
* @var array
*/
private $style = [];
private $style;

/**
* @var TerminalInterface
Expand All @@ -84,27 +84,9 @@ class CliMenuBuilder
*/
public function __construct(CliMenuBuilder $parent = null)
{
$this->parent = $parent;
$this->terminal = TerminalFactory::fromSystem();
$this->style = $this->getStyleClassDefaults();
$this->style['terminal'] = $this->terminal;
}

/**
* Pull the constructor params into an array with default values
*
* @return array
*/
private function getStyleClassDefaults()
{
$styleClassParameters = (new \ReflectionClass(MenuStyle::class))->getConstructor()->getParameters();

$defaults = [];
foreach ($styleClassParameters as $parameter) {
$defaults[$parameter->getName()] = $parameter->getDefaultValue();
}

return $defaults;
$this->parent = $parent;
$this->terminal = TerminalFactory::fromSystem();
$this->style = MenuStyle::getDefaultStyleValues();
}

/**
Expand Down Expand Up @@ -389,7 +371,6 @@ public function setTitleSeparator($separator)
public function setTerminal(TerminalInterface $terminal)
{
$this->terminal = $terminal;
$this->style['terminal'] = $this->terminal;
return $this;
}

Expand Down Expand Up @@ -436,19 +417,33 @@ private function itemsHaveExtra(array $items)
*/
private function getMenuStyle()
{
$diff = array_udiff_assoc($this->style, $this->getStyleClassDefaults(), function ($current, $default) {
if ($current instanceof TerminalInterface) {
return 0;
}

return $current === $default ? 0 : 1;
});
if (null === $this->parent) {
return $this->buildStyle();
}

if (!$diff && null !== $this->parent) {
return $this->parent->getMenuStyle();
if ($this->style !== MenuStyle::getDefaultStyleValues()) {
return $this->buildStyle();
}

return new MenuStyle(...array_values($this->style));

return $this->parent->getMenuStyle();
}

/**
* @return MenuStyle
*/
private function buildStyle()
{
return (new MenuStyle($this->terminal))
->setFg($this->style['fg'])
->setBg($this->style['bg'])
->setWidth($this->style['width'])
->setPadding($this->style['padding'])
->setMargin($this->style['margin'])
->setSelectedMarker($this->style['selectedMarker'])
->setUnselectedMarker($this->style['unselectedMarker'])
->setItemExtra($this->style['itemExtra'])
->setDisplaysExtra($this->style['displaysExtra'])
->setTitleSeparator($this->style['titleSeparator']);
}

/**
Expand Down
82 changes: 44 additions & 38 deletions src/MenuStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,32 @@ class MenuStyle
*/
private $titleSeparator;

/**
* Default Values
*
* @var array
*/
private static $defaultStyleValues = [
'fg' => 'white',
'bg' => 'blue',
'width' => 100,
'padding' => 2,
'margin' => 2,
'selectedMarker' => '●',
'unselectedMarker' => '○',
'itemExtra' => '✔',
'displaysExtra' => false,
'titleSeparator' => '=',
];

/**
* @return array
*/
public static function getDefaultStyleValues()
{
return static::$defaultStyleValues;
}

/**
* @var array
*/
Expand Down Expand Up @@ -121,45 +147,22 @@ class MenuStyle
/**
* Initialise style
*
* @param string $bg
* @param string $fg
* @param int $width
* @param int $padding
* @param int $margin
* @param string $unselectedMarker
* @param string $selectedMarker
* @param string $itemExtra
* @param bool $displaysExtra
* @param string $titleSeparator
* @param TerminalInterface $terminal
* @throws InvalidInstantiationException
*/
public function __construct(
$bg = 'blue',
$fg = 'white',
$width = 100,
$padding = 2,
$margin = 2,
$unselectedMarker = '○',
$selectedMarker = '●',
$itemExtra = '✔',
$displaysExtra = false,
$titleSeparator = '=',
TerminalInterface $terminal = null
) {
$this->terminal = $terminal ?: TerminalFactory::fromSystem();
$this->bg = $bg;
$this->fg = $fg;
$this->padding = $padding;
$this->margin = $margin;
$this->itemExtra = $itemExtra;
$this->displaysExtra = $displaysExtra;
$this->titleSeparator = $titleSeparator;

$this->setUnselectedMarker($unselectedMarker);
$this->setSelectedMarker($selectedMarker);
$this->setWidth($width);
$this->calculateContentWidth();
*/
public function __construct(TerminalInterface $terminal = null)
{
$this->terminal = $terminal ?: TerminalFactory::fromSystem();

$this->setFg(static::$defaultStyleValues['fg']);
$this->setBg(static::$defaultStyleValues['bg']);
$this->setWidth(static::$defaultStyleValues['width']);
$this->setPadding(static::$defaultStyleValues['padding']);
$this->setMargin(static::$defaultStyleValues['margin']);
$this->setSelectedMarker(static::$defaultStyleValues['selectedMarker']);
$this->setUnselectedMarker(static::$defaultStyleValues['unselectedMarker']);
$this->setItemExtra(static::$defaultStyleValues['itemExtra']);
$this->setDisplaysExtra(static::$defaultStyleValues['displaysExtra']);
$this->setTitleSeparator(static::$defaultStyleValues['titleSeparator']);
}

/**
Expand Down Expand Up @@ -432,10 +435,13 @@ public function getMarker($selected)

/**
* @param string $itemExtra
* @return $this
*/
public function setItemExtra($itemExtra)
{
$this->itemExtra = $itemExtra;

return $this;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/CliMenuBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function testModifyStyles()

$terminal = static::createMock(TerminalInterface::class);
$terminal
->expects($this->once())
->expects($this->any())
->method('getWidth')
->will($this->returnValue(200));

Expand Down
14 changes: 1 addition & 13 deletions test/CliMenuTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,6 @@ private function getTestFile()
*/
private function getStyle(TerminalInterface $terminal)
{
return new MenuStyle(
'blue',
'white',
100,
2,
2,
'○',
'●',
'✔',
false,
'=',
$terminal
);
return new MenuStyle($terminal);
}
}
20 changes: 10 additions & 10 deletions test/res/testReDrawReDrawsImmediately.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@


 
 PHP School FTW 
 ================================== 
 ● Item 1 
 
 
 PHP School FTW 
 ========================================== 
 ● Item 1 
 




 
 PHP School FTW 
 ================================== 
 ● Item 1 
 
 
 PHP School FTW 
 ========================================== 
 ● Item 1 
 


Expand Down
10 changes: 5 additions & 5 deletions test/res/testSimpleOpenClose.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@


 
 PHP School FTW 
 ================================== 
 ● Item 1 
 
 
 PHP School FTW 
 ========================================== 
 ● Item 1 
 


Expand Down