-
Notifications
You must be signed in to change notification settings - Fork 106
Add border styling #100
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
Add border styling #100
Changes from 31 commits
bd16029
b94fdb1
3bf82d3
4645481
5b9c9f6
8ad863f
5209f8c
796a49d
0d83512
7eb1520
763619b
07b4e0c
17164ed
42a0206
a49d87d
5a23153
f435c44
ff3b1e8
ce87d98
9c0cac9
752fa64
ce5ba31
78affc2
da7a7f7
0b8f8f1
c1ee5d2
d484e5c
df9cad4
cf795f2
ae227bc
00a84fe
87df3da
7a8590a
c471626
d645c73
cfa7953
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -280,6 +280,37 @@ public function setTitleSeparator(string $separator) : self | |
return $this; | ||
} | ||
|
||
public function setBorder( | ||
int $topWidth, | ||
$rightWidth = null, | ||
$bottomWidth = null, | ||
$leftWidth = null, | ||
string $colour = null | ||
) : self { | ||
if (!is_int($rightWidth)) { | ||
$colour = $rightWidth; | ||
$rightWidth = $bottomWidth = $leftWidth = $topWidth; | ||
} elseif (!is_int($bottomWidth)) { | ||
$colour = $bottomWidth; | ||
$bottomWidth = $topWidth; | ||
$leftWidth = $rightWidth; | ||
} elseif (!is_int($leftWidth)) { | ||
$colour = $leftWidth; | ||
$leftWidth = $rightWidth; | ||
} | ||
|
||
$this->style['borderTopWidth'] = $topWidth; | ||
$this->style['borderRightWidth'] = $rightWidth; | ||
$this->style['borderBottomWidth'] = $bottomWidth; | ||
$this->style['borderLeftWidth'] = $leftWidth; | ||
|
||
if (is_string($colour)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we throw an exception if colour is not null and not a string? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
$this->style['borderColour'] = $colour; | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public function setTerminal(Terminal $terminal) : self | ||
{ | ||
$this->terminal = $terminal; | ||
|
@@ -344,7 +375,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']); | ||
|
||
$this->style['marginAuto'] ? $style->setMarginAuto() : $style->setMargin($this->style['margin']); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,6 +94,41 @@ class MenuStyle | |
*/ | ||
private $coloursResetCode = "\033[0m"; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $borderTopWidth; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $borderRightWidth; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $borderBottomWidth; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $borderLeftWidth; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $borderColour = 'white'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can probably drop this assignment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I drop it I get an error in generateBorderRows when the border colour isn't set. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm - It works fine for me as |
||
|
||
/** | ||
* @var array | ||
*/ | ||
private $borderTopRows = []; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $borderBottomRows = []; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
|
@@ -115,6 +150,11 @@ class MenuStyle | |
'itemExtra' => '✔', | ||
'displaysExtra' => false, | ||
'titleSeparator' => '=', | ||
'borderTopWidth' => 0, | ||
'borderRightWidth' => 0, | ||
'borderBottomWidth' => 0, | ||
'borderLeftWidth' => 0, | ||
'borderColour' => 'white', | ||
'marginAuto' => false, | ||
]; | ||
|
||
|
@@ -182,6 +222,11 @@ 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 function getDisabledItemText(string $text) : string | ||
|
@@ -251,7 +296,9 @@ public function getColoursResetCode() : string | |
*/ | ||
protected function calculateContentWidth() : void | ||
{ | ||
$this->contentWidth = $this->width - ($this->padding * 2); | ||
$this->contentWidth = $this->width | ||
- ($this->padding * 2) | ||
- ($this->borderRightWidth + $this->borderLeftWidth); | ||
} | ||
|
||
public function getFg() | ||
|
@@ -303,7 +350,9 @@ public function setWidth(int $width) : self | |
if ($this->marginAuto) { | ||
$this->setMarginAuto(); | ||
} | ||
|
||
$this->calculateContentWidth(); | ||
$this->generateBorderRows(); | ||
|
||
return $this; | ||
} | ||
|
@@ -423,4 +472,156 @@ public function setTitleSeparator(string $actionSeparator) : self | |
|
||
return $this; | ||
} | ||
|
||
private function generateBorderRows() : void | ||
{ | ||
$borderRow = sprintf( | ||
"%s%s%s%s%s\n", | ||
str_repeat(' ', $this->margin), | ||
$this->getBorderColourCode(), | ||
str_repeat(' ', $this->width), | ||
$this->coloursResetCode, | ||
str_repeat(' ', $this->margin) | ||
); | ||
|
||
$this->borderTopRows = []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could do something like the following if you wanted: $this->borderTopRows = array_fill(0, $this->borderTopWidth, $borderRow) Your call, I don't mind :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep I'll do that good call |
||
for ($b = 0; $b < $this->borderTopWidth; $b++) { | ||
$this->borderTopRows[] = $borderRow; | ||
} | ||
|
||
$this->borderBottomRows = []; | ||
for ($b = 0; $b < $this->borderBottomWidth; $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 | ||
*/ | ||
public function setBorder( | ||
int $topWidth, | ||
$rightWidth = null, | ||
$bottomWidth = null, | ||
$leftWidth = null, | ||
string $colour = null | ||
) : self { | ||
if (!is_int($rightWidth)) { | ||
$colour = $rightWidth; | ||
$rightWidth = $bottomWidth = $leftWidth = $topWidth; | ||
} elseif (!is_int($bottomWidth)) { | ||
$colour = $bottomWidth; | ||
$bottomWidth = $topWidth; | ||
$leftWidth = $rightWidth; | ||
} elseif (!is_int($leftWidth)) { | ||
$colour = $leftWidth; | ||
$leftWidth = $rightWidth; | ||
} | ||
|
||
$this->borderTopWidth = $topWidth; | ||
$this->borderRightWidth = $rightWidth; | ||
$this->borderBottomWidth = $bottomWidth; | ||
$this->borderLeftWidth = $leftWidth; | ||
|
||
if (is_string($colour)) { | ||
$this->setBorderColour($colour); | ||
} | ||
|
||
$this->calculateContentWidth(); | ||
$this->generateBorderRows(); | ||
|
||
return $this; | ||
} | ||
|
||
public function setBorderTopWidth(int $width) : self | ||
{ | ||
$this->borderTopWidth = $width; | ||
|
||
$this->generateBorderRows(); | ||
|
||
return $this; | ||
} | ||
|
||
public function setBorderRightWidth(int $width) : self | ||
{ | ||
$this->borderRightWidth = $width; | ||
$this->calculateContentWidth(); | ||
|
||
return $this; | ||
} | ||
|
||
public function setBorderBottomWidth(int $width) : self | ||
{ | ||
$this->borderBottomWidth = $width; | ||
|
||
$this->generateBorderRows(); | ||
|
||
return $this; | ||
} | ||
|
||
public function setBorderLeftWidth(int $width) : self | ||
{ | ||
$this->borderLeftWidth = $width; | ||
$this->calculateContentWidth(); | ||
|
||
return $this; | ||
} | ||
|
||
public function setBorderColour(string $colour, $fallback = null) : self | ||
{ | ||
$this->borderColour = ColourUtil::validateColour( | ||
$this->terminal, | ||
$colour, | ||
$fallback | ||
); | ||
|
||
$this->generateBorderRows(); | ||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a new line between each of these methods |
||
{ | ||
if (ctype_digit($this->borderColour)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this if statement needs to be negated |
||
$borderColourCode = self::$availableBackgroundColors[$this->bg]; | ||
} else { | ||
$borderColourCode = sprintf("48;5;%s", $this->bg); | ||
} | ||
|
||
return sprintf("\033[%sm", $borderColourCode); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we're missing the MenuStyle configuring which should be done in
buildStyle()