Skip to content

Split padding in top/bottom and left/right #121

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 7 commits into from
May 10, 2018
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
22 changes: 14 additions & 8 deletions src/CliMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function isOpen() : bool
public function addItem(MenuItemInterface $item) : void
{
$this->items[] = $item;

if (count($this->items) === 1) {
$this->selectFirstItem();
}
Expand Down Expand Up @@ -318,7 +318,7 @@ public function redraw(bool $clear = false) : void
if ($clear) {
$this->terminal->clear();
}

$this->assertOpen();
$this->draw();
}
Expand All @@ -343,8 +343,11 @@ protected function draw() : void
$frame->addRows($this->style->getBorderTopRows());
}

if ($this->style->getPaddingTopBottom() > 0) {
$frame->addRows($this->style->getPaddingTopBottomRows());
}

if ($this->title) {
$frame->addRows($this->drawMenuItem(new LineBreakItem()));
$frame->addRows($this->drawMenuItem(new StaticItem($this->title)));
$frame->addRows($this->drawMenuItem(new LineBreakItem($this->style->getTitleSeparator())));
}
Expand All @@ -353,14 +356,17 @@ protected function draw() : void
$frame->addRows($this->drawMenuItem($item, $index === $this->selectedItem));
}, $this->items, array_keys($this->items));

$frame->addRows($this->drawMenuItem(new LineBreakItem()));


if ($this->style->getPaddingTopBottom() > 0) {
$frame->addRows($this->style->getPaddingTopBottomRows());
}

if ($this->style->getBorderBottomWidth() > 0) {
$frame->addRows($this->style->getBorderBottomRows());
}

$frame->newLine(2);

$this->terminal->moveCursorToTop();
foreach ($frame->getRows() as $row) {
if ($row == "\n") {
Expand Down Expand Up @@ -401,7 +407,7 @@ protected function drawMenuItem(MenuItemInterface $item, bool $selected = false)
str_repeat(' ', $this->style->getBorderLeftWidth()),
$this->style->getColoursSetCode(),
$invertedColoursSetCode,
str_repeat(' ', $this->style->getPadding()),
str_repeat(' ', $this->style->getPaddingLeftRight()),
$row,
str_repeat(' ', $this->style->getRightHandPadding(mb_strlen(s::stripAnsiEscapeSequence($row)))),
$invertedColoursUnsetCode,
Expand Down Expand Up @@ -439,7 +445,7 @@ public function close() : void
$menu->closeThis();
$menu = $menu->getParent();
} while (null !== $menu);

$this->tearDownTerminal();
}

Expand Down
51 changes: 34 additions & 17 deletions src/CliMenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use PhpSchool\CliMenu\MenuItem\StaticItem;
use PhpSchool\CliMenu\Terminal\TerminalFactory;
use PhpSchool\CliMenu\Util\ColourUtil;
use Assert\Assertion;
use PhpSchool\Terminal\Terminal;
use RuntimeException;

Expand All @@ -31,7 +30,7 @@ class CliMenuBuilder
* @var null|self
*/
private $parent;

/**
* @var self[]
*/
Expand All @@ -46,7 +45,7 @@ class CliMenuBuilder
* @var string
*/
private $goBackButtonText = 'Go Back';

/**
* @var string
*/
Expand Down Expand Up @@ -121,7 +120,7 @@ public function addItems(array $items) : self
foreach ($items as $item) {
$this->addItem(...$item);
}

return $this;
}

Expand Down Expand Up @@ -152,12 +151,12 @@ public function addAsciiArt(string $art, string $position = AsciiArtItem::POSITI
public function addSubMenu(string $id, CliMenuBuilder $subMenuBuilder = null) : CliMenuBuilder
{
$this->menuItems[] = $id;

if (null === $subMenuBuilder) {
$this->subMenuBuilders[$id] = new static($this);
return $this->subMenuBuilders[$id];
}

$this->subMenuBuilders[$id] = $subMenuBuilder;
return $this;
}
Expand Down Expand Up @@ -188,14 +187,14 @@ public function isMenuDisabled() : bool
public function setGoBackButtonText(string $goBackButtonTest) : self
{
$this->goBackButtonText = $goBackButtonTest;

return $this;
}

public function setExitButtonText(string $exitButtonText) : self
{
$this->exitButtonText = $exitButtonText;

return $this;
}

Expand Down Expand Up @@ -228,24 +227,41 @@ public function setWidth(int $width) : self
return $this;
}

public function setPadding(int $padding) : self
public function setPadding(int $topBottom, int $leftRight = null) : self
{
$this->style['padding'] = $padding;
if ($leftRight === null) {
$leftRight = $topBottom;
}

$this->setPaddingTopBottom($topBottom);
$this->setPaddingLeftRight($leftRight);

return $this;
}

public function setPaddingTopBottom(int $topBottom) : self
{
$this->style['paddingTopBottom'] = $topBottom;

return $this;
}

public function setPaddingLeftRight(int $leftRight) : self
{
$this->style['paddingLeftRight'] = $leftRight;

return $this;
}

public function setMarginAuto() : self
{
$this->style['marginAuto'] = true;

return $this;
}

public function setMargin(int $margin) : self
{
Assertion::greaterOrEqualThan($margin, 0);

$this->style['marginAuto'] = false;
$this->style['margin'] = $margin;

Expand Down Expand Up @@ -330,7 +346,7 @@ private function getDefaultItems() : array
if ($this->parent) {
$actions[] = new SelectableItem($this->goBackButtonText, new GoBackAction);
}

$actions[] = new SelectableItem($this->exitButtonText, new ExitAction);
return $actions;
}
Expand Down Expand Up @@ -372,7 +388,8 @@ private function buildStyle() : MenuStyle
->setFg($this->style['fg'])
->setBg($this->style['bg'])
->setWidth($this->style['width'])
->setPadding($this->style['padding'])
->setPaddingTopBottom($this->style['paddingTopBottom'])
->setPaddingLeftRight($this->style['paddingLeftRight'])
->setSelectedMarker($this->style['selectedMarker'])
->setUnselectedMarker($this->style['unselectedMarker'])
->setItemExtra($this->style['itemExtra'])
Expand All @@ -385,7 +402,7 @@ private function buildStyle() : MenuStyle
->setBorderColour($this->style['borderColour']);

