| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <fabien@symfony.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\Console\Formatter; |
| 13 |
|
| 14 |
use Symfony\Component\Console\Exception\InvalidArgumentException; |
| 15 |
|
| 16 |
/** |
| 17 |
* Formatter style class for defining styles. |
| 18 |
* |
| 19 |
* @author Konstantin Kudryashov <ever.zet@gmail.com> |
| 20 |
*/ |
| 21 |
class OutputFormatterStyle implements OutputFormatterStyleInterface |
| 22 |
{ |
| 23 |
private static $availableForegroundColors = [ |
| 24 |
'black' => ['set' => 30, 'unset' => 39], |
| 25 |
'red' => ['set' => 31, 'unset' => 39], |
| 26 |
'green' => ['set' => 32, 'unset' => 39], |
| 27 |
'yellow' => ['set' => 33, 'unset' => 39], |
| 28 |
'blue' => ['set' => 34, 'unset' => 39], |
| 29 |
'magenta' => ['set' => 35, 'unset' => 39], |
| 30 |
'cyan' => ['set' => 36, 'unset' => 39], |
| 31 |
'white' => ['set' => 37, 'unset' => 39], |
| 32 |
'default' => ['set' => 39, 'unset' => 39], |
| 33 |
]; |
| 34 |
private static $availableBackgroundColors = [ |
| 35 |
'black' => ['set' => 40, 'unset' => 49], |
| 36 |
'red' => ['set' => 41, 'unset' => 49], |
| 37 |
'green' => ['set' => 42, 'unset' => 49], |
| 38 |
'yellow' => ['set' => 43, 'unset' => 49], |
| 39 |
'blue' => ['set' => 44, 'unset' => 49], |
| 40 |
'magenta' => ['set' => 45, 'unset' => 49], |
| 41 |
'cyan' => ['set' => 46, 'unset' => 49], |
| 42 |
'white' => ['set' => 47, 'unset' => 49], |
| 43 |
'default' => ['set' => 49, 'unset' => 49], |
| 44 |
]; |
| 45 |
private static $availableOptions = [ |
| 46 |
'bold' => ['set' => 1, 'unset' => 22], |
| 47 |
'underscore' => ['set' => 4, 'unset' => 24], |
| 48 |
'blink' => ['set' => 5, 'unset' => 25], |
| 49 |
'reverse' => ['set' => 7, 'unset' => 27], |
| 50 |
'conceal' => ['set' => 8, 'unset' => 28], |
| 51 |
]; |
| 52 |
|
| 53 |
private $foreground; |
| 54 |
private $background; |
| 55 |
private $href; |
| 56 |
private $options = []; |
| 57 |
private $handlesHrefGracefully; |
| 58 |
|
| 59 |
/** |
| 60 |
* Initializes output formatter style. |
| 61 |
* |
| 62 |
* @param string|null $foreground The style foreground color name |
| 63 |
* @param string|null $background The style background color name |
| 64 |
*/ |
| 65 |
public function __construct(string $foreground = null, string $background = null, array $options = []) |
| 66 |
{ |
| 67 |
if (null !== $foreground) { |
| 68 |
$this->setForeground($foreground); |
| 69 |
} |
| 70 |
if (null !== $background) { |
| 71 |
$this->setBackground($background); |
| 72 |
} |
| 73 |
if (\count($options)) { |
| 74 |
$this->setOptions($options); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* {@inheritdoc} |
| 80 |
*/ |
| 81 |
public function setForeground(string $color = null) |
| 82 |
{ |
| 83 |
if (null === $color) { |
| 84 |
$this->foreground = null; |
| 85 |
|
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
if (!isset(static::$availableForegroundColors[$color])) { |
| 90 |
throw new InvalidArgumentException(sprintf('Invalid foreground color specified: "%s". Expected one of (%s).', $color, implode(', ', array_keys(static::$availableForegroundColors)))); |
| 91 |
} |
| 92 |
|
| 93 |
$this->foreground = static::$availableForegroundColors[$color]; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* {@inheritdoc} |
| 98 |
*/ |
| 99 |
public function setBackground(string $color = null) |
| 100 |
{ |
| 101 |
if (null === $color) { |
| 102 |
$this->background = null; |
| 103 |
|
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
if (!isset(static::$availableBackgroundColors[$color])) { |
| 108 |
throw new InvalidArgumentException(sprintf('Invalid background color specified: "%s". Expected one of (%s).', $color, implode(', ', array_keys(static::$availableBackgroundColors)))); |
| 109 |
} |
| 110 |
|
| 111 |
$this->background = static::$availableBackgroundColors[$color]; |
| 112 |
} |
| 113 |
|
| 114 |
public function setHref(string $url): void |
| 115 |
{ |
| 116 |
$this->href = $url; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* {@inheritdoc} |
| 121 |
*/ |
| 122 |
public function setOption(string $option) |
| 123 |
{ |
| 124 |
if (!isset(static::$availableOptions[$option])) { |
| 125 |
throw new InvalidArgumentException(sprintf('Invalid option specified: "%s". Expected one of (%s).', $option, implode(', ', array_keys(static::$availableOptions)))); |
| 126 |
} |
| 127 |
|
| 128 |
if (!\in_array(static::$availableOptions[$option], $this->options)) { |
| 129 |
$this->options[] = static::$availableOptions[$option]; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* {@inheritdoc} |
| 135 |
*/ |
| 136 |
public function unsetOption(string $option) |
| 137 |
{ |
| 138 |
if (!isset(static::$availableOptions[$option])) { |
| 139 |
throw new InvalidArgumentException(sprintf('Invalid option specified: "%s". Expected one of (%s).', $option, implode(', ', array_keys(static::$availableOptions)))); |
| 140 |
} |
| 141 |
|
| 142 |
$pos = array_search(static::$availableOptions[$option], $this->options); |
| 143 |
if (false !== $pos) { |
| 144 |
unset($this->options[$pos]); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* {@inheritdoc} |
| 150 |
*/ |
| 151 |
public function setOptions(array $options) |
| 152 |
{ |
| 153 |
$this->options = []; |
| 154 |
|
| 155 |
foreach ($options as $option) { |
| 156 |
$this->setOption($option); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* {@inheritdoc} |
| 162 |
*/ |
| 163 |
public function apply(string $text) |
| 164 |
{ |
| 165 |
$setCodes = []; |
| 166 |
$unsetCodes = []; |
| 167 |
|
| 168 |
if (null === $this->handlesHrefGracefully) { |
| 169 |
$this->handlesHrefGracefully = 'JetBrains-JediTerm' !== getenv('TERMINAL_EMULATOR') |
| 170 |
&& (!getenv('KONSOLE_VERSION') || (int) getenv('KONSOLE_VERSION') > 201100); |
| 171 |
} |
| 172 |
|
| 173 |
if (null !== $this->foreground) { |
| 174 |
$setCodes[] = $this->foreground['set']; |
| 175 |
$unsetCodes[] = $this->foreground['unset']; |
| 176 |
} |
| 177 |
if (null !== $this->background) { |
| 178 |
$setCodes[] = $this->background['set']; |
| 179 |
$unsetCodes[] = $this->background['unset']; |
| 180 |
} |
| 181 |
|
| 182 |
foreach ($this->options as $option) { |
| 183 |
$setCodes[] = $option['set']; |
| 184 |
$unsetCodes[] = $option['unset']; |
| 185 |
} |
| 186 |
|
| 187 |
if (null !== $this->href && $this->handlesHrefGracefully) { |
| 188 |
$text = "\033]8;;$this->href\033\\$text\033]8;;\033\\"; |
| 189 |
} |
| 190 |
|
| 191 |
if (0 === \count($setCodes)) { |
| 192 |
return $text; |
| 193 |
} |
| 194 |
|
| 195 |
return sprintf("\033[%sm%s\033[%sm", implode(';', $setCodes), $text, implode(';', $unsetCodes)); |
| 196 |
} |
| 197 |
} |
| 198 |
|