From 6d74ed474f5fc088203a21e72cbdc847b5dff70f Mon Sep 17 00:00:00 2001 From: Alexander Zhigalin Date: Fri, 15 Dec 2023 15:06:41 +0100 Subject: [PATCH] Add option to change the newlines before and after the menu is drawn aka vertical margin --- README.md | 13 ++++++++++++ src/Builder/CliMenuBuilder.php | 8 ++++++++ src/CliMenu.php | 12 +++++++++--- src/MenuStyle.php | 36 ++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 97274f0..26c4aec 100644 --- a/README.md +++ b/README.md @@ -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 +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 diff --git a/src/Builder/CliMenuBuilder.php b/src/Builder/CliMenuBuilder.php index 67e3cab..1af8049 100644 --- a/src/Builder/CliMenuBuilder.php +++ b/src/Builder/CliMenuBuilder.php @@ -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; diff --git a/src/CliMenu.php b/src/CliMenu.php index f3f9de7..c117cd4 100644 --- a/src/CliMenu.php +++ b/src/CliMenu.php @@ -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()); @@ -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) { diff --git a/src/MenuStyle.php b/src/MenuStyle.php index c67e7c2..0b6fae5 100644 --- a/src/MenuStyle.php +++ b/src/MenuStyle.php @@ -159,6 +159,16 @@ class MenuStyle */ private $debugMode = false; + /** + * @var int + */ + protected $frameTopNewlines = 2; + + /** + * @var int + */ + protected $frameBottomNewlines = 2; + /** * Default Values * @@ -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.