From bd160290c3de9286b0d7871f94486de5fbce9bda Mon Sep 17 00:00:00 2001 From: Lynesth Date: Fri, 4 May 2018 19:01:51 +1100 Subject: [PATCH 01/35] Add border styling --- src/MenuStyle.php | 110 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 108 insertions(+), 2 deletions(-) diff --git a/src/MenuStyle.php b/src/MenuStyle.php index 9f630719..81fbe3d5 100644 --- a/src/MenuStyle.php +++ b/src/MenuStyle.php @@ -73,6 +73,31 @@ class MenuStyle */ private $titleSeparator; + /** + * @var int + */ + private $borderTopWidth; + + /** + * @var int + */ + private $borderRightWidth; + + /** + * @var int + */ + private $borderBottomWidth; + + /** + * @var int + */ + private $borderLeftWidth; + + /** + * @var string + */ + private $borderColour; + /** * Default Values * @@ -89,6 +114,11 @@ class MenuStyle 'itemExtra' => '✔', 'displaysExtra' => false, 'titleSeparator' => '=', + 'borderTopWidth' => 0, + 'borderRightWidth' => 0, + 'borderBottomWidth' => 0, + 'borderLeftWidth' => 0, + 'borderColour' => 'white', ]; public static function getDefaultStyleValues() : array @@ -155,6 +185,12 @@ public function __construct(Terminal $terminal = null) $this->setItemExtra(static::$defaultStyleValues['itemExtra']); $this->setDisplaysExtra(static::$defaultStyleValues['displaysExtra']); $this->setTitleSeparator(static::$defaultStyleValues['titleSeparator']); + $this->setBorderTopWidth(static::$defaultStyleValues['borderTopWidth']); + $this->setBorderRightWidth(static::$defaultStyleValues['borderRightWidth']); + $this->setBorderBottomWidth(static::$defaultStyleValues['borderBottomWidth']); + $this->setBorderLeftWidth(static::$defaultStyleValues['borderLeftWidth']); + $this->setBorderColour(static::$defaultStyleValues['borderColour']); + } public static function getAvailableColours() : array @@ -233,7 +269,7 @@ public function getUnselectedUnsetCode() : string */ protected function calculateContentWidth() : void { - $this->contentWidth = $this->width - ($this->padding*2) - ($this->margin*2); + $this->contentWidth = $this->width - ($this->padding*2) - ($this->margin*2) - ($this->borderRightWidth + $this->borderLeftWidth); } public function getFg() : string @@ -267,7 +303,7 @@ public function getWidth() : int public function setWidth(int $width) : self { - $availableWidth = $this->terminal->getWidth() - ($this->margin * 2) - ($this->padding * 2); + $availableWidth = $this->terminal->getWidth() - ($this->margin * 2) - ($this->padding * 2) - ($this->borderRightWidth + $this->borderLeftWidth); if ($width >= $availableWidth) { $width = $availableWidth; @@ -387,4 +423,74 @@ public function setTitleSeparator(string $actionSeparator) : self return $this; } + + /** + * Shorthand function to set all borders values at once + */ + public function setBorder(int $topWidth, int $rightWidth = null, int $bottomWidth = null, int $leftWidth = null, string $colour = null) : self + { + $this->borderTopWidth = $topWidth; + $this->borderRightWidth = $rightWidth ?? $topWidth; + $this->borderBottomWidth = $bottomWidth ?? $topWidth; + $this->borderLeftWidth = $leftWidth ?? $rightWidth ?? $topWidth; + if ($colour !== null) { + $this->borderColour = $colour; + } + + return $this; + } + public function setBorderTopWidth(int $width) : self + { + $this->borderTopWidth = $width; + + return $this; + } + public function setBorderRightWidth(int $width) : self + { + $this->borderRightWidth = $width; + + return $this; + } + public function setBorderBottomWidth(int $width) : self + { + $this->borderBottomWidth = $width; + + return $this; + } + public function setBorderLeftWidth(int $width) : self + { + $this->borderLeftWidth = $width; + + return $this; + } + public function setBorderColour(string $colour) : self + { + $this->borderColour = $colour; + + return $this; + } + public function getBorderTopWidth() : int + { + return $this->borderTopWidth; + } + public function getBorderRightWidth() : int + { + return $this->borderRightWidth; + } + public function getBorderBottomWidth() : int + { + return $this->borderBottomWidth; + } + public function getBorderLeftWidth() : int + { + return $this->borderLeftWidth; + } + public function getBorderColour() : string + { + return $this->borderColour; + } + public function getBorderColourCode() : string + { + return sprintf("\033[%sm", self::$availableBackgroundColors[$this->getBorderColour()]['set']); + } } From b94fdb18c43d14f2d73c690f850dbc8a0a9eb11e Mon Sep 17 00:00:00 2001 From: Lynesth Date: Fri, 4 May 2018 19:15:24 +1100 Subject: [PATCH 02/35] Add border styling --- src/CliMenu.php | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/CliMenu.php b/src/CliMenu.php index c07df062..bea4bfdc 100644 --- a/src/CliMenu.php +++ b/src/CliMenu.php @@ -319,6 +319,27 @@ protected function draw() : void $frame = new Frame; $frame->newLine(2); + + $borderRow = [ + sprintf( + "%s%s%s%s%s%s%s%s%s\n", + str_repeat(' ', $this->style->getMargin()), + $this->style->getBorderColourCode(), + str_repeat(' ', $this->style->getBorderLeftWidth()), + str_repeat(' ', $this->style->getPadding()), + str_repeat(' ', $this->style->getContentWidth()), + str_repeat(' ', $this->style->getPadding()), + str_repeat(' ', $this->style->getBorderRightWidth()), + "\033[0m", + str_repeat(' ', $this->style->getMargin()) + ) + ]; + + if ($this->style->getBorderTopWidth() > 0) { + for ($b = 0; $b < $this->style->getBorderTopWidth(); $b++) { + $frame->addRows($borderRow); + } + } if ($this->title) { $frame->addRows($this->drawMenuItem(new LineBreakItem())); @@ -331,6 +352,12 @@ protected function draw() : void }, $this->items, array_keys($this->items)); $frame->addRows($this->drawMenuItem(new LineBreakItem())); + + if ($this->style->getBorderBottomWidth() > 0) { + for ($b = 0; $b < $this->style->getBorderBottomWidth(); $b++) { + $frame->addRows($borderRow); + } + } $frame->newLine(2); @@ -360,15 +387,21 @@ protected function drawMenuItem(MenuItemInterface $item, bool $selected = false) $unsetColour = $selected ? $this->style->getSelectedUnsetCode() : $this->style->getUnselectedUnsetCode(); + + $borderColour = $this->style->getBorderColourCode(); - return array_map(function ($row) use ($setColour, $unsetColour) { + return array_map(function ($row) use ($setColour, $unsetColour, $borderColour) { return sprintf( - "%s%s%s%s%s%s%s\n", + "%s%s%s%s%s%s%s%s%s%s%s\n", str_repeat(' ', $this->style->getMargin()), + $borderColour, + str_repeat(' ', $this->style->getBorderLeftWidth()), $setColour, str_repeat(' ', $this->style->getPadding()), $row, str_repeat(' ', $this->style->getRightHandPadding(mb_strlen(s::stripAnsiEscapeSequence($row)))), + $borderColour, + str_repeat(' ', $this->style->getBorderRightWidth()), $unsetColour, str_repeat(' ', $this->style->getMargin()) ); From 3bf82d35a57ddfaed044c84433044a21ce866ec2 Mon Sep 17 00:00:00 2001 From: Lynesth Date: Fri, 4 May 2018 21:48:05 +1100 Subject: [PATCH 03/35] Fix setBorder() to work like CSS border --- src/MenuStyle.php | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/MenuStyle.php b/src/MenuStyle.php index 81fbe3d5..a1562228 100644 --- a/src/MenuStyle.php +++ b/src/MenuStyle.php @@ -427,13 +427,28 @@ public function setTitleSeparator(string $actionSeparator) : self /** * Shorthand function to set all borders values at once */ - public function setBorder(int $topWidth, int $rightWidth = null, int $bottomWidth = null, int $leftWidth = null, string $colour = null) : self - { + public function setBorder( + int $topWidth, + $rightWidth = null, + $bottomWidth = null, + $leftWidth = null, + string $colour = null + ) : self { $this->borderTopWidth = $topWidth; - $this->borderRightWidth = $rightWidth ?? $topWidth; - $this->borderBottomWidth = $bottomWidth ?? $topWidth; - $this->borderLeftWidth = $leftWidth ?? $rightWidth ?? $topWidth; - if ($colour !== null) { + + if (!is_int($rightWidth)) { + $this->borderRightWidth = $this->borderBottomWidth = $this->borderLeftWidth = $topWidth; + $colour = $rightWidth; + } else if (!is_int($bottomWidth)) { + $this->borderBottomWidth = $topWidth; + $this->borderLeftWidth = $rightWidth; + $colour = $bottomWidth; + } else if (!is_int($leftWidth)) { + $this->borderLeftWidth = $rightWidth; + $colour = $leftWidth; + } + + if (is_string($colour)) { $this->borderColour = $colour; } From 4645481c78ef07ec17164b4bf7e0a72ef2e4d2d2 Mon Sep 17 00:00:00 2001 From: Lynesth Date: Fri, 4 May 2018 21:49:00 +1100 Subject: [PATCH 04/35] Add setBorder() to CliMenuBuilder --- src/CliMenuBuilder.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/CliMenuBuilder.php b/src/CliMenuBuilder.php index 3a2174b6..610aca5e 100644 --- a/src/CliMenuBuilder.php +++ b/src/CliMenuBuilder.php @@ -265,6 +265,18 @@ public function setTitleSeparator(string $separator) : self return $this; } + public function setBorder( + int $topWidth, + $rightWidth = null, + $bottomWidth = null, + $leftWidth = null, + string $colour = null + ) : self { + $this->style->setBorder($topWidth, $rightWidth, $bottomWidth, $leftWidth, $colour); + + return $this; + } + public function setTerminal(Terminal $terminal) : self { $this->terminal = $terminal; From 5b9c9f6cba71c0c591dd6a0fd766c033ba63575e Mon Sep 17 00:00:00 2001 From: Lynesth Date: Fri, 4 May 2018 21:50:52 +1100 Subject: [PATCH 05/35] Fix PSR2 --- src/MenuStyle.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/MenuStyle.php b/src/MenuStyle.php index a1562228..eeb4d63e 100644 --- a/src/MenuStyle.php +++ b/src/MenuStyle.php @@ -190,7 +190,6 @@ public function __construct(Terminal $terminal = null) $this->setBorderBottomWidth(static::$defaultStyleValues['borderBottomWidth']); $this->setBorderLeftWidth(static::$defaultStyleValues['borderLeftWidth']); $this->setBorderColour(static::$defaultStyleValues['borderColour']); - } public static function getAvailableColours() : array @@ -269,7 +268,10 @@ public function getUnselectedUnsetCode() : string */ protected function calculateContentWidth() : void { - $this->contentWidth = $this->width - ($this->padding*2) - ($this->margin*2) - ($this->borderRightWidth + $this->borderLeftWidth); + $this->contentWidth = $this->width + - ($this->padding*2) + - ($this->margin*2) + - ($this->borderRightWidth + $this->borderLeftWidth); } public function getFg() : string @@ -303,7 +305,10 @@ public function getWidth() : int public function setWidth(int $width) : self { - $availableWidth = $this->terminal->getWidth() - ($this->margin * 2) - ($this->padding * 2) - ($this->borderRightWidth + $this->borderLeftWidth); + $availableWidth = $this->terminal->getWidth() + - ($this->margin * 2) + - ($this->padding * 2) + - ($this->borderRightWidth + $this->borderLeftWidth); if ($width >= $availableWidth) { $width = $availableWidth; From 8ad863f1592efd62b9146f59cac76b58e0cb3921 Mon Sep 17 00:00:00 2001 From: Lynesth Date: Fri, 4 May 2018 21:55:02 +1100 Subject: [PATCH 06/35] Fix PSR2 --- src/MenuStyle.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MenuStyle.php b/src/MenuStyle.php index eeb4d63e..635af451 100644 --- a/src/MenuStyle.php +++ b/src/MenuStyle.php @@ -444,11 +444,11 @@ public function setBorder( if (!is_int($rightWidth)) { $this->borderRightWidth = $this->borderBottomWidth = $this->borderLeftWidth = $topWidth; $colour = $rightWidth; - } else if (!is_int($bottomWidth)) { + } elseif (!is_int($bottomWidth)) { $this->borderBottomWidth = $topWidth; $this->borderLeftWidth = $rightWidth; $colour = $bottomWidth; - } else if (!is_int($leftWidth)) { + } elseif (!is_int($leftWidth)) { $this->borderLeftWidth = $rightWidth; $colour = $leftWidth; } From 5209f8c157663f8a4429a33d96e04f9870091979 Mon Sep 17 00:00:00 2001 From: Lynesth Date: Fri, 4 May 2018 21:57:11 +1100 Subject: [PATCH 07/35] Should fix the tests --- src/CliMenu.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/CliMenu.php b/src/CliMenu.php index bea4bfdc..88ea96a2 100644 --- a/src/CliMenu.php +++ b/src/CliMenu.php @@ -388,7 +388,11 @@ protected function drawMenuItem(MenuItemInterface $item, bool $selected = false) ? $this->style->getSelectedUnsetCode() : $this->style->getUnselectedUnsetCode(); - $borderColour = $this->style->getBorderColourCode(); + if ($this->style->getBorderLeftWidth() || $this->style->getBorderRightWidth()) { + $borderColour = $this->style->getBorderColourCode(); + } else { + $borderColour = ''; + } return array_map(function ($row) use ($setColour, $unsetColour, $borderColour) { return sprintf( From 796a49de040716d552aabfe48e2066a0bfb31d74 Mon Sep 17 00:00:00 2001 From: Lynesth Date: Fri, 4 May 2018 22:20:24 +1100 Subject: [PATCH 08/35] Fix setBorder --- src/CliMenuBuilder.php | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/CliMenuBuilder.php b/src/CliMenuBuilder.php index 610aca5e..56f5fac0 100644 --- a/src/CliMenuBuilder.php +++ b/src/CliMenuBuilder.php @@ -272,7 +272,25 @@ public function setBorder( $leftWidth = null, string $colour = null ) : self { - $this->style->setBorder($topWidth, $rightWidth, $bottomWidth, $leftWidth, $colour); + $this->style['borderTopWidth'] = $topWidth; + + if (!is_int($rightWidth)) { + $this->style['borderRightWidth'] = $topWidth; + $this->style['borderBottomWidth'] = $topWidth; + $this->style['borderLeftWidth'] = $topWidth; + $colour = $rightWidth; + } elseif (!is_int($bottomWidth)) { + $this->style['borderBottomWidth'] = $topWidth; + $this->style['borderLeftWidth'] = $rightWidth; + $colour = $bottomWidth; + } elseif (!is_int($leftWidth)) { + $this->style['borderLeftWidth'] = $rightWidth; + $colour = $leftWidth; + } + + if (is_string($colour)) { + $this->style['borderColour'] = $colour; + } return $this; } From 0d8351201c7aaa89dfc6def49cdb88c168ed6485 Mon Sep 17 00:00:00 2001 From: Lynesth Date: Fri, 4 May 2018 22:36:38 +1100 Subject: [PATCH 09/35] Missing newlines --- src/MenuStyle.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/MenuStyle.php b/src/MenuStyle.php index 635af451..3712a6a5 100644 --- a/src/MenuStyle.php +++ b/src/MenuStyle.php @@ -459,56 +459,67 @@ public function setBorder( return $this; } + public function setBorderTopWidth(int $width) : self { $this->borderTopWidth = $width; return $this; } + public function setBorderRightWidth(int $width) : self { $this->borderRightWidth = $width; return $this; } + public function setBorderBottomWidth(int $width) : self { $this->borderBottomWidth = $width; return $this; } + public function setBorderLeftWidth(int $width) : self { $this->borderLeftWidth = $width; return $this; } + public function setBorderColour(string $colour) : self { $this->borderColour = $colour; return $this; } + public function getBorderTopWidth() : int { return $this->borderTopWidth; } + public function getBorderRightWidth() : int { return $this->borderRightWidth; } + public function getBorderBottomWidth() : int { return $this->borderBottomWidth; } + public function getBorderLeftWidth() : int { return $this->borderLeftWidth; } + public function getBorderColour() : string { return $this->borderColour; } + public function getBorderColourCode() : string { return sprintf("\033[%sm", self::$availableBackgroundColors[$this->getBorderColour()]['set']); From 7eb1520dddd8a4a571e3b2231a0e5c42e46390bc Mon Sep 17 00:00:00 2001 From: Lynesth Date: Fri, 4 May 2018 22:40:12 +1100 Subject: [PATCH 10/35] Missing configuration in buildStyle() --- src/CliMenuBuilder.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/CliMenuBuilder.php b/src/CliMenuBuilder.php index 56f5fac0..f1838f0c 100644 --- a/src/CliMenuBuilder.php +++ b/src/CliMenuBuilder.php @@ -360,7 +360,12 @@ private function buildStyle() : MenuStyle ->setUnselectedMarker($this->style['unselectedMarker']) ->setItemExtra($this->style['itemExtra']) ->setDisplaysExtra($this->style['displaysExtra']) - ->setTitleSeparator($this->style['titleSeparator']); + ->setTitleSeparator($this->style['titleSeparator']) + ->setBorderTopWidth($this->style['borderTopWidth']) + ->setBorderRightWidth($this->style['borderRightWidth']) + ->setBorderBottomWidth($this->style['borderBottomWidth']) + ->setBorderLeftWidth($this->style['borderLeftWidth']) + ->setBorderColour($this->style['borderColour']); } /** From 763619b192b1b939cd58a02abddb447c89b48e13 Mon Sep 17 00:00:00 2001 From: Lynesth Date: Fri, 4 May 2018 22:56:42 +1100 Subject: [PATCH 11/35] Add MenuStyle border tests --- test/MenuStyleTest.php | 82 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/test/MenuStyleTest.php b/test/MenuStyleTest.php index 37651c03..21c6e719 100644 --- a/test/MenuStyleTest.php +++ b/test/MenuStyleTest.php @@ -117,6 +117,11 @@ public function testGetterAndSetters() : void static::assertSame(100, $style->getWidth()); static::assertSame(2, $style->getMargin()); static::assertSame(2, $style->getPadding()); + static::assertSame(0, $style->getBorderTopWidth()); + static::assertSame(0, $style->getBorderRightWidth()); + static::assertSame(0, $style->getBorderBottomWidth()); + static::assertSame(0, $style->getBorderLeftWidth()); + static::assertSame('white', $style->getBorderColour()); $style->setBg('red'); $style->setFg('yellow'); @@ -128,6 +133,11 @@ public function testGetterAndSetters() : void $style->setWidth(200); $style->setMargin(10); $style->setPadding(10); + $style->setBorderTopWidth(1); + $style->setBorderRightWidth(2); + $style->setBorderBottomWidth(3); + $style->setBorderLeftWidth(4); + $style->setBorderColour('green'); static::assertSame('red', $style->getBg()); static::assertSame('yellow', $style->getFg()); @@ -139,6 +149,78 @@ public function testGetterAndSetters() : void static::assertSame(200, $style->getWidth()); static::assertSame(10, $style->getMargin()); static::assertSame(10, $style->getPadding()); + static::assertSame(1, $style->getBorderTopWidth()); + static::assertSame(2, $style->getBorderRightWidth()); + static::assertSame(3, $style->getBorderBottomWidth()); + static::assertSame(4, $style->getBorderLeftWidth()); + static::assertSame('green', $style->getBorderColour()); + } + + public function testSetBorderShorthandFunction() : void + { + $style = $this->getMenuStyle(); + $style->setBorder(3); + static::assertSame(3, $style->getBorderTopWidth()); + static::assertSame(3, $style->getBorderRightWidth()); + static::assertSame(3, $style->getBorderBottomWidth()); + static::assertSame(3, $style->getBorderLeftWidth()); + static::assertSame('white', $style->getBorderColour()); + + $style = $this->getMenuStyle(); + $style->setBorder(3, 4); + static::assertSame(3, $style->getBorderTopWidth()); + static::assertSame(4, $style->getBorderRightWidth()); + static::assertSame(3, $style->getBorderBottomWidth()); + static::assertSame(4, $style->getBorderLeftWidth()); + static::assertSame('white', $style->getBorderColour()); + + $style = $this->getMenuStyle(); + $style->setBorder(3, 4, 5); + static::assertSame(3, $style->getBorderTopWidth()); + static::assertSame(4, $style->getBorderRightWidth()); + static::assertSame(5, $style->getBorderBottomWidth()); + static::assertSame(4, $style->getBorderLeftWidth()); + static::assertSame('white', $style->getBorderColour()); + + $style = $this->getMenuStyle(); + $style->setBorder(3, 4, 5, 6); + static::assertSame(3, $style->getBorderTopWidth()); + static::assertSame(4, $style->getBorderRightWidth()); + static::assertSame(5, $style->getBorderBottomWidth()); + static::assertSame(6, $style->getBorderLeftWidth()); + static::assertSame('white', $style->getBorderColour()); + + $style = $this->getMenuStyle(); + $style->setBorder(3, 4, 5, 6, 'red'); + static::assertSame(3, $style->getBorderTopWidth()); + static::assertSame(4, $style->getBorderRightWidth()); + static::assertSame(5, $style->getBorderBottomWidth()); + static::assertSame(6, $style->getBorderLeftWidth()); + static::assertSame('red', $style->getBorderColour()); + + $style = $this->getMenuStyle(); + $style->setBorder(3, 4, 5, 'red'); + static::assertSame(3, $style->getBorderTopWidth()); + static::assertSame(4, $style->getBorderRightWidth()); + static::assertSame(5, $style->getBorderBottomWidth()); + static::assertSame(4, $style->getBorderLeftWidth()); + static::assertSame('red', $style->getBorderColour()); + + $style = $this->getMenuStyle(); + $style->setBorder(3, 4, 'red'); + static::assertSame(3, $style->getBorderTopWidth()); + static::assertSame(4, $style->getBorderRightWidth()); + static::assertSame(3, $style->getBorderBottomWidth()); + static::assertSame(4, $style->getBorderLeftWidth()); + static::assertSame('red', $style->getBorderColour()); + + $style = $this->getMenuStyle(); + $style->setBorder(3, 'red'); + static::assertSame(3, $style->getBorderTopWidth()); + static::assertSame(3, $style->getBorderRightWidth()); + static::assertSame(3, $style->getBorderBottomWidth()); + static::assertSame(3, $style->getBorderLeftWidth()); + static::assertSame('red', $style->getBorderColour()); } public function testGetMarkerReturnsTheCorrectMarkers() : void From 07b4e0cf5f676c24d0e49a3d28df00e0af5b34da Mon Sep 17 00:00:00 2001 From: Lynesth Date: Fri, 4 May 2018 23:05:12 +1100 Subject: [PATCH 12/35] Fix failing logic... --- src/MenuStyle.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/MenuStyle.php b/src/MenuStyle.php index 3712a6a5..34a63ff9 100644 --- a/src/MenuStyle.php +++ b/src/MenuStyle.php @@ -439,20 +439,23 @@ public function setBorder( $leftWidth = null, string $colour = null ) : self { - $this->borderTopWidth = $topWidth; - if (!is_int($rightWidth)) { - $this->borderRightWidth = $this->borderBottomWidth = $this->borderLeftWidth = $topWidth; + $rightWidth = $bottomWidth = $leftWidth = $topWidth; $colour = $rightWidth; } elseif (!is_int($bottomWidth)) { - $this->borderBottomWidth = $topWidth; - $this->borderLeftWidth = $rightWidth; + $bottomWidth = $topWidth; + $leftWidth = $rightWidth; $colour = $bottomWidth; } elseif (!is_int($leftWidth)) { - $this->borderLeftWidth = $rightWidth; + $leftWidth = $rightWidth; $colour = $leftWidth; } + $this->borderTopWidth = $topWidth; + $this->borderRightWidth = $rightWidth; + $this->borderBottomWidth = $bottomWidth; + $this->borderLeftWidth = $leftWidth; + if (is_string($colour)) { $this->borderColour = $colour; } From 17164ed88d589abbc9d1a587ac334d6678e4f7c7 Mon Sep 17 00:00:00 2001 From: Lynesth Date: Fri, 4 May 2018 23:07:35 +1100 Subject: [PATCH 13/35] Fix failing logic... --- src/CliMenuBuilder.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/CliMenuBuilder.php b/src/CliMenuBuilder.php index f1838f0c..4c695911 100644 --- a/src/CliMenuBuilder.php +++ b/src/CliMenuBuilder.php @@ -272,22 +272,23 @@ public function setBorder( $leftWidth = null, string $colour = null ) : self { - $this->style['borderTopWidth'] = $topWidth; - if (!is_int($rightWidth)) { - $this->style['borderRightWidth'] = $topWidth; - $this->style['borderBottomWidth'] = $topWidth; - $this->style['borderLeftWidth'] = $topWidth; + $rightWidth = $bottomWidth = $leftWidth = $topWidth; $colour = $rightWidth; } elseif (!is_int($bottomWidth)) { - $this->style['borderBottomWidth'] = $topWidth; - $this->style['borderLeftWidth'] = $rightWidth; + $bottomWidth = $topWidth; + $leftWidth = $rightWidth; $colour = $bottomWidth; } elseif (!is_int($leftWidth)) { - $this->style['borderLeftWidth'] = $rightWidth; + $leftWidth = $rightWidth; $colour = $leftWidth; } + $this->style['borderTopWidth'] = $topWidth; + $this->style['borderRightWidth'] = $rightWidth; + $this->style['borderBottomWidth'] = $bottomWidth; + $this->style['borderLeftWidth'] = $leftWidth; + if (is_string($colour)) { $this->style['borderColour'] = $colour; } From 42a0206b4f9d3a01e7642ba0ab3ebd3d53a6be14 Mon Sep 17 00:00:00 2001 From: Lynesth Date: Fri, 4 May 2018 23:12:55 +1100 Subject: [PATCH 14/35] Fixing some more --- src/MenuStyle.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/MenuStyle.php b/src/MenuStyle.php index 34a63ff9..4f508589 100644 --- a/src/MenuStyle.php +++ b/src/MenuStyle.php @@ -440,15 +440,15 @@ public function setBorder( string $colour = null ) : self { if (!is_int($rightWidth)) { - $rightWidth = $bottomWidth = $leftWidth = $topWidth; $colour = $rightWidth; + $rightWidth = $bottomWidth = $leftWidth = $topWidth; } elseif (!is_int($bottomWidth)) { + $colour = $bottomWidth; $bottomWidth = $topWidth; $leftWidth = $rightWidth; - $colour = $bottomWidth; } elseif (!is_int($leftWidth)) { - $leftWidth = $rightWidth; $colour = $leftWidth; + $leftWidth = $rightWidth; } $this->borderTopWidth = $topWidth; From a49d87db8724d23010a750b7cb9842c25ac15004 Mon Sep 17 00:00:00 2001 From: Lynesth Date: Fri, 4 May 2018 23:16:42 +1100 Subject: [PATCH 15/35] Some more fixes --- src/CliMenuBuilder.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/CliMenuBuilder.php b/src/CliMenuBuilder.php index 4c695911..695f8e50 100644 --- a/src/CliMenuBuilder.php +++ b/src/CliMenuBuilder.php @@ -273,15 +273,15 @@ public function setBorder( string $colour = null ) : self { if (!is_int($rightWidth)) { - $rightWidth = $bottomWidth = $leftWidth = $topWidth; $colour = $rightWidth; + $rightWidth = $bottomWidth = $leftWidth = $topWidth; } elseif (!is_int($bottomWidth)) { + $colour = $bottomWidth; $bottomWidth = $topWidth; $leftWidth = $rightWidth; - $colour = $bottomWidth; } elseif (!is_int($leftWidth)) { - $leftWidth = $rightWidth; $colour = $leftWidth; + $leftWidth = $rightWidth; } $this->style['borderTopWidth'] = $topWidth; From 5a23153b8ba6f3b50b83fcf43151f494a6a5dc2d Mon Sep 17 00:00:00 2001 From: Lynesth Date: Fri, 4 May 2018 23:19:28 +1100 Subject: [PATCH 16/35] Adding border to width and padding tests --- test/MenuStyleTest.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/MenuStyleTest.php b/test/MenuStyleTest.php index 21c6e719..3fdc27d6 100644 --- a/test/MenuStyleTest.php +++ b/test/MenuStyleTest.php @@ -241,8 +241,9 @@ public function testWidthCalculation() : void $style->setWidth(300); $style->setPadding(5); $style->setMargin(5); + $style->setBorder(2); - static::assertSame(280, $style->getContentWidth()); + static::assertSame(276, $style->getContentWidth()); } public function testRightHandPaddingCalculation() : void @@ -252,7 +253,8 @@ public function testRightHandPaddingCalculation() : void $style->setWidth(300); $style->setPadding(5); $style->setMargin(5); + $style->setBorder(2); - static::assertSame(235, $style->getRightHandPadding(50)); + static::assertSame(231, $style->getRightHandPadding(50)); } } From f435c442c2f14682263dba7a472660b821c88d10 Mon Sep 17 00:00:00 2001 From: Lynesth Date: Sat, 5 May 2018 11:17:15 +1100 Subject: [PATCH 17/35] Recalculate content width after setting borders --- src/MenuStyle.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/MenuStyle.php b/src/MenuStyle.php index 4f508589..34320994 100644 --- a/src/MenuStyle.php +++ b/src/MenuStyle.php @@ -460,6 +460,8 @@ public function setBorder( $this->borderColour = $colour; } + $this->calculateContentWidth(); + return $this; } @@ -473,6 +475,7 @@ public function setBorderTopWidth(int $width) : self public function setBorderRightWidth(int $width) : self { $this->borderRightWidth = $width; + $this->calculateContentWidth(); return $this; } @@ -487,6 +490,7 @@ public function setBorderBottomWidth(int $width) : self public function setBorderLeftWidth(int $width) : self { $this->borderLeftWidth = $width; + $this->calculateContentWidth(); return $this; } From ff3b1e82b49367b0fa76b2b8015baa08c92a82a4 Mon Sep 17 00:00:00 2001 From: Lynesth Date: Sat, 5 May 2018 12:38:16 +1100 Subject: [PATCH 18/35] Add Builder border tests --- test/CliMenuBuilderTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/CliMenuBuilderTest.php b/test/CliMenuBuilderTest.php index af096b2c..3db28961 100644 --- a/test/CliMenuBuilderTest.php +++ b/test/CliMenuBuilderTest.php @@ -61,6 +61,7 @@ public function testModifyStyles() : void $builder->setSelectedMarker('x'); $builder->setItemExtra('*'); $builder->setTitleSeparator('-'); + $builder->setBorder(2, 4, 5, 'green'); $terminal = static::createMock(Terminal::class); $terminal @@ -81,6 +82,11 @@ public function testModifyStyles() : void $this->checkStyleVariable($menu, 'selectedMarker', 'x'); $this->checkStyleVariable($menu, 'itemExtra', '*'); $this->checkStyleVariable($menu, 'titleSeparator', '-'); + $this->checkStyleVariable($menu, 'borderTopWidth', 2); + $this->checkStyleVariable($menu, 'borderRightWidth', 4); + $this->checkStyleVariable($menu, 'borderBottomWidth', 5); + $this->checkStyleVariable($menu, 'borderLeftWidth', 4); + $this->checkStyleVariable($menu, 'borderColour', 'green'); } public function testDisableDefaultItems() : void From ce87d98b1edc9456870543b75310229ecfeb3cb4 Mon Sep 17 00:00:00 2001 From: Lynesth Date: Sat, 5 May 2018 12:58:27 +1100 Subject: [PATCH 19/35] Max coverage for setBorder tests in Builder --- test/CliMenuBuilderTest.php | 93 +++++++++++++++++++++++++++++++++++-- 1 file changed, 88 insertions(+), 5 deletions(-) diff --git a/test/CliMenuBuilderTest.php b/test/CliMenuBuilderTest.php index 3db28961..278e12e9 100644 --- a/test/CliMenuBuilderTest.php +++ b/test/CliMenuBuilderTest.php @@ -61,18 +61,17 @@ public function testModifyStyles() : void $builder->setSelectedMarker('x'); $builder->setItemExtra('*'); $builder->setTitleSeparator('-'); - $builder->setBorder(2, 4, 5, 'green'); $terminal = static::createMock(Terminal::class); $terminal ->expects($this->any()) ->method('getWidth') ->will($this->returnValue(200)); - + $builder->setTerminal($terminal); - + $menu = $builder->build(); - + $this->checkStyleVariable($menu, 'bg', 'red'); $this->checkStyleVariable($menu, 'fg', 'red'); $this->checkStyleVariable($menu, 'width', 40); @@ -82,10 +81,94 @@ public function testModifyStyles() : void $this->checkStyleVariable($menu, 'selectedMarker', 'x'); $this->checkStyleVariable($menu, 'itemExtra', '*'); $this->checkStyleVariable($menu, 'titleSeparator', '-'); + } + + public function testSetBorderShorthandFunction() + { + $terminal = static::createMock(Terminal::class); + $terminal + ->expects($this->any()) + ->method('getWidth') + ->will($this->returnValue(200)); + + $menu = (new CliMenuBuilder) + ->setTerminal($terminal) + ->setBorder(2); + ->build(); + $this->checkStyleVariable($menu, 'borderTopWidth', 2); + $this->checkStyleVariable($menu, 'borderRightWidth', 2); + $this->checkStyleVariable($menu, 'borderBottomWidth', 2); + $this->checkStyleVariable($menu, 'borderLeftWidth', 2); + $this->checkStyleVariable($menu, 'borderColour', 'white'); + + $menu = (new CliMenuBuilder) + ->setTerminal($terminal) + ->setBorder(2, 4); + ->build(); $this->checkStyleVariable($menu, 'borderTopWidth', 2); $this->checkStyleVariable($menu, 'borderRightWidth', 4); - $this->checkStyleVariable($menu, 'borderBottomWidth', 5); + $this->checkStyleVariable($menu, 'borderBottomWidth', 2); $this->checkStyleVariable($menu, 'borderLeftWidth', 4); + $this->checkStyleVariable($menu, 'borderColour', 'white'); + + $menu = (new CliMenuBuilder) + ->setTerminal($terminal) + ->setBorder(2, 4, 6); + ->build(); + $this->checkStyleVariable($menu, 'borderTopWidth', 2); + $this->checkStyleVariable($menu, 'borderRightWidth', 4); + $this->checkStyleVariable($menu, 'borderBottomWidth', 6); + $this->checkStyleVariable($menu, 'borderLeftWidth', 4); + $this->checkStyleVariable($menu, 'borderColour', 'white'); + + $menu = (new CliMenuBuilder) + ->setTerminal($terminal) + ->setBorder(2, 4, 6, 8); + ->build(); + $this->checkStyleVariable($menu, 'borderTopWidth', 2); + $this->checkStyleVariable($menu, 'borderRightWidth', 4); + $this->checkStyleVariable($menu, 'borderBottomWidth', 6); + $this->checkStyleVariable($menu, 'borderLeftWidth', 8); + $this->checkStyleVariable($menu, 'borderColour', 'white'); + + $menu = (new CliMenuBuilder) + ->setTerminal($terminal) + ->setBorder(2, 4, 6, 8, 'green'); + ->build(); + $this->checkStyleVariable($menu, 'borderTopWidth', 2); + $this->checkStyleVariable($menu, 'borderRightWidth', 4); + $this->checkStyleVariable($menu, 'borderBottomWidth', 6); + $this->checkStyleVariable($menu, 'borderLeftWidth', 8); + $this->checkStyleVariable($menu, 'borderColour', 'green'); + + $menu = (new CliMenuBuilder) + ->setTerminal($terminal) + ->setBorder(2, 4, 6, 'green'); + ->build(); + $this->checkStyleVariable($menu, 'borderTopWidth', 2); + $this->checkStyleVariable($menu, 'borderRightWidth', 4); + $this->checkStyleVariable($menu, 'borderBottomWidth', 6); + $this->checkStyleVariable($menu, 'borderLeftWidth', 4); + $this->checkStyleVariable($menu, 'borderColour', 'green'); + + $menu = (new CliMenuBuilder) + ->setTerminal($terminal) + ->setBorder(2, 4, 'green'); + ->build(); + $this->checkStyleVariable($menu, 'borderTopWidth', 2); + $this->checkStyleVariable($menu, 'borderRightWidth', 4); + $this->checkStyleVariable($menu, 'borderBottomWidth', 2); + $this->checkStyleVariable($menu, 'borderLeftWidth', 4); + $this->checkStyleVariable($menu, 'borderColour', 'green'); + + $menu = (new CliMenuBuilder) + ->setTerminal($terminal) + ->setBorder(2, 'green'); + ->build(); + $this->checkStyleVariable($menu, 'borderTopWidth', 2); + $this->checkStyleVariable($menu, 'borderRightWidth', 2); + $this->checkStyleVariable($menu, 'borderBottomWidth', 2); + $this->checkStyleVariable($menu, 'borderLeftWidth', 2); $this->checkStyleVariable($menu, 'borderColour', 'green'); } From 9c0cac973494bc676008c6283a98d02ef29ff70a Mon Sep 17 00:00:00 2001 From: Lynesth Date: Sat, 5 May 2018 13:15:16 +1100 Subject: [PATCH 20/35] Fix typo --- test/CliMenuBuilderTest.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/CliMenuBuilderTest.php b/test/CliMenuBuilderTest.php index 278e12e9..2cefe7e8 100644 --- a/test/CliMenuBuilderTest.php +++ b/test/CliMenuBuilderTest.php @@ -93,7 +93,7 @@ public function testSetBorderShorthandFunction() $menu = (new CliMenuBuilder) ->setTerminal($terminal) - ->setBorder(2); + ->setBorder(2) ->build(); $this->checkStyleVariable($menu, 'borderTopWidth', 2); $this->checkStyleVariable($menu, 'borderRightWidth', 2); @@ -103,7 +103,7 @@ public function testSetBorderShorthandFunction() $menu = (new CliMenuBuilder) ->setTerminal($terminal) - ->setBorder(2, 4); + ->setBorder(2, 4) ->build(); $this->checkStyleVariable($menu, 'borderTopWidth', 2); $this->checkStyleVariable($menu, 'borderRightWidth', 4); @@ -113,7 +113,7 @@ public function testSetBorderShorthandFunction() $menu = (new CliMenuBuilder) ->setTerminal($terminal) - ->setBorder(2, 4, 6); + ->setBorder(2, 4, 6) ->build(); $this->checkStyleVariable($menu, 'borderTopWidth', 2); $this->checkStyleVariable($menu, 'borderRightWidth', 4); @@ -123,7 +123,7 @@ public function testSetBorderShorthandFunction() $menu = (new CliMenuBuilder) ->setTerminal($terminal) - ->setBorder(2, 4, 6, 8); + ->setBorder(2, 4, 6, 8) ->build(); $this->checkStyleVariable($menu, 'borderTopWidth', 2); $this->checkStyleVariable($menu, 'borderRightWidth', 4); @@ -133,7 +133,7 @@ public function testSetBorderShorthandFunction() $menu = (new CliMenuBuilder) ->setTerminal($terminal) - ->setBorder(2, 4, 6, 8, 'green'); + ->setBorder(2, 4, 6, 8, 'green') ->build(); $this->checkStyleVariable($menu, 'borderTopWidth', 2); $this->checkStyleVariable($menu, 'borderRightWidth', 4); @@ -143,7 +143,7 @@ public function testSetBorderShorthandFunction() $menu = (new CliMenuBuilder) ->setTerminal($terminal) - ->setBorder(2, 4, 6, 'green'); + ->setBorder(2, 4, 6, 'green') ->build(); $this->checkStyleVariable($menu, 'borderTopWidth', 2); $this->checkStyleVariable($menu, 'borderRightWidth', 4); @@ -153,7 +153,7 @@ public function testSetBorderShorthandFunction() $menu = (new CliMenuBuilder) ->setTerminal($terminal) - ->setBorder(2, 4, 'green'); + ->setBorder(2, 4, 'green') ->build(); $this->checkStyleVariable($menu, 'borderTopWidth', 2); $this->checkStyleVariable($menu, 'borderRightWidth', 4); @@ -163,7 +163,7 @@ public function testSetBorderShorthandFunction() $menu = (new CliMenuBuilder) ->setTerminal($terminal) - ->setBorder(2, 'green'); + ->setBorder(2, 'green') ->build(); $this->checkStyleVariable($menu, 'borderTopWidth', 2); $this->checkStyleVariable($menu, 'borderRightWidth', 2); From 752fa645335dfa9354df44c23756a921c47c94c0 Mon Sep 17 00:00:00 2001 From: Lynesth Date: Sat, 5 May 2018 13:22:52 +1100 Subject: [PATCH 21/35] Add borders to custom-styles example --- examples/custom-styles.php | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/custom-styles.php b/examples/custom-styles.php index e06ffacd..dead0fb4 100644 --- a/examples/custom-styles.php +++ b/examples/custom-styles.php @@ -19,6 +19,7 @@ ->setForegroundColour('black') ->setPadding(4) ->setMargin(4) + ->setBorder(1, 2, 'red') ->setUnselectedMarker(' ') ->setSelectedMarker('>') ->setTitleSeparator('- ') From 78affc20c732675f0ada73d0536da11574f02ee1 Mon Sep 17 00:00:00 2001 From: Lynesth Date: Tue, 8 May 2018 00:59:09 +1100 Subject: [PATCH 22/35] Better borderRows generation --- src/CliMenu.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/CliMenu.php b/src/CliMenu.php index 49dbf565..86abb78a 100644 --- a/src/CliMenu.php +++ b/src/CliMenu.php @@ -322,15 +322,11 @@ protected function draw() : void $borderRow = [ sprintf( - "%s%s%s%s%s%s%s%s%s\n", + "%s%s%s%s%s\n", str_repeat(' ', $this->style->getMargin()), $this->style->getBorderColourCode(), - str_repeat(' ', $this->style->getBorderLeftWidth()), - str_repeat(' ', $this->style->getPadding()), - str_repeat(' ', $this->style->getContentWidth()), - str_repeat(' ', $this->style->getPadding()), - str_repeat(' ', $this->style->getBorderRightWidth()), - "\033[0m", + str_repeat(' ', $this->style->getWidth()), + $this->style->getColoursResetCode(), str_repeat(' ', $this->style->getMargin()) ) ]; From da7a7f7e569bd10310be3698ece57d418bd2b444 Mon Sep 17 00:00:00 2001 From: Lynesth Date: Tue, 8 May 2018 01:13:10 +1100 Subject: [PATCH 23/35] Pre generate border top and bottom rows --- src/MenuStyle.php | 53 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/src/MenuStyle.php b/src/MenuStyle.php index 5f64e6ad..a810d01e 100644 --- a/src/MenuStyle.php +++ b/src/MenuStyle.php @@ -119,6 +119,16 @@ class MenuStyle */ private $borderColour; + /** + * @var array + */ + private $borderTopRows = []; + + /** + * @var array + */ + private $borderBottomRows = []; + /** * @var bool */ @@ -340,7 +350,9 @@ public function setWidth(int $width) : self if ($this->marginAuto) { $this->setMarginAuto(); } + $this->calculateContentWidth(); + $this->generateBorderRows(); return $this; } @@ -460,7 +472,39 @@ public function setTitleSeparator(string $actionSeparator) : self return $this; } - + + private function generateBorderRows() : void + { + $borderRow = sprintf( + "%s%s%s%s%s\n", + str_repeat(' ', $this->style->getMargin()), + $this->style->getBorderColourCode(), + str_repeat(' ', $this->style->getWidth()), + $this->style->getColoursResetCode(), + str_repeat(' ', $this->style->getMargin()) + ); + + $this->borderTopRows = []; + for ($b = 0; $b < $this->style->getBorderTopWidth(); $b++) { + $this->borderTopRows[] = $borderRow; + } + + $this->borderBottomRows = []; + for ($b = 0; $b < $this->style->getBorderBottomWidth(); $b++) { + $this->borderBottomRows[] = $borderRow; + } + } + + public function getBorderTopRows() : array + { + return $this->borderTopRows; + } + + public function getBorderBottomRows() : array + { + return $this->borderBottomRows; + } + /** * Shorthand function to set all borders values at once */ @@ -493,6 +537,7 @@ public function setBorder( } $this->calculateContentWidth(); + $this->generateBorderRows(); return $this; } @@ -501,6 +546,8 @@ public function setBorderTopWidth(int $width) : self { $this->borderTopWidth = $width; + $this->generateBorderRows(); + return $this; } @@ -516,6 +563,8 @@ public function setBorderBottomWidth(int $width) : self { $this->borderBottomWidth = $width; + $this->generateBorderRows(); + return $this; } @@ -531,6 +580,8 @@ public function setBorderColour(string $colour) : self { $this->borderColour = $colour; + $this->generateBorderRows(); + return $this; } From 0b8f8f1e9de8d2d3be45b2a1eaaddef909ae0cc2 Mon Sep 17 00:00:00 2001 From: Lynesth Date: Tue, 8 May 2018 01:15:22 +1100 Subject: [PATCH 24/35] Use pre generated border rows --- src/CliMenu.php | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/src/CliMenu.php b/src/CliMenu.php index 86abb78a..9d4fd04e 100644 --- a/src/CliMenu.php +++ b/src/CliMenu.php @@ -319,22 +319,9 @@ protected function draw() : void $frame = new Frame; $frame->newLine(2); - - $borderRow = [ - sprintf( - "%s%s%s%s%s\n", - str_repeat(' ', $this->style->getMargin()), - $this->style->getBorderColourCode(), - str_repeat(' ', $this->style->getWidth()), - $this->style->getColoursResetCode(), - str_repeat(' ', $this->style->getMargin()) - ) - ]; if ($this->style->getBorderTopWidth() > 0) { - for ($b = 0; $b < $this->style->getBorderTopWidth(); $b++) { - $frame->addRows($borderRow); - } + $frame->addRows($this->style->getBorderTopRows()); } if ($this->title) { @@ -350,9 +337,7 @@ protected function draw() : void $frame->addRows($this->drawMenuItem(new LineBreakItem())); if ($this->style->getBorderBottomWidth() > 0) { - for ($b = 0; $b < $this->style->getBorderBottomWidth(); $b++) { - $frame->addRows($borderRow); - } + $frame->addRows($this->style>getBorderBottomRows()); } $frame->newLine(2); From c1ee5d2d0e1524c0cb14ae0aa81a2e7d772c1fe4 Mon Sep 17 00:00:00 2001 From: Lynesth Date: Tue, 8 May 2018 01:20:33 +1100 Subject: [PATCH 25/35] Fix wrong scope --- src/MenuStyle.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/MenuStyle.php b/src/MenuStyle.php index a810d01e..a802d427 100644 --- a/src/MenuStyle.php +++ b/src/MenuStyle.php @@ -477,11 +477,11 @@ private function generateBorderRows() : void { $borderRow = sprintf( "%s%s%s%s%s\n", - str_repeat(' ', $this->style->getMargin()), - $this->style->getBorderColourCode(), - str_repeat(' ', $this->style->getWidth()), - $this->style->getColoursResetCode(), - str_repeat(' ', $this->style->getMargin()) + str_repeat(' ', $this->margin), + $this->getBorderColourCode(), + str_repeat(' ', $this->width), + $this->coloursResetCode(), + str_repeat(' ', $this->margin) ); $this->borderTopRows = []; From d484e5cdaa0332a92c4670d0334f753a17152cbb Mon Sep 17 00:00:00 2001 From: Lynesth Date: Tue, 8 May 2018 01:33:35 +1100 Subject: [PATCH 26/35] Add 256 colors support for border colour --- src/MenuStyle.php | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/MenuStyle.php b/src/MenuStyle.php index a802d427..3cb6d74b 100644 --- a/src/MenuStyle.php +++ b/src/MenuStyle.php @@ -115,9 +115,9 @@ class MenuStyle private $borderLeftWidth; /** - * @var string + * @var int|string */ - private $borderColour; + private $borderColour = 'white'; /** * @var array @@ -533,7 +533,7 @@ public function setBorder( $this->borderLeftWidth = $leftWidth; if (is_string($colour)) { - $this->borderColour = $colour; + $this->setBorderColour($colour); } $this->calculateContentWidth(); @@ -576,9 +576,17 @@ public function setBorderLeftWidth(int $width) : self return $this; } - public function setBorderColour(string $colour) : self + public function setBorderColour($colour) : self { - $this->borderColour = $colour; + if (is_string($colour) && ctype_digit($colour)) { + $colour = intval($colour); + } + + $this->borderColour = ColourUtil::validateColour( + $this->terminal, + $bg, + $fallback + ); $this->generateBorderRows(); @@ -605,13 +613,19 @@ public function getBorderLeftWidth() : int return $this->borderLeftWidth; } - public function getBorderColour() : string + public function getBorderColour() { return $this->borderColour; } public function getBorderColourCode() : string { - return sprintf("\033[%sm", self::$availableBackgroundColors[$this->getBorderColour()]['set']); + if (is_string($this->borderColour)) { + $borderColourCode = self::$availableBackgroundColors[$this->bg]; + } else { + $borderColourCode = sprintf("48;5;%s", $this->bg); + } + + return sprintf("\033[%sm", $borderColourCode); } } From df9cad4e6c262fc8854c4882c3e80ca89921665b Mon Sep 17 00:00:00 2001 From: Lynesth Date: Tue, 8 May 2018 01:40:55 +1100 Subject: [PATCH 27/35] Many fixes --- src/MenuStyle.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/MenuStyle.php b/src/MenuStyle.php index 3cb6d74b..f13ece1f 100644 --- a/src/MenuStyle.php +++ b/src/MenuStyle.php @@ -480,17 +480,17 @@ private function generateBorderRows() : void str_repeat(' ', $this->margin), $this->getBorderColourCode(), str_repeat(' ', $this->width), - $this->coloursResetCode(), + $this->coloursResetCode, str_repeat(' ', $this->margin) ); $this->borderTopRows = []; - for ($b = 0; $b < $this->style->getBorderTopWidth(); $b++) { + for ($b = 0; $b < $this->borderTopWidth; $b++) { $this->borderTopRows[] = $borderRow; } $this->borderBottomRows = []; - for ($b = 0; $b < $this->style->getBorderBottomWidth(); $b++) { + for ($b = 0; $b < $this->borderBottomWidth; $b++) { $this->borderBottomRows[] = $borderRow; } } @@ -576,7 +576,7 @@ public function setBorderLeftWidth(int $width) : self return $this; } - public function setBorderColour($colour) : self + public function setBorderColour($colour, $fallback = null) : self { if (is_string($colour) && ctype_digit($colour)) { $colour = intval($colour); @@ -584,7 +584,7 @@ public function setBorderColour($colour) : self $this->borderColour = ColourUtil::validateColour( $this->terminal, - $bg, + $colour, $fallback ); From cf795f27637b0a360f4d17c04a257d1c4511dadb Mon Sep 17 00:00:00 2001 From: Lynesth Date: Tue, 8 May 2018 01:41:04 +1100 Subject: [PATCH 28/35] typo --- src/CliMenu.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CliMenu.php b/src/CliMenu.php index 9d4fd04e..04df955a 100644 --- a/src/CliMenu.php +++ b/src/CliMenu.php @@ -337,7 +337,7 @@ protected function draw() : void $frame->addRows($this->drawMenuItem(new LineBreakItem())); if ($this->style->getBorderBottomWidth() > 0) { - $frame->addRows($this->style>getBorderBottomRows()); + $frame->addRows($this->style->getBorderBottomRows()); } $frame->newLine(2); From ae227bc31f75ad5923149f5928fdd13dd17c1d9c Mon Sep 17 00:00:00 2001 From: Lynesth Date: Tue, 8 May 2018 02:16:22 +1100 Subject: [PATCH 29/35] Modify border colour handling (string only) --- src/MenuStyle.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/MenuStyle.php b/src/MenuStyle.php index f13ece1f..d70b5ba2 100644 --- a/src/MenuStyle.php +++ b/src/MenuStyle.php @@ -576,12 +576,8 @@ public function setBorderLeftWidth(int $width) : self return $this; } - public function setBorderColour($colour, $fallback = null) : self + public function setBorderColour(string $colour, $fallback = null) : self { - if (is_string($colour) && ctype_digit($colour)) { - $colour = intval($colour); - } - $this->borderColour = ColourUtil::validateColour( $this->terminal, $colour, @@ -613,14 +609,14 @@ public function getBorderLeftWidth() : int return $this->borderLeftWidth; } - public function getBorderColour() + public function getBorderColour() : string { return $this->borderColour; } public function getBorderColourCode() : string { - if (is_string($this->borderColour)) { + if (ctype_digit($this->borderColour)) { $borderColourCode = self::$availableBackgroundColors[$this->bg]; } else { $borderColourCode = sprintf("48;5;%s", $this->bg); From 00a84fe05893eec3a53519b2d0513e0272384c44 Mon Sep 17 00:00:00 2001 From: Lynesth Date: Tue, 8 May 2018 02:20:14 +1100 Subject: [PATCH 30/35] typehint fix --- src/MenuStyle.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MenuStyle.php b/src/MenuStyle.php index d70b5ba2..1398193b 100644 --- a/src/MenuStyle.php +++ b/src/MenuStyle.php @@ -115,7 +115,7 @@ class MenuStyle private $borderLeftWidth; /** - * @var int|string + * @var string */ private $borderColour = 'white'; From 87df3da3df8625db41454009362f5b03c7ba5bb2 Mon Sep 17 00:00:00 2001 From: Lynesth Date: Tue, 8 May 2018 02:45:11 +1100 Subject: [PATCH 31/35] throw exception is colour isn't string or null --- src/CliMenuBuilder.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/CliMenuBuilder.php b/src/CliMenuBuilder.php index 434821dd..4fd90371 100644 --- a/src/CliMenuBuilder.php +++ b/src/CliMenuBuilder.php @@ -306,6 +306,8 @@ public function setBorder( if (is_string($colour)) { $this->style['borderColour'] = $colour; + } elseif ($colour !== null) { + throw new \InvalidArgumentException('Invalid colour'); } return $this; From 7a8590a340e8bb645a778c5ca5d9f0b5589d6515 Mon Sep 17 00:00:00 2001 From: Lynesth Date: Tue, 8 May 2018 02:53:01 +1100 Subject: [PATCH 32/35] refactoring --- src/MenuStyle.php | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/MenuStyle.php b/src/MenuStyle.php index 1398193b..061ee00e 100644 --- a/src/MenuStyle.php +++ b/src/MenuStyle.php @@ -484,15 +484,8 @@ private function generateBorderRows() : void str_repeat(' ', $this->margin) ); - $this->borderTopRows = []; - for ($b = 0; $b < $this->borderTopWidth; $b++) { - $this->borderTopRows[] = $borderRow; - } - - $this->borderBottomRows = []; - for ($b = 0; $b < $this->borderBottomWidth; $b++) { - $this->borderBottomRows[] = $borderRow; - } + $this->borderTopRows = array_fill(0, $this->borderTopWidth, $borderRow) + $this->borderBottomRows = array_fill(0, $this->borderBottomWidth, $borderRow) } public function getBorderTopRows() : array @@ -534,6 +527,8 @@ public function setBorder( if (is_string($colour)) { $this->setBorderColour($colour); + } elseif ($colour !== null) { + throw new \InvalidArgumentException('Invalid colour'); } $this->calculateContentWidth(); From c4716265219f26ed05476cc30c6cde522e0ec798 Mon Sep 17 00:00:00 2001 From: Lynesth Date: Tue, 8 May 2018 02:58:35 +1100 Subject: [PATCH 33/35] missing ; --- src/MenuStyle.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MenuStyle.php b/src/MenuStyle.php index 061ee00e..d48b3ac1 100644 --- a/src/MenuStyle.php +++ b/src/MenuStyle.php @@ -484,8 +484,8 @@ private function generateBorderRows() : void str_repeat(' ', $this->margin) ); - $this->borderTopRows = array_fill(0, $this->borderTopWidth, $borderRow) - $this->borderBottomRows = array_fill(0, $this->borderBottomWidth, $borderRow) + $this->borderTopRows = array_fill(0, $this->borderTopWidth, $borderRow); + $this->borderBottomRows = array_fill(0, $this->borderBottomWidth, $borderRow); } public function getBorderTopRows() : array From d645c73991ce079ae4da121d6d3ab159c132beea Mon Sep 17 00:00:00 2001 From: Lynesth Date: Tue, 8 May 2018 03:31:36 +1100 Subject: [PATCH 34/35] Fixes ! --- src/MenuStyle.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/MenuStyle.php b/src/MenuStyle.php index d48b3ac1..2adee6a0 100644 --- a/src/MenuStyle.php +++ b/src/MenuStyle.php @@ -380,7 +380,9 @@ public function setMarginAuto() : self { $this->marginAuto = true; $this->margin = floor(($this->terminal->getWidth() - $this->width) / 2); - + + $this->generateBorderRows(); + return $this; } @@ -389,6 +391,8 @@ public function setMargin(int $margin) : self $this->marginAuto = false; $this->margin = $margin; + $this->generateBorderRows(); + return $this; } @@ -611,10 +615,10 @@ public function getBorderColour() : string public function getBorderColourCode() : string { - if (ctype_digit($this->borderColour)) { - $borderColourCode = self::$availableBackgroundColors[$this->bg]; + if (!ctype_digit($this->borderColour)) { + $borderColourCode = self::$availableBackgroundColors[$this->borderColour]; } else { - $borderColourCode = sprintf("48;5;%s", $this->bg); + $borderColourCode = sprintf("48;5;%s", $this->borderColour); } return sprintf("\033[%sm", $borderColourCode); From cfa7953d770738d98061767ee3917fac4dff530e Mon Sep 17 00:00:00 2001 From: Lynesth Date: Tue, 8 May 2018 03:33:33 +1100 Subject: [PATCH 35/35] Fix "wonky" selected item --- src/CliMenu.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/CliMenu.php b/src/CliMenu.php index 04df955a..420292ed 100644 --- a/src/CliMenu.php +++ b/src/CliMenu.php @@ -375,7 +375,7 @@ protected function drawMenuItem(MenuItemInterface $item, bool $selected = false) return array_map(function ($row) use ($setColour, $invertedColour, $resetColour, $borderColour) { return sprintf( - "%s%s%s%s%s%s%s%s%s%s%s%s\n", + "%s%s%s%s%s%s%s%s%s%s%s%s%s\n", str_repeat(' ', $this->style->getMargin()), $borderColour, str_repeat(' ', $this->style->getBorderLeftWidth()), @@ -384,6 +384,7 @@ protected function drawMenuItem(MenuItemInterface $item, bool $selected = false) str_repeat(' ', $this->style->getPadding()), $row, str_repeat(' ', $this->style->getRightHandPadding(mb_strlen(s::stripAnsiEscapeSequence($row)))), + $this->style->getInvertedColoursUnsetCode(), $borderColour, str_repeat(' ', $this->style->getBorderRightWidth()), $resetColour,