Skip to content

Commit

Permalink
Passing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inxilpro committed Apr 13, 2024
1 parent b871453 commit f732fc0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
5 changes: 5 additions & 0 deletions src/AnsiChar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
31 changes: 22 additions & 9 deletions src/AnsiString.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class AnsiString implements Stringable
/** @var \Illuminate\Support\Collection<int,\Glhd\AnsiPants\AnsiChar> */
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) {
Expand All @@ -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);
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tests/Unit/AnsiStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit f732fc0

Please sign in to comment.