Skip to content

Selectable item renderer #231

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 1 commit into from
Feb 15, 2020
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
30 changes: 1 addition & 29 deletions src/MenuItem/CheckboxItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use PhpSchool\CliMenu\CliMenu;
use PhpSchool\CliMenu\MenuStyle;
use PhpSchool\CliMenu\Util\StringUtil;
use PhpSchool\CliMenu\Style\CheckboxStyle;
use PhpSchool\CliMenu\Style\ItemStyle;

Expand Down Expand Up @@ -61,34 +60,7 @@ public function __construct(
*/
public function getRows(MenuStyle $style, bool $selected = false) : array
{
$marker = sprintf("%s", $this->style->getMarker($this, $selected));

$itemExtra = $this->style->getItemExtra();

$length = $this->style->getDisplaysExtra()
? $style->getContentWidth() - (mb_strlen($itemExtra) + 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, $itemExtra) {
$text = $this->disabled ? $style->getDisabledItemText($row) : $row;

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

return $text;
}, $rows, array_keys($rows));
return (new SelectableItemRenderer())->render($style, $this, $selected, $this->disabled);
}

/**
Expand Down
34 changes: 1 addition & 33 deletions src/MenuItem/MenuMenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

use PhpSchool\CliMenu\CliMenu;
use PhpSchool\CliMenu\MenuStyle;
use PhpSchool\CliMenu\Util\StringUtil;
use PhpSchool\CliMenu\Style\ItemStyle;
use PhpSchool\CliMenu\Style\SelectableStyle;
use function PhpSchool\CliMenu\Util\mapWithKeys;

/**
* @author Michael Woodward <[email protected]>
Expand Down Expand Up @@ -56,37 +54,7 @@ public function __construct(
*/
public function getRows(MenuStyle $style, bool $selected = false) : array
{
$marker = sprintf("%s", $this->style->getMarker($this, $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 mapWithKeys($rows, function (int $key, string $row) 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;
});
return (new SelectableItemRenderer())->render($style, $this, $selected, $this->disabled);
}

/**
Expand Down
30 changes: 1 addition & 29 deletions src/MenuItem/RadioItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use PhpSchool\CliMenu\CliMenu;
use PhpSchool\CliMenu\MenuStyle;
use PhpSchool\CliMenu\Util\StringUtil;
use PhpSchool\CliMenu\Style\ItemStyle;
use PhpSchool\CliMenu\Style\RadioStyle;

Expand Down Expand Up @@ -61,34 +60,7 @@ public function __construct(
*/
public function getRows(MenuStyle $style, bool $selected = false) : array
{
$marker = sprintf("%s", $this->style->getMarker($this, $selected));

$itemExtra = $this->style->getItemExtra();

$length = $this->style->getDisplaysExtra()
? $style->getContentWidth() - (mb_strlen($itemExtra) + 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, $itemExtra) {
$text = $this->disabled ? $style->getDisabledItemText($row) : $row;

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

return $text;
}, $rows, array_keys($rows));
return (new SelectableItemRenderer())->render($style, $this, $selected, $this->disabled);
}

