Nenhuma descrição

OutputFormatterStyle.php 6.0KB

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. */
  59. public function __construct(string $foreground = null, string $background = null, array $options = [])
  60. {
  61. if (null !== $foreground) {
  62. $this->setForeground($foreground);
  63. }
  64. if (null !== $background) {
  65. $this->setBackground($background);
  66. }
  67. if (\count($options)) {
  68. $this->setOptions($options);
  69. }
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function setForeground($color = null)
  75. {
  76. if (null === $color) {
  77. $this->foreground = null;
  78. return;
  79. }
  80. if (!isset(static::$availableForegroundColors[$color])) {
  81. throw new InvalidArgumentException(sprintf('Invalid foreground color specified: "%s". Expected one of (%s).', $color, implode(', ', array_keys(static::$availableForegroundColors))));
  82. }
  83. $this->foreground = static::$availableForegroundColors[$color];
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function setBackground($color = null)
  89. {
  90. if (null === $color) {
  91. $this->background = null;
  92. return;
  93. }
  94. if (!isset(static::$availableBackgroundColors[$color])) {
  95. throw new InvalidArgumentException(sprintf('Invalid background color specified: "%s". Expected one of (%s).', $color, implode(', ', array_keys(static::$availableBackgroundColors))));
  96. }
  97. $this->background = static::$availableBackgroundColors[$color];
  98. }
  99. public function setHref(string $url): void
  100. {
  101. $this->href = $url;
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function setOption($option)
  107. {
  108. if (!isset(static::$availableOptions[$option])) {
  109. throw new InvalidArgumentException(sprintf('Invalid option specified: "%s". Expected one of (%s).', $option, implode(', ', array_keys(static::$availableOptions))));
  110. }
  111. if (!\in_array(static::$availableOptions[$option], $this->options)) {
  112. $this->options[] = static::$availableOptions[$option];
  113. }
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function unsetOption($option)
  119. {
  120. if (!isset(static::$availableOptions[$option])) {
  121. throw new InvalidArgumentException(sprintf('Invalid option specified: "%s". Expected one of (%s).', $option, implode(', ', array_keys(static::$availableOptions))));
  122. }
  123. $pos = array_search(static::$availableOptions[$option], $this->options);
  124. if (false !== $pos) {
  125. unset($this->options[$pos]);
  126. }
  127. }
  128. /**
  129. * {@inheritdoc}
  130. */
  131. public function setOptions(array $options)
  132. {
  133. $this->options = [];
  134. foreach ($options as $option) {
  135. $this->setOption($option);
  136. }
  137. }
  138. /**
  139. * {@inheritdoc}
  140. */
  141. public function apply($text)
  142. {
  143. $setCodes = [];
  144. $unsetCodes = [];
  145. if (null === $this->handlesHrefGracefully) {
  146. $this->handlesHrefGracefully = 'JetBrains-JediTerm' !== getenv('TERMINAL_EMULATOR')
  147. && (!getenv('KONSOLE_VERSION') || (int) getenv('KONSOLE_VERSION') > 201100);
  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. }