OutputFormatterStyle.php 6.0 KB

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