Skip to content

Commit

Permalink
padding wip
Browse files Browse the repository at this point in the history
  • Loading branch information
inxilpro committed Apr 13, 2024
1 parent eeceee9 commit c3b38de
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
63 changes: 60 additions & 3 deletions src/AnsiString.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,66 @@ class AnsiString implements Stringable
/** @var \Illuminate\Support\Collection<int,\Glhd\AnsiPants\AnsiChar> */
protected Collection $chars;

public function __construct(string $input)
public function __construct(AnsiString|Collection|string $input)
{
$this->chars = $this->parse($input);
if ($input instanceof Collection) {
$input->ensure(AnsiChar::class);
$this->chars = clone $input;
} elseif ($input instanceof AnsiString) {
$this->chars = clone $input->chars;
} else {
$this->chars = $this->parse($input);
}
}

public function prepend(AnsiString|string $string): static
{
$string = new AnsiString($string);

return new AnsiString($string->chars->merge($this->chars));
}

public function append(AnsiString|string $string): static
{
$string = new AnsiString($string);

return new AnsiString($this->chars->merge($string->chars));
}

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

return $this->prepend(mb_substr(str_repeat($pad, $short), 0, $short));
}

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

return $this->append(mb_substr(str_repeat($pad, $short), 0, $short));
}

public function padBoth(int $length, AnsiString|string $pad = ' '): static
{
$short = max(0, $length - $this->length());
$left = floor($short / 2);
$right = ceil($short / 2);

// FIXME: We need to grab the flags from the first + last char and apply unless $pad is already ANSI

return $this
->prepend(mb_substr(str_repeat($pad, $left), 0, $short))
->append(mb_substr(str_repeat($pad, $right), 0, $short));
}

public function length(): int
{
return count($this->chars);
}

public function __toString(): string
Expand All @@ -27,7 +84,7 @@ public function __toString(): string
[$remove, $add] = $this->diffFlags($active_flags, $char->flags);

// If it's simpler, just reset and then add them all
if (count($remove) > count($char->flags)) {
if (count($remove) >= count($char->flags)) {
[$remove, $add] = [[Flag::Reset], $char->flags];
}

Expand Down
16 changes: 16 additions & 0 deletions tests/Unit/AnsiStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,20 @@ public function test_it_can_be_instantiated_from_a_string_with_ansi_sequences():

$this->assertEquals($original, (string) $parsed);
}

public function test_pad_left(): void
{
$parsed = new AnsiString("\e[1mHello \e[0mworld");
$expected = "\e[1m Hello \e[0mworld";

$this->assertEquals($expected, (string) $parsed->padLeft(15));
}

public function test_pad_right(): void
{
$parsed = new AnsiString("\e[1mHello \e[0mworld");
$expected = "\e[1mHello \e[0mworld ";

$this->assertEquals($expected, (string) $parsed->padRight(15));
}
}

0 comments on commit c3b38de

Please sign in to comment.