Skip to content

Commit 97058e0

Browse files
authored
Merge pull request #121 from Lynesth/patch-21
Split padding in top/bottom and left/right
2 parents 1a3e425 + bbe785a commit 97058e0

File tree

10 files changed

+184
-63
lines changed

10 files changed

+184
-63
lines changed

src/CliMenu.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public function isOpen() : bool
157157
public function addItem(MenuItemInterface $item) : void
158158
{
159159
$this->items[] = $item;
160-
160+
161161
if (count($this->items) === 1) {
162162
$this->selectFirstItem();
163163
}
@@ -318,7 +318,7 @@ public function redraw(bool $clear = false) : void
318318
if ($clear) {
319319
$this->terminal->clear();
320320
}
321-
321+
322322
$this->assertOpen();
323323
$this->draw();
324324
}
@@ -343,8 +343,11 @@ protected function draw() : void
343343
$frame->addRows($this->style->getBorderTopRows());
344344
}
345345

346+
if ($this->style->getPaddingTopBottom() > 0) {
347+
$frame->addRows($this->style->getPaddingTopBottomRows());
348+
}
349+
346350
if ($this->title) {
347-
$frame->addRows($this->drawMenuItem(new LineBreakItem()));
348351
$frame->addRows($this->drawMenuItem(new StaticItem($this->title)));
349352
$frame->addRows($this->drawMenuItem(new LineBreakItem($this->style->getTitleSeparator())));
350353
}
@@ -353,14 +356,17 @@ protected function draw() : void
353356
$frame->addRows($this->drawMenuItem($item, $index === $this->selectedItem));
354357
}, $this->items, array_keys($this->items));
355358

356-
$frame->addRows($this->drawMenuItem(new LineBreakItem()));
357-
359+
360+
if ($this->style->getPaddingTopBottom() > 0) {
361+
$frame->addRows($this->style->getPaddingTopBottomRows());
362+
}
363+
358364
if ($this->style->getBorderBottomWidth() > 0) {
359365
$frame->addRows($this->style->getBorderBottomRows());
360366
}
361367

362368
$frame->newLine(2);
363-
369+
364370
$this->terminal->moveCursorToTop();
365371
foreach ($frame->getRows() as $row) {
366372
if ($row == "\n") {
@@ -401,7 +407,7 @@ protected function drawMenuItem(MenuItemInterface $item, bool $selected = false)
401407
str_repeat(' ', $this->style->getBorderLeftWidth()),
402408
$this->style->getColoursSetCode(),
403409
$invertedColoursSetCode,
404-
str_repeat(' ', $this->style->getPadding()),
410+
str_repeat(' ', $this->style->getPaddingLeftRight()),
405411
$row,
406412
str_repeat(' ', $this->style->getRightHandPadding(mb_strlen(s::stripAnsiEscapeSequence($row)))),
407413
$invertedColoursUnsetCode,
@@ -439,7 +445,7 @@ public function close() : void
439445
$menu->closeThis();
440446
$menu = $menu->getParent();
441447
} while (null !== $menu);
442-
448+
443449
$this->tearDownTerminal();
444450
}
445451

src/CliMenuBuilder.php

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use PhpSchool\CliMenu\MenuItem\StaticItem;
1313
use PhpSchool\CliMenu\Terminal\TerminalFactory;
1414
use PhpSchool\CliMenu\Util\ColourUtil;
15-
use Assert\Assertion;
1615
use PhpSchool\Terminal\Terminal;
1716
use RuntimeException;
1817

@@ -31,7 +30,7 @@ class CliMenuBuilder
3130
* @var null|self
3231
*/
3332
private $parent;
34-
33+
3534
/**
3635
* @var self[]
3736
*/
@@ -46,7 +45,7 @@ class CliMenuBuilder
4645
* @var string
4746
*/
4847
private $goBackButtonText = 'Go Back';
49-
48+
5049
/**
5150
* @var string
5251
*/
@@ -121,7 +120,7 @@ public function addItems(array $items) : self
121120
foreach ($items as $item) {
122121
$this->addItem(...$item);
123122
}
124-
123+
125124
return $this;
126125
}
127126