$this->style['marginAuto'] ? $style->setMarginAuto() : $style->setMargin($this->style['margin']);

return $style;
}

Expand Down Expand Up @@ -447,7 +464,7 @@ public function build() : CliMenu
$this->terminal,
$this->getMenuStyle()
);

foreach ($this->subMenus as $subMenu) {
$subMenu->setParent($menu);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Dialogue/Confirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public function display(string $confirmText = 'OK') : void
$this->write(sprintf(
"%s%s%s%s%s\n",
$this->style->getColoursSetCode(),
str_repeat(' ', $this->style->getPadding()),
str_repeat(' ', $this->style->getPaddingLeftRight()),
$this->text,
str_repeat(' ', $this->style->getPadding()),
str_repeat(' ', $this->style->getPaddingLeftRight()),
$this->style->getColoursResetCode()
));

Expand All @@ -52,9 +52,9 @@ public function display(string $confirmText = 'OK') : void
$this->write(sprintf(
"%s%s%s%s%s\n",
$this->style->getColoursSetCode(),
str_repeat(' ', $this->style->getPadding()),
str_repeat(' ', $this->style->getPaddingLeftRight()),
str_repeat(' ', mb_strlen($this->text)),
str_repeat(' ', $this->style->getPadding()),
str_repeat(' ', $this->style->getPaddingLeftRight()),
$this->style->getColoursResetCode()
));

Expand Down
6 changes: 3 additions & 3 deletions src/Dialogue/Dialogue.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected function calculateCoordinates() : void

//x
$parentStyle = $this->parentMenu->getStyle();
$dialogueHalfLength = (mb_strlen($this->text) + ($this->style->getPadding() * 2)) / 2;
$dialogueHalfLength = (mb_strlen($this->text) + ($this->style->getPaddingLeftRight() * 2)) / 2;
$widthHalfLength = ceil($parentStyle->getWidth() / 2 + $parentStyle->getMargin());
$this->x = $widthHalfLength - $dialogueHalfLength;
}
Expand All @@ -87,9 +87,9 @@ protected function emptyRow() : void
sprintf(
"%s%s%s%s%s\n",
$this->style->getColoursSetCode(),
str_repeat(' ', $this->style->getPadding()),
str_repeat(' ', $this->style->getPaddingLeftRight()),
str_repeat(' ', mb_strlen($this->text)),
str_repeat(' ', $this->style->getPadding()),
str_repeat(' ', $this->style->getPaddingLeftRight()),
$this->style->getColoursResetCode()
)
);
Expand Down
4 changes: 2 additions & 2 deletions src/Dialogue/Flash.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public function display() : void
$this->write(sprintf(
"%s%s%s%s%s\n",
$this->style->getColoursSetCode(),
str_repeat(' ', $this->style->getPadding()),
str_repeat(' ', $this->style->getPaddingLeftRight()),
$this->text,
str_repeat(' ', $this->style->getPadding()),
str_repeat(' ', $this->style->getPaddingLeftRight()),
$this->style->getColoursResetCode()
));

Expand Down
6 changes: 3 additions & 3 deletions src/Input/InputIO.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private function calculateXPosition(Input $input, string $userInput) : int
);

$parentStyle = $this->parentMenu->getStyle();
$halfWidth = ($width + ($input->getStyle()->getPadding() * 2)) / 2;
$halfWidth = ($width + ($input->getStyle()->getPaddingLeftRight() * 2)) / 2;
$parentHalfWidth = ceil($parentStyle->getWidth() / 2 + $parentStyle->getMargin());

return $parentHalfWidth - $halfWidth;
Expand All @@ -144,9 +144,9 @@ private function drawLine(Input $input, string $userInput, string $text) : void
$line = sprintf(
"%s%s%s%s%s\n",
$input->getStyle()->getColoursSetCode(),
str_repeat(' ', $input->getStyle()->getPadding()),
str_repeat(' ', $input->getStyle()->getPaddingLeftRight()),
$text,
str_repeat(' ', $input->getStyle()->getPadding()),
str_repeat(' ', $input->getStyle()->getPaddingLeftRight()),
$input->getStyle()->getColoursResetCode()
);

Expand Down
6 changes: 3 additions & 3 deletions src/MenuItem/AsciiArtItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class AsciiArtItem implements MenuItemInterface
const POSITION_CENTER = 'center';
const POSITION_LEFT = 'left';
const POSITION_RIGHT = 'right';

/**
* @var string
*/
Expand All @@ -40,7 +40,7 @@ class AsciiArtItem implements MenuItemInterface
public function __construct(string $text, string $position = self::POSITION_CENTER, string $alt = '')
{
Assertion::inArray($position, [self::POSITION_CENTER, self::POSITION_RIGHT, self::POSITION_LEFT]);

$this->text = implode("\n", array_map(function (string $line) {
return rtrim($line, ' ');
}, explode("\n", $text)));
Expand All @@ -60,7 +60,7 @@ public function getRows(MenuStyle $style, bool $selected = false) : array
}

$padding = $style->getContentWidth() - $this->artLength;

return array_map(function ($row) use ($padding) {
switch ($this->position) {
case self::POSITION_LEFT:
Expand Down
Loading