OutputFormatterStyle.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. * Sets style foreground color.
  74. *
  75. * @param string|null $color The color name
  76. *
  77. * @throws InvalidArgumentException When the color name isn't defined
  78. */
  79. public function setForeground($color = null)
  80. {
  81. if (null === $color) {
  82. $this->foreground = null;
  83. return;
  84. }
  85. if (!isset(static::$availableForegroundColors[$color])) {
  86. throw new InvalidArgumentException(sprintf('Invalid foreground color specified: "%s". Expected one of (%s)', $color, implode(', ', array_keys(static::$availableForegroundColors))));
  87. }
  88. $this->foreground = static::$availableForegroundColors[$color];
  89. }
  90. /**
  91. * Sets style background color.
  92. *
  93. * @param string|null $color The color name
  94. *
  95. * @throws InvalidArgumentException When the color name isn't defined
  96. */
  97. public function setBackground($color = null)
  98. {
  99. if (null === $color) {
  100. $this->background = null;
  101. return;
  102. }
  103. if (!isset(static::$availableBackgroundColors[$color])) {
  104. throw new InvalidArgumentException(sprintf('Invalid background color specified: "%s". Expected one of (%s)', $color, implode(', ', array_keys(static::$availableBackgroundColors))));
  105. }
  106. $this->background = static::$availableBackgroundColors[$color];
  107. }
  108. public function setHref(string $url): void
  109. {
  110. $this->href = $url;
  111. }
  112. /**
  113. * Sets some specific style option.
  114. *
  115. * @param string $option The option name
  116. *
  117. * @throws InvalidArgumentException When the option name isn't defined
  118. */
  119. public function setOption($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. if (!\in_array(static::$availableOptions[$option], $this->options)) {
  125. $this->options[] = static::$availableOptions[$option];
  126. }
  127. }
  128. /**
  129. * Unsets some specific style option.
  130. *
  131. * @param string $option The option name
  132. *
  133. * @throws InvalidArgumentException When the option name isn't defined
  134. */
  135. public function unsetOption($option)
  136. {
  137. if (!isset(static::$availableOptions[$option])) {
  138. throw new InvalidArgumentException(sprintf('Invalid option specified: "%s". Expected one of (%s)', $option, implode(', ', array_keys(static::$availableOptions))));
  139. }
  140. $pos = array_search(static::$availableOptions[$option], $this->options);
  141. if (false !== $pos) {
  142. unset($this->options[$pos]);
  143. }
  144. }
  145. /**
  146. * {@inheritdoc}
  147. */
  148. public function setOptions(array $options)
  149. {
  150. $this->options = [];
  151. foreach ($options as $option) {
  152. $this->setOption($option);
  153. }
  154. }
  155. /**
  156. * Applies the style to a given text.
  157. *
  158. * @param string $text The text to style
  159. *
  160. * @return string
  161. */
  162. public function apply($text)
  163. {
  164. $setCodes = [];
  165. $unsetCodes = [];
  166. if (null === $this->handlesHrefGracefully) {
  167. $this->handlesHrefGracefully = 'JetBrains-JediTerm' !== getenv('TERMINAL_EMULATOR') && !getenv('KONSOLE_VERSION');
  168. }
  169. if (null !== $this->foreground) {
  170. $setCodes[] = $this->foreground['set'];
  171. $unsetCodes[] = $this->foreground['unset'];
  172. }
  173. if (null !== $this->background) {
  174. $setCodes[] = $this->background['set'];
  175. $unsetCodes[] = $this->background['unset'];
  176. }
  177. foreach ($this->options as $option) {
  178. $setCodes[] = $option['set'];
  179. $unsetCodes[] = $option['unset'];
  180. }
  181. if (null !== $this->href && $this->handlesHrefGracefully) {
  182. $text = "\033]8;;$this->href\033\\$text\033]8;;\033\\";
  183. }
  184. if (0 === \count($setCodes)) {
  185. return $text;
  186. }
  187. return sprintf("\033[%sm%s\033[%sm", implode(';', $setCodes), $text, implode(';', $unsetCodes));
  188. }
  189. }