@@ -152,12 +151,12 @@ public function addAsciiArt(string $art, string $position = AsciiArtItem::POSITI
152151
public function addSubMenu(string $id, CliMenuBuilder $subMenuBuilder = null) : CliMenuBuilder
153152
{
154153
$this->menuItems[] = $id;
155-
154+
156155
if (null === $subMenuBuilder) {
157156
$this->subMenuBuilders[$id] = new static($this);
158157
return $this->subMenuBuilders[$id];
159158
}
160-
159+
161160
$this->subMenuBuilders[$id] = $subMenuBuilder;
162161
return $this;
163162
}
@@ -188,14 +187,14 @@ public function isMenuDisabled() : bool
188187
public function setGoBackButtonText(string $goBackButtonTest) : self
189188
{
190189
$this->goBackButtonText = $goBackButtonTest;
191-
190+
192191
return $this;
193192
}
194193

195194
public function setExitButtonText(string $exitButtonText) : self
196195
{
197196
$this->exitButtonText = $exitButtonText;
198-
197+
199198
return $this;
200199
}
201200

@@ -228,24 +227,41 @@ public function setWidth(int $width) : self
228227
return $this;
229228
}
230229

231-
public function setPadding(int $padding) : self
230+
public function setPadding(int $topBottom, int $leftRight = null) : self
232231
{
233-
$this->style['padding'] = $padding;
232+
if ($leftRight === null) {
233+
$leftRight = $topBottom;
234+
}
235+
236+
$this->setPaddingTopBottom($topBottom);
237+
$this->setPaddingLeftRight($leftRight);
238+
239+
return $this;
240+
}
241+
242+
public function setPaddingTopBottom(int $topBottom) : self
243+
{
244+
$this->style['paddingTopBottom'] = $topBottom;
245+
246+
return $this;
247+
}
248+
249+
public function setPaddingLeftRight(int $leftRight) : self
250+
{
251+
$this->style['paddingLeftRight'] = $leftRight;
234252

235253
return $this;
236254
}
237255

238256
public function setMarginAuto() : self
239257
{
240258
$this->style['marginAuto'] = true;
241-
259+
242260
return $this;
243261
}
244262

245263
public function setMargin(int $margin) : self
246264
{
247-
Assertion::greaterOrEqualThan($margin, 0);
248-
249265
$this->style['marginAuto'] = false;
250266
$this->style['margin'] = $margin;
251267

@@ -330,7 +346,7 @@ private function getDefaultItems() : array
330346
if ($this->parent) {
331347
$actions[] = new SelectableItem($this->goBackButtonText, new GoBackAction);
332348
}
333-
349+
334350
$actions[] = new SelectableItem($this->exitButtonText, new ExitAction);
335351
return $actions;
336352
}
@@ -372,7 +388,8 @@ private function buildStyle() : MenuStyle
372388
->setFg($this->style['fg'])
373389
->setBg($this->style['bg'])
374390
->setWidth($this->style['width'])
375-
->setPadding($this->style['padding'])
391+
->setPaddingTopBottom($this->style['paddingTopBottom'])
392+
->setPaddingLeftRight($this->style['paddingLeftRight'])
376393
->setSelectedMarker($this->style['selectedMarker'])
377394
->setUnselectedMarker($this->style['unselectedMarker'])
378395
->setItemExtra($this->style['itemExtra'])
@@ -385,7 +402,7 @@ private function buildStyle() : MenuStyle
385402
->setBorderColour($this->style['borderColour']);
386403

387404
$this->style['marginAuto'] ? $style->setMarginAuto() : $style->setMargin($this->style['margin']);
388-
405+
389406
return $style;
390407
}
391408

@@ -447,7 +464,7 @@ public function build() : CliMenu
447464
$this->terminal,
448465
$this->getMenuStyle()
449466
);
450-
467+
451468
foreach ($this->subMenus as $subMenu) {
452469
$subMenu->setParent($menu);
453470
}

src/Dialogue/Confirm.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ public function display(string $confirmText = 'OK') : void
2727
$this->write(sprintf(
2828
"%s%s%s%s%s\n",
2929
$this->style->getColoursSetCode(),
30-
str_repeat(' ', $this->style->getPadding()),
30+
str_repeat(' ', $this->style->getPaddingLeftRight()),
3131
$this->text,
32-
str_repeat(' ', $this->style->getPadding()),
32+
str_repeat(' ', $this->style->getPaddingLeftRight()),
3333
$this->style->getColoursResetCode()
3434
));
3535

