1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Conia\Cli; |
6
|
|
|
|
7
|
|
|
class Output |
8
|
|
|
{ |
9
|
|
|
protected mixed $stream; |
10
|
|
|
protected array $fg = [ |
11
|
|
|
'black' => [0, 30], |
12
|
|
|
'gray' => [1, 30], |
13
|
|
|
'grey' => [1, 30], |
14
|
|
|
'red' => [0, 31], |
15
|
|
|
'lightred' => [1, 31], |
16
|
|
|
'green' => [0, 32], |
17
|
|
|
'lightgreen' => [1, 32], |
18
|
|
|
'brown' => [0, 33], |
19
|
|
|
'yellow' => [1, 33], |
20
|
|
|
'blue' => [0, 34], |
21
|
|
|
'lightblue' => [1, 34], |
22
|
|
|
'purple' => [0, 35], |
23
|
|
|
'lightpurple' => [1, 35], |
24
|
|
|
'magenta' => [0, 35], |
25
|
|
|
'lightmagenta' => [1, 35], |
26
|
|
|
'cyan' => [0, 36], |
27
|
|
|
'lightcyan' => [1, 36], |
28
|
|
|
'lightgray' => [0, 37], |
29
|
|
|
'lightgrey' => [0, 37], |
30
|
|
|
'white' => [1, 37], |
31
|
|
|
]; |
32
|
|
|
protected array $bg = [ |
33
|
|
|
'black' => 40, |
34
|
|
|
'red' => 41, |
35
|
|
|
'green' => 42, |
36
|
|
|
'yellow' => 43, |
37
|
|
|
'blue' => 44, |
38
|
|
|
'purple' => 45, |
39
|
|
|
'magenta' => 45, |
40
|
|
|
'cyan' => 46, |
41
|
|
|
'gray' => 47, |
42
|
|
|
'grey' => 47, |
43
|
|
|
'white' => 47, |
44
|
|
|
]; |
45
|
|
|
|
46
|
16 |
|
public function __construct(protected readonly string $target) |
47
|
|
|
{ |
48
|
16 |
|
} |
49
|
|
|
|
50
|
13 |
|
public function echo(string $message): void |
51
|
|
|
{ |
52
|
13 |
|
fwrite($this->getStream(), $message); |
53
|
13 |
|
fflush($this->stream); |
54
|
|
|
} |
55
|
|
|
|
56
|
8 |
|
public function color(string $text, string $color, string $background = null): string |
57
|
|
|
{ |
58
|
8 |
|
[$first, $second] = $this->fg[$color]; |
59
|
|
|
|
60
|
8 |
|
if ($background) { |
61
|
1 |
|
$bg = $this->bg[$background]; |
62
|
|
|
|
63
|
1 |
|
return "\033[{$first};{$second};{$bg}m{$text}\033[0m"; |
64
|
|
|
} |
65
|
|
|
|
66
|
7 |
|
return "\033[{$first};{$second}m{$text}\033[0m"; |
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
public function indent( |
70
|
|
|
string $text, |
71
|
|
|
int $indent, |
72
|
|
|
?int $max = null, |
73
|
|
|
): string { |
74
|
2 |
|
$spaces = str_repeat(' ', $indent); |
75
|
|
|
|
76
|
|
|
/** @psalm-suppress ForbiddenCode */ |
77
|
2 |
|
$width = shell_exec('tput cols'); |
78
|
|
|
|
79
|
2 |
|
if ($width === null) { |
80
|
|
|
// Need a way to force $width to be null in a sane way |
81
|
|
|
// @codeCoverageIgnoreStart |
82
|
|
|
$width = 80; |
83
|
|
|
// @codeCoverageIgnoreEnd |
84
|
|
|
} |
85
|
|
|
|
86
|
2 |
|
$width = (int)$width - $indent; |
87
|
|
|
|
88
|
2 |
|
if ($max !== null && $max < $width) { |
89
|
1 |
|
$width = $max; |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
$lines = explode("\n", wordwrap($text, $width, "\n")); |
93
|
|
|
|
94
|
2 |
|
return implode("\n", array_map(function ($line) use ($spaces) { |
95
|
2 |
|
return $spaces . $line; |
96
|
2 |
|
}, $lines)); |
97
|
|
|
} |
98
|
|
|
|
99
|
13 |
|
protected function getStream(): mixed |
100
|
|
|
{ |
101
|
13 |
|
if (!isset($this->stream)) { |
102
|
13 |
|
$this->stream = fopen($this->target, 'w'); |
103
|
|
|
} |
104
|
|
|
|
105
|
13 |
|
return $this->stream; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|