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 2 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
37 changes: 35 additions & 2 deletions src/CliMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,27 @@ protected function draw() : void
$frame = new Frame;

$frame->newLine(2);

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Maybe the $borderRow could be stored in the style so that it doesn't need to be recomputed each time but just when one of the values change, I'm not sure.

Copy link
Member

Choose a reason for hiding this comment

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

Doesn't sound like a crazy idea

$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()));
Expand All @@ -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);

Expand Down Expand Up @@ -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())
);
Expand Down
110 changes: 108 additions & 2 deletions src/MenuStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
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

{
return sprintf("\033[%sm", self::$availableBackgroundColors[$this->getBorderColour()]['set']);
}
}