1
0
Fork 0
composer/src/Composer/Console/HtmlOutputFormatter.php

104 lines
2.9 KiB
PHP
Raw Normal View History

2022-02-23 15:58:18 +00:00
<?php declare(strict_types=1);
2012-11-05 12:55:23 +00:00
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Console;
use Closure;
use Composer\Pcre\Preg;
2012-11-05 12:55:23 +00:00
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
2012-11-05 12:55:23 +00:00
/**
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class HtmlOutputFormatter extends OutputFormatter
{
/** @var array<int, string> */
2022-08-17 12:20:07 +00:00
private static $availableForegroundColors = [
2012-11-05 12:55:23 +00:00
30 => 'black',
31 => 'red',
32 => 'green',
33 => 'yellow',
34 => 'blue',
35 => 'magenta',
36 => 'cyan',
2015-09-28 09:51:14 +00:00
37 => 'white',
2022-08-17 12:20:07 +00:00
];
/** @var array<int, string> */
2022-08-17 12:20:07 +00:00
private static $availableBackgroundColors = [
2012-11-05 12:55:23 +00:00
40 => 'black',
41 => 'red',
42 => 'green',
43 => 'yellow',
44 => 'blue',
45 => 'magenta',
46 => 'cyan',
2015-09-28 09:51:14 +00:00
47 => 'white',
2022-08-17 12:20:07 +00:00
];
/** @var array<int, string> */
2022-08-17 12:20:07 +00:00
private static $availableOptions = [
2012-11-05 12:55:23 +00:00
1 => 'bold',
4 => 'underscore',
//5 => 'blink',
//7 => 'reverse',
//8 => 'conceal'
2022-08-17 12:20:07 +00:00
];
2012-11-05 12:55:23 +00:00
/**
* @param array<string, OutputFormatterStyle> $styles Array of "name => FormatterStyle" instances
2012-11-05 12:55:23 +00:00
*/
2022-08-17 12:20:07 +00:00
public function __construct(array $styles = [])
2012-11-05 12:55:23 +00:00
{
parent::__construct(true, $styles);
}
2021-12-08 21:06:17 +00:00
public function format(?string $message): ?string
2012-11-05 12:55:23 +00:00
{
$formatted = parent::format($message);
if ($formatted === null) {
return null;
}
$clearEscapeCodes = '(?:39|49|0|22|24|25|27|28)';
2015-06-30 14:38:32 +00:00
return Preg::replaceCallback("{\033\[([0-9;]+)m(.*?)\033\[(?:".$clearEscapeCodes.";)*?".$clearEscapeCodes."m}s", Closure::fromCallable([$this, 'formatHtml']), $formatted);
2012-11-05 12:55:23 +00:00
}
2021-10-17 08:18:22 +00:00
/**
* @param string[] $matches
2021-10-17 08:18:22 +00:00
*/
private function formatHtml(array $matches): string
2012-11-05 12:55:23 +00:00
{
$out = '<span style="';
foreach (explode(';', $matches[1]) as $code) {
if (isset(self::$availableForegroundColors[(int) $code])) {
$out .= 'color:'.self::$availableForegroundColors[(int) $code].';';
} elseif (isset(self::$availableBackgroundColors[(int) $code])) {
$out .= 'background-color:'.self::$availableBackgroundColors[(int) $code].';';
} elseif (isset(self::$availableOptions[(int) $code])) {
switch (self::$availableOptions[(int) $code]) {
2012-11-05 12:55:23 +00:00
case 'bold':
$out .= 'font-weight:bold;';
break;
case 'underscore':
$out .= 'text-decoration:underline;';
break;
}
}
}
return $out.'">'.$matches[2].'</span>';
2012-11-05 12:55:23 +00:00
}
}