Skip to content

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

Merged
merged 36 commits into from
May 7, 2018
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
bd16029
Add border styling
Lynesth May 4, 2018
b94fdb1
Add border styling
Lynesth May 4, 2018
3bf82d3
Fix setBorder() to work like CSS border
Lynesth May 4, 2018
4645481
Add setBorder() to CliMenuBuilder
Lynesth May 4, 2018
5b9c9f6
Fix PSR2
Lynesth May 4, 2018
8ad863f
Fix PSR2
Lynesth May 4, 2018
5209f8c
Should fix the tests
Lynesth May 4, 2018
796a49d
Fix setBorder
Lynesth May 4, 2018
0d83512
Missing newlines
Lynesth May 4, 2018
7eb1520
Missing configuration in buildStyle()
Lynesth May 4, 2018
763619b
Add MenuStyle border tests
Lynesth May 4, 2018
07b4e0c
Fix failing logic...
Lynesth May 4, 2018
17164ed
Fix failing logic...
Lynesth May 4, 2018
42a0206
Fixing some more
Lynesth May 4, 2018
a49d87d
Some more fixes
Lynesth May 4, 2018
5a23153
Adding border to width and padding tests
Lynesth May 4, 2018
f435c44
Recalculate content width after setting borders
Lynesth May 5, 2018
ff3b1e8
Add Builder border tests
Lynesth May 5, 2018
ce87d98
Max coverage for setBorder tests in Builder
Lynesth May 5, 2018
9c0cac9
Fix typo
Lynesth May 5, 2018
752fa64
Add borders to custom-styles example
Lynesth May 5, 2018
ce5ba31
Merge branch 'master' into patch-14
Lynesth May 7, 2018
78affc2
Better borderRows generation
Lynesth May 7, 2018
da7a7f7
Pre generate border top and bottom rows
Lynesth May 7, 2018
0b8f8f1
Use pre generated border rows
Lynesth May 7, 2018
c1ee5d2
Fix wrong scope
Lynesth May 7, 2018
d484e5c
Add 256 colors support for border colour
Lynesth May 7, 2018
df9cad4
Many fixes
Lynesth May 7, 2018
cf795f2
typo
Lynesth May 7, 2018
ae227bc
Modify border colour handling (string only)
Lynesth May 7, 2018
00a84fe
typehint fix
Lynesth May 7, 2018
87df3da
throw exception is colour isn't string or null
Lynesth May 7, 2018
7a8590a
refactoring
Lynesth May 7, 2018
c471626
missing ;
Lynesth May 7, 2018
d645c73
Fixes !
Lynesth May 7, 2018
cfa7953
Fix "wonky" selected item
Lynesth May 7, 2018
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
1 change: 1 addition & 0 deletions examples/custom-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
->setForegroundColour('black')
->setPadding(4)
->setMargin(4)
->setBorder(1, 2, 'red')
->setUnselectedMarker(' ')
->setSelectedMarker('>')
->setTitleSeparator('- ')
Expand Down
22 changes: 20 additions & 2 deletions src/CliMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@ protected function draw() : void

$frame->newLine(2);

if ($this->style->getBorderTopWidth() > 0) {
$frame->addRows($this->style->getBorderTopRows());
}

if ($this->title) {
$frame->addRows($this->drawMenuItem(new LineBreakItem()));
$frame->addRows($this->drawMenuItem(new StaticItem($this->title)));
Expand All @@ -331,6 +335,10 @@ protected function draw() : void
}, $this->items, array_keys($this->items));

$frame->addRows($this->drawMenuItem(new LineBreakItem()));

if ($this->style->getBorderBottomWidth() > 0) {
$frame->addRows($this->style->getBorderBottomRows());
}

$frame->newLine(2);

Expand Down Expand Up @@ -359,15 +367,25 @@ protected function drawMenuItem(MenuItemInterface $item, bool $selected = false)
? $this->style->getInvertedColoursSetCode()
: '';

return array_map(function ($row) use ($setColour, $invertedColour, $resetColour) {
if ($this->style->getBorderLeftWidth() || $this->style->getBorderRightWidth()) {
$borderColour = $this->style->getBorderColourCode();
} else {
$borderColour = '';
}

return array_map(function ($row) use ($setColour, $invertedColour, $resetColour, $borderColour) {
return sprintf(
"%s%s%s%s%s%s%s%s\n",
"%s%s%s%s%s%s%s%s%s%s%s%s\n",
str_repeat(' ', $this->style->getMargin()),
$borderColour,
str_repeat(' ', $this->style->getBorderLeftWidth()),
$setColour,
$invertedColour,
str_repeat(' ', $this->style->getPadding()),
$row,
str_repeat(' ', $this->style->getRightHandPadding(mb_strlen(s::stripAnsiEscapeSequence($row)))),
$borderColour,
str_repeat(' ', $this->style->getBorderRightWidth()),
$resetColour,
str_repeat(' ', $this->style->getMargin())
);
Expand Down
38 changes: 37 additions & 1 deletion src/CliMenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,37 @@ public function setTitleSeparator(string $separator) : self
return $this;
}

public function setBorder(
Copy link
Member

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()

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)) {
Copy link
Member

Choose a reason for hiding this comment

The 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? validateColour will check the colour later but not if someone does something weird like pass an object to it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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;
Expand Down Expand Up @@ -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']);

Expand Down
203 changes: 202 additions & 1 deletion src/MenuStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can probably drop this assignment.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.
Juste like the issue you had with bg and fg colours earlier.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm - It works fine for me as private $borderColour. It is set in the constructor from $defaultStyleValues


/**
* @var array
*/
private $borderTopRows = [];

/**
* @var array
*/
private $borderBottomRows = [];

/**
* @var bool
*/
Expand All @@ -115,6 +150,11 @@ class MenuStyle
'itemExtra' => '✔',
'displaysExtra' => false,
'titleSeparator' => '=',
'borderTopWidth' => 0,
'borderRightWidth' => 0,
'borderBottomWidth' => 0,
'borderLeftWidth' => 0,
'borderColour' => 'white',
'marginAuto' => false,
];

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -303,7 +350,9 @@ public function setWidth(int $width) : self
if ($this->marginAuto) {
$this->setMarginAuto();
}

$this->calculateContentWidth();
$this->generateBorderRows();

return $this;
}
Expand Down Expand Up @@ -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 = [];
Copy link
Member

Choose a reason for hiding this comment

The 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 :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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)) {
Copy link
Member

Choose a reason for hiding this comment

The 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);
}
}
Loading