/**
Expand Down
66 changes: 66 additions & 0 deletions src/MenuItem/SelectableItemRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace PhpSchool\CliMenu\MenuItem;

use PhpSchool\CliMenu\MenuStyle;
use PhpSchool\CliMenu\Style\ItemStyle;
use PhpSchool\CliMenu\Style\Selectable;
use PhpSchool\CliMenu\Util\StringUtil as s;
use function PhpSchool\CliMenu\Util\mapWithKeys;

class SelectableItemRenderer
{
public function render(MenuStyle $menuStyle, MenuItemInterface $item, bool $selected, bool $disabled) : array
{
$itemStyle = $item->getStyle();
$marker = $itemStyle->getMarker($item, $selected);
$availableTextWidth = $this->getAvailableTextWidth($menuStyle, $itemStyle);

return mapWithKeys(
$this->wrapAndIndentText($marker, $item->getText(), $availableTextWidth),
function (int $key, string $row) use ($menuStyle, $itemStyle, $availableTextWidth, $disabled) {
$text = $disabled ? $menuStyle->getDisabledItemText($row) : $row;

return $key === 0 && $itemStyle->getDisplaysExtra()
? $this->lineWithExtra($text, $availableTextWidth, $itemStyle)
: $text;
}
);
}

public function wrapAndIndentText(string $marker, string $text, int $availableWidth) : array
{
return explode(
"\n",
s::wordwrap(
"{$marker}{$text}",
$availableWidth,
sprintf("\n%s", $this->emptyString(mb_strlen($marker)))
)
);
}

public function lineWithExtra(string $text, int $availableWidth, ItemStyle $itemStyle) : string
{
return sprintf(
'%s%s %s',
$text,
$this->emptyString($availableWidth - s::length($text)),
$itemStyle->getItemExtra()
);
}

public function emptyString(int $numCharacters) : string
{
return str_repeat(' ', $numCharacters);
}

public function getAvailableTextWidth(MenuStyle $menuStyle, ItemStyle $itemStyle) : int
{
return $itemStyle->getDisplaysExtra()
? $menuStyle->getContentWidth() - (mb_strlen($itemStyle->getItemExtra()) + 2)
: $menuStyle->getContentWidth();
}
}
151 changes: 151 additions & 0 deletions test/MenuItem/SelectableItemRendererTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php

declare(strict_types=1);

namespace PhpSchool\CliMenuTest\MenuItem;

use PhpSchool\CliMenu\MenuItem\SelectableItem;
use PhpSchool\CliMenu\MenuItem\SelectableItemRenderer;
use PhpSchool\CliMenu\MenuStyle;
use PhpSchool\CliMenu\Style\SelectableStyle;
use PhpSchool\CliMenuTest\MockTerminal;
use PHPUnit\Framework\TestCase;

class SelectableItemRendererTest extends TestCase
{
public function testRender() : void
{
$renderer = new SelectableItemRenderer();

$menuStyle = new MenuStyle(new MockTerminal);
$menuStyle->setWidth(35);

$style = (new SelectableStyle())->setItemExtra('[DONE]');

$item = new SelectableItem('SOME TEXT', function () {
});
$item->setStyle($style);

self::assertEquals(
[
'● SOME TEXT [DONE]',
],
$renderer->render($menuStyle, $item, true, false)
);
}
public function testRenderMultiLine() : void
{
$renderer = new SelectableItemRenderer();

$menuStyle = new MenuStyle(new MockTerminal);
$menuStyle->setWidth(35);
$style = (new SelectableStyle())->setItemExtra('[DONE]');

$item = new SelectableItem('SOME TEXT THAT IS MUCH LONGER THAN THE AVAILABLE WIDTH', function () {
});
$item->setStyle($style);

self::assertEquals(
[
'● SOME TEXT THAT IS [DONE]',
' MUCH LONGER THAN THE',
' AVAILABLE WIDTH',
],
$renderer->render($menuStyle, $item, true, false)
);
}
public function testRenderUnselected() : void
{
$renderer = new SelectableItemRenderer();

$menuStyle = new MenuStyle(new MockTerminal);
$menuStyle->setWidth(35);
$style = (new SelectableStyle())->setItemExtra('[DONE]');

$item = new SelectableItem('SOME TEXT', function () {
});
$item->setStyle($style);

self::assertEquals(
[
'○ SOME TEXT [DONE]',
],
$renderer->render($menuStyle, $item, false, false)
);
}
public function testRenderDisabled() : void
{
$renderer = new SelectableItemRenderer();

$menuStyle = new MenuStyle(new MockTerminal);
$menuStyle->setWidth(35);
$style = (new SelectableStyle())->setItemExtra('[DONE]');

$item = new SelectableItem('SOME TEXT', function () {
});
$item->setStyle($style);

self::assertEquals(
[
"\033[2m● SOME TEXT\033[22m [DONE]",
],
$renderer->render($menuStyle, $item, true, true)
);
}
public function testWrapAndIndentText() : void
{
$renderer = new SelectableItemRenderer();

$text = 'SOME TEXT THAT IS MUCH LONGER THAN THE AVAILABLE WIDTH';

self::assertEquals(
[
'[ ] SOME TEXT THAT',
' IS MUCH LONGER THAN',
' THE AVAILABLE WIDTH',
],
$renderer->wrapAndIndentText('[ ] ', $text, 20)
);
}
public function testLineWithExtra() : void
{
$renderer = new SelectableItemRenderer();
$style = (new SelectableStyle())->setItemExtra('[DONE]');

self::assertEquals(
'FIRST LINE [DONE]',
$renderer->lineWithExtra('FIRST LINE', 15, $style)
);
}
public function testEmptyString() : void
{
$renderer = new SelectableItemRenderer();

self::assertEquals(' ', $renderer->emptyString(1));
self::assertEquals(' ', $renderer->emptyString(3));
self::assertEquals(' ', $renderer->emptyString(5));
}
public function testGetAvailableTextWidthWithoutExtra() : void
{
$renderer = new SelectableItemRenderer();

$menuStyle = new MenuStyle(new MockTerminal);
$menuStyle->setWidth(100);

$itemStyle = new SelectableStyle();

self::assertEquals(92, $renderer->getAvailableTextWidth($menuStyle, $itemStyle));
}
public function testGetAvailableTextWidthWithExtra() : void
{
$renderer = new SelectableItemRenderer();

$menuStyle = new MenuStyle(new MockTerminal);
$menuStyle->setWidth(100);

$itemStyle = new SelectableStyle();
$itemStyle->setItemExtra('[DONE]');

self::assertEquals(84, $renderer->getAvailableTextWidth($menuStyle, $itemStyle));
}
}