Repository navigation
Expand file tree
/
Copy pathOutput.php
More file actions
274 lines (222 loc) · 6.8 KB
/
Copy pathOutput.php
File metadata and controls
274 lines (222 loc) · 6.8 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
declare(strict_types=1);
/*
* This file is part of the PHPRegex package.
*
* (c) Younes ENNAJI <younes.ennaji.pro@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPRegex\Cli;
/**
* @internal
*/
final class Output
{
public const RESET = "\033[0m";
public const RED = "\033[31m";
public const GREEN = "\033[32m";
public const YELLOW = "\033[33m";
public const BLUE = "\033[34m";
public const MAGENTA = "\033[35m";
public const CYAN = "\033[36m";
public const WHITE = "\033[37m";
public const GRAY = "\033[90m";
public const BLACK = "\033[30m";
public const BOLD = "\033[1m";
public const BG_RED = "\033[41m";
public const BG_GREEN = "\033[42m";
public const BG_YELLOW = "\033[43m";
public const BG_BLUE = "\033[44m";
public const BG_CYAN = "\033[46m";
public const BG_GRAY = "\033[100m";
private const PROGRESS_BAR_WIDTH = 28;
private int $progressTotal = 0;
private int $progressCurrent = 0;
private bool $progressActive = false;
private int $progressStartedAt = 0;
private bool $documentWritten = false;
/**
* @param resource|null $errorStream where writeError() writes; STDERR by default
*/
public function __construct(
private bool $ansi,
private bool $quiet,
private readonly string $progressBarFull = '#',
private readonly string $progressBarEmpty = '-',
private $errorStream = null,
) {}
public function isAnsi(): bool
{
return $this->ansi;
}
public function setAnsi(bool $ansi): void
{
$this->ansi = $ansi;
}
public function isQuiet(): bool
{
return $this->quiet;
}
public function setQuiet(bool $quiet): void
{
$this->quiet = $quiet;
}
public function write(string $text): void
{
if (!$this->quiet) {
echo $text;
}
}
/**
* Write a document a program reads, a JSON report: quiet mode silences
* the status lines around it, never the document itself.
*/
public function writeDocument(string $text): void
{
$this->documentWritten = true;
echo $text;
}
/**
* Whether a document was written: a run prints one at most.
*/
public function hasWrittenDocument(): bool
{
return $this->documentWritten;
}
/**
* Write to the error stream, which stays apart from a report on stdout.
* Quiet mode silences stdout only: an error still reaches the user.
*/
public function writeError(string $text): void
{
$stream = $this->errorStream ?? (\defined('STDERR') ? \STDERR : null);
if (\is_resource($stream)) {
fwrite($stream, $text);
}
}
public function color(string $text, string $color): string
{
return $this->ansi ? $color.$text.self::RESET : $text;
}
public function success(string $text): string
{
return $this->color($text, self::GREEN);
}
public function error(string $text): string
{
return $this->color($text, self::RED);
}
public function warning(string $text): string
{
return $this->color($text, self::YELLOW);
}
public function info(string $text): string
{
return $this->color($text, self::BLUE);
}
public function bold(string $text): string
{
return $this->color($text, self::BOLD);
}
public function dim(string $text): string
{
return $this->color($text, self::GRAY);
}
public function badge(string $text, string $fg, string $bg): string
{
if (!$this->ansi) {
return \sprintf('%-4s', $text);
}
return $this->color(' '.$text.' ', $bg.$fg.self::BOLD);
}
public function progressStart(int $total): void
{
if ($this->shouldSkipProgress($total)) {
return;
}
$this->initializeProgress($total);
}
public function progressAdvance(int $step = 1): void
{
if (!$this->progressActive) {
return;
}
$this->progressCurrent = min($this->progressTotal, $this->progressCurrent + $step);
$this->renderProgress();
}
public function progressFinish(): void
{
if (!$this->progressActive) {
return;
}
$this->progressCurrent = $this->progressTotal;
$this->renderProgress(true);
$this->progressActive = false;
}
private function shouldSkipProgress(int $total): bool
{
return $total <= 0 || $this->quiet;
}
private function initializeProgress(int $total): void
{
$this->progressTotal = $total;
$this->progressCurrent = 0;
$this->progressActive = $this->ansi;
$this->progressStartedAt = (int) time();
if ($this->progressActive) {
$this->renderProgress();
}
}
private function renderProgress(bool $finish = false): void
{
$bar = $this->buildProgressBar();
$percent = $this->calculateProgressPercent();
$elapsed = $this->formatElapsed((int) (time() - $this->progressStartedAt));
$status = $this->formatProgressStatus();
$line = \sprintf(' %s [%s] %3d%% %8s', $status, $bar, $percent, $elapsed);
$this->write("\r".$line);
if ($finish) {
$this->write("\n\n");
}
$this->flushOutput();
}
private function buildProgressBar(): string
{
$total = max(1, $this->progressTotal);
$current = min($this->progressCurrent, $total);
$filled = (int) floor(($current / $total) * self::PROGRESS_BAR_WIDTH);
return str_repeat($this->progressBarFull, $filled)
.str_repeat($this->progressBarEmpty, self::PROGRESS_BAR_WIDTH - $filled);
}
private function calculateProgressPercent(): int
{
$total = max(1, $this->progressTotal);
$current = min($this->progressCurrent, $total);
return (int) round(($current / $total) * 100);
}
private function formatProgressStatus(): string
{
$total = max(1, $this->progressTotal);
$current = min($this->progressCurrent, $total);
return str_pad($current.'/'.$total, 15, ' ', \STR_PAD_LEFT);
}
private function flushOutput(): void
{
if (\function_exists('fflush')) {
fflush(\STDOUT);
}
}
private function formatElapsed(int $seconds): string
{
$seconds = max(0, $seconds);
$hours = intdiv($seconds, 3600);
$minutes = intdiv($seconds % 3600, 60);
$secs = $seconds % 60;
if ($hours > 0) {
return \sprintf('%02d:%02d:%02d', $hours, $minutes, $secs);
}
return \sprintf('%02d:%02d', $minutes, $secs);
}
}