| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Console\View\Components;
|
|
|
4 |
|
|
|
5 |
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
6 |
use Throwable;
|
|
|
7 |
|
| 991 |
lars |
8 |
use function Termwind\terminal;
|
|
|
9 |
|
| 148 |
lars |
10 |
class Task extends Component
|
|
|
11 |
{
|
|
|
12 |
/**
|
|
|
13 |
* Renders the component using the given arguments.
|
|
|
14 |
*
|
|
|
15 |
* @param string $description
|
|
|
16 |
* @param (callable(): bool)|null $task
|
|
|
17 |
* @param int $verbosity
|
|
|
18 |
* @return void
|
|
|
19 |
*/
|
|
|
20 |
public function render($description, $task = null, $verbosity = OutputInterface::VERBOSITY_NORMAL)
|
|
|
21 |
{
|
|
|
22 |
$description = $this->mutate($description, [
|
|
|
23 |
Mutators\EnsureDynamicContentIsHighlighted::class,
|
|
|
24 |
Mutators\EnsureNoPunctuation::class,
|
|
|
25 |
Mutators\EnsureRelativePaths::class,
|
|
|
26 |
]);
|
|
|
27 |
|
|
|
28 |
$descriptionWidth = mb_strlen(preg_replace("/\<[\w=#\/\;,:.&,%?]+\>|\\e\[\d+m/", '$1', $description) ?? '');
|
|
|
29 |
|
|
|
30 |
$this->output->write(" $description ", false, $verbosity);
|
|
|
31 |
|
|
|
32 |
$startTime = microtime(true);
|
|
|
33 |
|
|
|
34 |
$result = false;
|
|
|
35 |
|
|
|
36 |
try {
|
|
|
37 |
$result = ($task ?: fn () => true)();
|
|
|
38 |
} catch (Throwable $e) {
|
|
|
39 |
throw $e;
|
|
|
40 |
} finally {
|
|
|
41 |
$runTime = $task
|
|
|
42 |
? (' '.number_format((microtime(true) - $startTime) * 1000).'ms')
|
|
|
43 |
: '';
|
|
|
44 |
|
|
|
45 |
$runTimeWidth = mb_strlen($runTime);
|
|
|
46 |
$width = min(terminal()->width(), 150);
|
|
|
47 |
$dots = max($width - $descriptionWidth - $runTimeWidth - 10, 0);
|
|
|
48 |
|
|
|
49 |
$this->output->write(str_repeat('<fg=gray>.</>', $dots), false, $verbosity);
|
|
|
50 |
$this->output->write("<fg=gray>$runTime</>", false, $verbosity);
|
|
|
51 |
|
|
|
52 |
$this->output->writeln(
|
|
|
53 |
$result !== false ? ' <fg=green;options=bold>DONE</>' : ' <fg=red;options=bold>FAIL</>',
|
|
|
54 |
$verbosity,
|
|
|
55 |
);
|
|
|
56 |
}
|
|
|
57 |
}
|
|
|
58 |
}
|