-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnsiOutput.php
41 lines (34 loc) · 934 Bytes
/
AnsiOutput.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
namespace Felix\Tin\Outputs;
use Felix\Tin\Contracts\OutputInterface;
use Felix\Tin\Contracts\ThemeInterface;
use Felix\Tin\Enums\TokenType;
use Felix\Tin\Line;
readonly class AnsiOutput implements OutputInterface
{
public function __construct(
protected ThemeInterface $theme,
protected bool $ansiEnabled = true)
{
}
public function transform(TokenType $type, string $value): string
{
if (!$this->ansiEnabled) {
return $value;
}
return "\e[38;2;{$this->theme->color($type)}m{$value}\e[0m";
}
public function theme(): ThemeInterface
{
return $this->theme;
}
public function transformLine(Line $line): string
{
return str_pad(
(string) $line->number,
strlen((string) $line->totalCount),
' ',
STR_PAD_LEFT
) . ' | ' . $line->toString() . PHP_EOL;
}
}