Skip to content

Add option to change the newlines before and after the menu is drawn #280

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,19 @@ $menu = (new CliMenuBuilder)
->build();
```

You can also change the number of newlines before and after the menu acting sort like a vertical margin:

```php
<?php

use PhpSchool\CliMenu\Builder\CliMenuBuilder;

$menu = (new CliMenuBuilder)
->setWidth(200)
->setFrameNewlines(5, 2) // top, bottom
->build();
```

#### Borders

Borders can be customised just like CSS borders. We can add any amount of border to either side, left, right top or
Expand Down
8 changes: 8 additions & 0 deletions src/Builder/CliMenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,14 @@ public function setBorderColour(string $colour, string $fallback = null) : self
return $this;
}

public function setFrameNewlines(int $top, int $bottom) : self
{
$this->style->setFrameTopNewlines($top);
$this->style->setFrameBottomNewlines($bottom);

return $this;
}

public function getStyle() : MenuStyle
{
return $this->style;
Expand Down
12 changes: 9 additions & 3 deletions src/CliMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,11 @@ private function assertOpen() : void
protected function draw() : void
{
$frame = new Frame;

$frame->newLine(2);

$lines = $this->style->getFrameTopNewlines();
if ($lines>0) {
$frame->newLine($lines);
}

if ($this->style->getBorderTopWidth() > 0) {
$frame->addRows($this->style->getBorderTopRows());
Expand Down Expand Up @@ -521,7 +524,10 @@ protected function draw() : void
$frame->addRows($this->style->getBorderBottomRows());
}

$frame->newLine(2);
$lines = $this->style->getFrameBottomNewlines();
if ($lines>0) {
$frame->newLine($lines);
}

$this->terminal->moveCursorToTop();
foreach ($frame->getRows() as $row) {
Expand Down
36 changes: 36 additions & 0 deletions src/MenuStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,16 @@ class MenuStyle
*/
private $debugMode = false;

/**
* @var int
*/
protected $frameTopNewlines = 2;

/**
* @var int
*/
protected $frameBottomNewlines = 2;

/**
* Default Values
*
Expand Down Expand Up @@ -818,6 +828,32 @@ public function getBorderColourCode() : string
return sprintf("\033[%sm", $borderColourCode);
}

public function getFrameTopNewlines() : int
{
return $this->frameTopNewlines;
}

public function getFrameBottomNewlines() : int
{
return $this->frameBottomNewlines;
}

public function setFrameTopNewlines(int $lines) : self
{
Assertion::greaterOrEqualThan($lines, 0);
$this->frameTopNewlines = $lines;

return $this;
}

public function setFrameBottomNewlines(int $lines) : self
{
Assertion::greaterOrEqualThan($lines, 0);
$this->frameBottomNewlines = $lines;

return $this;
}


/**
* Get ansi escape sequence for setting or unsetting the specified option code.
Expand Down