@@ -52,9 +52,9 @@ public function display(string $confirmText = 'OK') : void
5252
$this->write(sprintf(
5353
"%s%s%s%s%s\n",
5454
$this->style->getColoursSetCode(),
55-
str_repeat(' ', $this->style->getPadding()),
55+
str_repeat(' ', $this->style->getPaddingLeftRight()),
5656
str_repeat(' ', mb_strlen($this->text)),
57-
str_repeat(' ', $this->style->getPadding()),
57+
str_repeat(' ', $this->style->getPaddingLeftRight()),
5858
$this->style->getColoursResetCode()
5959
));
6060

src/Dialogue/Dialogue.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ protected function calculateCoordinates() : void
7373

7474
//x
7575
$parentStyle = $this->parentMenu->getStyle();
76-
$dialogueHalfLength = (mb_strlen($this->text) + ($this->style->getPadding() * 2)) / 2;
76+
$dialogueHalfLength = (mb_strlen($this->text) + ($this->style->getPaddingLeftRight() * 2)) / 2;
7777
$widthHalfLength = ceil($parentStyle->getWidth() / 2 + $parentStyle->getMargin());
7878
$this->x = $widthHalfLength - $dialogueHalfLength;
7979
}
@@ -87,9 +87,9 @@ protected function emptyRow() : void
8787
sprintf(
8888
"%s%s%s%s%s\n",
8989
$this->style->getColoursSetCode(),
90-
str_repeat(' ', $this->style->getPadding()),
90+
str_repeat(' ', $this->style->getPaddingLeftRight()),
9191
str_repeat(' ', mb_strlen($this->text)),
92-
str_repeat(' ', $this->style->getPadding()),
92+
str_repeat(' ', $this->style->getPaddingLeftRight()),
9393
$this->style->getColoursResetCode()
9494
)
9595
);

src/Dialogue/Flash.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public function display() : void
2424
$this->write(sprintf(
2525
"%s%s%s%s%s\n",
2626
$this->style->getColoursSetCode(),
27-
str_repeat(' ', $this->style->getPadding()),
27+
str_repeat(' ', $this->style->getPaddingLeftRight()),
2828
$this->text,
29-
str_repeat(' ', $this->style->getPadding()),
29+
str_repeat(' ', $this->style->getPaddingLeftRight()),
3030
$this->style->getColoursResetCode()
3131
));
3232

src/Input/InputIO.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ private function calculateXPosition(Input $input, string $userInput) : int
131131
);
132132

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

137137
return $parentHalfWidth - $halfWidth;
@@ -144,9 +144,9 @@ private function drawLine(Input $input, string $userInput, string $text) : void
144144
$line = sprintf(
145145
"%s%s%s%s%s\n",
146146
$input->getStyle()->getColoursSetCode(),
147-
str_repeat(' ', $input->getStyle()->getPadding()),
147+
str_repeat(' ', $input->getStyle()->getPaddingLeftRight()),
148148
$text,
149-
str_repeat(' ', $input->getStyle()->getPadding()),
149+
str_repeat(' ', $input->getStyle()->getPaddingLeftRight()),
150150
$input->getStyle()->getColoursResetCode()
151151
);
152152

src/MenuItem/AsciiArtItem.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class AsciiArtItem implements MenuItemInterface
1616
const POSITION_CENTER = 'center';
1717
const POSITION_LEFT = 'left';
1818
const POSITION_RIGHT = 'right';
19-
19+
2020
/**
2121
* @var string
2222
*/
@@ -40,7 +40,7 @@ class AsciiArtItem implements MenuItemInterface
4040
public function __construct(string $text, string $position = self::POSITION_CENTER, string $alt = '')
4141
{
4242
Assertion::inArray($position, [self::POSITION_CENTER, self::POSITION_RIGHT, self::POSITION_LEFT]);
43-
43+
4444
$this->text = implode("\n", array_map(function (string $line) {
4545
return rtrim($line, ' ');
4646
}, explode("\n", $text)));
@@ -60,7 +60,7 @@ public function getRows(MenuStyle $style, bool $selected = false) : array
6060
}
6161

6262
$padding = $style->getContentWidth() - $this->artLength;
63-
63+
6464
return array_map(function ($row) use ($padding) {
6565
switch ($this->position) {
6666
case self::POSITION_LEFT:

0 commit comments

Comments
 (0)