Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
30 changes: 30 additions & 0 deletions src/System/Console/Interfaces/Border.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace System\Console\Interfaces;

use System\Console\Style\Color\ForegroundColor;

interface Border
{
public function color(): ForegroundColor;

public function top(): bool;

public function right(): bool;

public function bottom(): bool;

public function left(): bool;

// extend

public function topLeft(): bool;

public function topRight(): bool;

public function bottomRight(): bool;

public function bottomLeft(): bool;

public function corner(): array;
}
16 changes: 16 additions & 0 deletions src/System/Console/Interfaces/BoxSize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace System\Console\Interfaces;

use System\Console\ValueObjects\Direction;

interface BoxSize
{
public function width(): int;

public function height(): int;

public function margin(): Direction;

public function padding(): Direction;
}
220 changes: 220 additions & 0 deletions src/System/Console/Style/Box.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
<?php

declare(strict_types=1);

namespace System\Console\Style;

use System\Console\Interfaces\Border;
use System\Console\Interfaces\BoxSize;
use System\Text\Str;

class Box
{
public const LEFT = 0;
public const RIGHT = 1;
public const CENTER = 2;

private string $text;
private BoxSize $boxSize;
private Border $border;
private int $left = 0;
private int $alignment = 0;

public function __construct(
string $text,
BoxSize $boxSize,
Border $border
) {
$this->text = $text;
$this->boxSize = $boxSize;
$this->border = $border;
}

public function left(int $left): self
{
$this->left = $left;

return $this;
}

public function textAlignment(int $alignment): self
{
$this->alignment = $alignment;

return $this;
}

public function render(): Style
{
$box_style = new Style();
foreach ($this->renderLines() as $style) {
// var_dump($style);
$box_style->tap($style);
}

return $box_style;
}

/**
* Order
* - margin top
* - border
* - padding.
*
* @return Style[]
*/
public function renderLines(): array
{
$margin_top = new Style();
$border_top = new Style();
$content = new Style();
$border_bottom = new Style();

// render margin top
$margin = $this->boxSize->margin()->top();
foreach (range(1, $margin) as $m_top) {
$margin_top
->repeat(' ', $this->left)
->repeat(' ', $this->boxSize->margin()->left())
->repeat(' ', $this->boxSize->width())
->new_lines()
;
}

// render border
$border_top
->repeat(' ', $this->left)
->repeat(' ', $this->boxSize->margin()->left())
->push($this->border->corner()[3])
->textColor($this->border->color())
->repeat('─', $this->boxSize->width() + $this->boxSize->padding()->left() + $this->boxSize->padding()->right())
->textColor($this->border->color())

->push($this->border->corner()[0])
->textColor($this->border->color())
->repeat(' ', $this->boxSize->margin()->right())
->new_lines()
;

// content
foreach (range(0, $this->boxSize->padding()->top()) as $p) {
$content
->repeat(' ', $this->left)
->repeat(' ', $this->boxSize->margin()->left())
->push('│')
->textColor($this->border->color())
->repeat(' ', $this->boxSize->width() + $this->boxSize->padding()->left() + $this->boxSize->padding()->right())
->push('│')
->textColor($this->border->color())
->new_lines()
;
}
foreach ($this->alignment($this->text, $this->alignment, $this->boxSize->width()) as $key => $value) {
if ($key + 1 > $this->boxSize->height()) {
$value = trim($value) . '...';
$value = $value . str_repeat(' ', $this->boxSize->width() - strlen($value));
}
$content
->repeat(' ', $this->left)
->repeat(' ', $this->boxSize->margin()->left())
->push('│')
->textColor($this->border->color())
->repeat(' ', $this->boxSize->padding()->left())
->push($value)
->repeat(' ', $this->boxSize->padding()->right())
->push('│')
->textColor($this->border->color())
->repeat(' ', $this->boxSize->margin()->right())
->new_lines()
;
if ($key + 1 > $this->boxSize->height()) {
break;
}
}
foreach (range(0, $this->boxSize->padding()->bottom()) as $p) {
$content
->repeat(' ', $this->left)
->repeat(' ', $this->boxSize->margin()->left())
->push('│')
->textColor($this->border->color())
->repeat(' ', $this->boxSize->width() + $this->boxSize->padding()->left() + $this->boxSize->padding()->right())
->push('│')
->textColor($this->border->color())
->new_lines()
;
}

// bottom
$border_bottom
->repeat(' ', $this->left)
->repeat(' ', $this->boxSize->margin()->left())
->push($this->border->corner()[2])
->textColor($this->border->color())
->repeat('─', $this->boxSize->width() + $this->boxSize->padding()->left() + $this->boxSize->padding()->right())
->textColor($this->border->color())
->push($this->border->corner()[1])
->textColor($this->border->color())
->repeat(' ', $this->boxSize->margin()->right())
->new_lines()
;

return [
'margin_top' => $margin_top,
'border_top' => $border_top,
'content' => $content,
'border_bottom' => $border_bottom,
'margin_bottom' => $margin_top,
];
}

private function alignment(string $text, int $alignment, int $width)
{
$words = explode(' ', $text);
$lines = [];
$line = 0;
while (count($words) > 0) {
if (!array_key_exists($line, $lines)) {
$lines[$line] = array_shift($words);
continue;
}
$word = array_shift($words);
$lenght = strlen($word) + strlen($lines[$line]) + 1;
if ($lenght < $width) {
$lines[$line] .= ' ' . $word;
continue;
}
$line++;
$lines[$line] = $word;
}

if ($alignment === static::RIGHT) {
return array_map(
fn ($value) => Str::fill($value, ' ', $width),
$lines
);
}

if ($alignment === static::CENTER) {
return array_map(
// fn($value) => Str::fill($value, ' ', $width),
function ($value) use ($width) {
foreach (range(1, $width - strlen($value)) as $q) {
if ($q % 2 === 0) {
$value = ' ' . $value;
} else {
$value .= ' ';
}
}

return $value;
},
$lines
);
}

return array_map(
fn ($value) => Str::fillEnd($value, ' ', $width),
$lines
);
}
}
101 changes: 101 additions & 0 deletions src/System/Console/Style/Layout/Border.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

