From f732fc08f079b7cf2f4575dbbdc07cdd87b54d25 Mon Sep 17 00:00:00 2001 From: Chris Morrell Date: Sat, 13 Apr 2024 00:56:32 -0400 Subject: [PATCH] Passing tests --- src/AnsiChar.php | 5 +++++ src/AnsiString.php | 31 ++++++++++++++++++++++--------- tests/Unit/AnsiStringTest.php | 8 ++++++++ 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/AnsiChar.php b/src/AnsiChar.php index ed52770..6a02b5b 100644 --- a/src/AnsiChar.php +++ b/src/AnsiChar.php @@ -11,6 +11,11 @@ public function __construct( ) { } + public function withFlags(Flag ...$flags): static + { + return new static($this->value, $flags); + } + public function hasFlag(Flag $flag): bool { return in_array($flag, $this->flags, true); diff --git a/src/AnsiString.php b/src/AnsiString.php index 2d2f019..76ccea8 100644 --- a/src/AnsiString.php +++ b/src/AnsiString.php @@ -13,6 +13,11 @@ class AnsiString implements Stringable /** @var \Illuminate\Support\Collection */ protected Collection $chars; + public static function make(AnsiString|Collection|string $input): static + { + return new static($input); + } + public function __construct(AnsiString|Collection|string $input) { if ($input instanceof Collection) { @@ -25,6 +30,11 @@ public function __construct(AnsiString|Collection|string $input) } } + public function withFlags(Flag ...$flags): static + { + return new static($this->chars->map(fn(AnsiChar $char) => $char->withFlags(...$flags))); + } + public function prepend(AnsiString|string $string): static { $string = new AnsiString($string); @@ -43,31 +53,34 @@ public function padLeft(int $length, AnsiString|string $pad = ' '): static { $short = max(0, $length - $this->length()); - // FIXME: We need to grab the flags from the first char and apply unless $pad is already ANSI + $padding = static::make(mb_substr(str_repeat($pad, $short), 0, $short)) + ->withFlags(...$this->chars->first()->flags); - return $this->prepend(mb_substr(str_repeat($pad, $short), 0, $short)); + return $this->prepend($padding); } public function padRight(int $length, AnsiString|string $pad = ' '): static { $short = max(0, $length - $this->length()); - // FIXME: We need to grab the flags from the last char and apply unless $pad is already ANSI + $padding = static::make(mb_substr(str_repeat($pad, $short), 0, $short)) + ->withFlags(...$this->chars->last()->flags); - return $this->append(mb_substr(str_repeat($pad, $short), 0, $short)); + return $this->append($padding); } public function padBoth(int $length, AnsiString|string $pad = ' '): static { $short = max(0, $length - $this->length()); - $left = floor($short / 2); - $right = ceil($short / 2); + $left_padding = static::make(mb_substr(str_repeat($pad, floor($short / 2)), 0, $short)) + ->withFlags(...$this->chars->first()->flags); - // FIXME: We need to grab the flags from the first + last char and apply unless $pad is already ANSI + $right_padding = static::make(mb_substr(str_repeat($pad, ceil($short / 2)), 0, $short)) + ->withFlags(...$this->chars->last()->flags); return $this - ->prepend(mb_substr(str_repeat($pad, $left), 0, $short)) - ->append(mb_substr(str_repeat($pad, $right), 0, $short)); + ->prepend($left_padding) + ->append($right_padding); } public function length(): int diff --git a/tests/Unit/AnsiStringTest.php b/tests/Unit/AnsiStringTest.php index 31da87f..41772f8 100644 --- a/tests/Unit/AnsiStringTest.php +++ b/tests/Unit/AnsiStringTest.php @@ -30,4 +30,12 @@ public function test_pad_right(): void $this->assertEquals($expected, (string) $parsed->padRight(15)); } + + public function test_pad_both(): void + { + $parsed = new AnsiString("\e[1mHello \e[0mworld"); + $expected = "\e[1m Hello \e[0mworld "; + + $this->assertEquals($expected, (string) $parsed->padBoth(15)); + } }