Skip to content
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 support for borders #178

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
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
62 changes: 62 additions & 0 deletions src/Actions/ApplyBorder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace Termwind\Actions;

use Termwind\Exceptions\StyleNotFound;
use Termwind\Repositories\Styles as StyleRepository;
use Termwind\Terminal;
use Termwind\ValueObjects\Styles;

/**
* @internal
*/
final class ApplyBorder
{
private const BORDER_SQUARE = [
'tl' => '┌',
'tr' => '┐',
'bl' => '└',
'br' => '┘',
];

private const BORDER_ROUNDED = [
'tl' => '╭',
'tr' => '╮',
'bl' => '╰',
'br' => '╯',
];

// Todo support text alignment

public static function format(string $text, int $padding = 2, bool $rounded = true): string
{
$lines = explode("\n", $text);

$width = max(array_map('strlen', $lines)) + ($padding * 2) + 2;

$borderStyle = $rounded ? self::BORDER_ROUNDED : self::BORDER_SQUARE;
$header = $borderStyle['tl'] . str_repeat('─', $width) . $borderStyle['tr'];
$footer = $borderStyle['bl'] . str_repeat('─', $width) . $borderStyle['br'];

$lines = array_map(function (string $line) use ($width): string {
return self::formatLine($line, $width - 2);
}, $lines);

$verticalPadding = (int) round($padding / 2);
if ($verticalPadding > 0) {
$lines = array_merge(array_fill(0, $verticalPadding, self::formatLine('', $width - 2)), $lines);
$lines = array_merge($lines, array_fill(0, $verticalPadding, self::formatLine('', $width - 2)));
}

return implode("\n", array_merge([$header], $lines, [$footer]));
}

private static function formatLine(string $line, int $width): string
{
$line = str_pad($line, $width, ' ', STR_PAD_BOTH);

return '│ ' . $line . ' │';
}
}
13 changes: 13 additions & 0 deletions src/ValueObjects/Styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Termwind\ValueObjects;

use Closure;
use Termwind\Actions\ApplyBorder;
use Termwind\Actions\StyleToMethod;
use Termwind\Components\Element;
use Termwind\Components\Hr;
Expand Down Expand Up @@ -166,6 +167,18 @@ final public function bg(string $color, int $variant = 0): self
]]);
}

/**
* Adds a border to the element.
*/
final public function border(string $color, int $variant = 0): self
{
$this->styleModifiers[__METHOD__] = static fn ($text): string => ApplyBorder::format($text);

return $this->with(['colors' => [
'border' => $this->getColorVariant($color, $variant),
]]);
}

/**
* Adds a bold style to the element.
*/
Expand Down
17 changes: 17 additions & 0 deletions tests/border.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

test('bordered', function () {
$html = parse(<<<'HTML'
<div class="border-black">
Hello World!
</div>
HTML);

expect($html)->toBe(<<<'TEXT'
╭──────────────────╮
│ │
│ Hello World! │
│ │
╰──────────────────╯
TEXT);
});