declare(strict_types=1);

namespace System\Console\Style\Layout;

use System\Console\Interfaces\Border as InterfacesBorder;
use System\Console\Style\Color\ForegroundColor;
use System\Console\Style\Colors;

class Border implements InterfacesBorder
{
private ForegroundColor $color;
private bool $top;
private bool $right;
private bool $bottom;
private bool $left;

/** @var string[] */
private array $corner = ['╮', '╯', '╰', '╭'];

public function __construct(
ForegroundColor $color,
bool $top = true,
bool $right = true,
bool $bottom = true,
bool $left = true
) {
$this->color = $color ?? Colors::hexText('#ffffff');
$this->top = $top;
$this->right = $right;
$this->bottom = $bottom;
$this->left = $left;
}

public function color(): ForegroundColor
{
return $this->color;
}

public function top(): bool
{
return $this->top;
}

public function right(): bool
{
return $this->right;
}

public function bottom(): bool
{
return $this->bottom;
}

public function left(): bool
{
return $this->left;
}

public function topLeft(): bool
{
return $this->top() && $this->left();
}

public function topRight(): bool
{
return $this->top() && $this->right();
}

public function bottomRight(): bool
{
return $this->bottom() && $this->right();
}

public function bottomLeft(): bool
{
return $this->bottom() && $this->left();
}

/**
* order:
* 0: top-right
* 1: bottom-right
* 2: bottom-left
* 3: top-left.
*
* @param string[] $corner
*/
public function cornerStyle(array $corner): self
{
$this->corner = $corner;

return $this;
}

public function corner(): array
{
return $this->corner;
}
}
Loading