OutputFormatterStyle.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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\Color;
  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 $color;
  20. private $foreground;
  21. private $background;
  22. private $options;
  23. private $href;
  24. private $handlesHrefGracefully;
  25. /**
  26. * Initializes output formatter style.
  27. *
  28. * @param string|null $foreground The style foreground color name
  29. * @param string|null $background The style background color name
  30. */
  31. public function __construct(string $foreground = null, string $background = null, array $options = [])
  32. {
  33. $this->color = new Color($this->foreground = $foreground ?: '', $this->background = $background ?: '', $this->options = $options);
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function setForeground(string $color = null)
  39. {
  40. $this->color = new Color($this->foreground = $color ?: '', $this->background, $this->options);
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function setBackground(string $color = null)
  46. {
  47. $this->color = new Color($this->foreground, $this->background = $color ?: '', $this->options);
  48. }
  49. public function setHref(string $url): void
  50. {
  51. $this->href = $url;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function setOption(string $option)
  57. {
  58. $this->options[] = $option;
  59. $this->color = new Color($this->foreground, $this->background, $this->options);
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function unsetOption(string $option)
  65. {
  66. $pos = array_search($option, $this->options);
  67. if (false !== $pos) {
  68. unset($this->options[$pos]);
  69. }
  70. $this->color = new Color($this->foreground, $this->background, $this->options);
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function setOptions(array $options)
  76. {
  77. $this->color = new Color($this->foreground, $this->background, $this->options = $options);
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function apply(string $text)
  83. {
  84. if (null === $this->handlesHrefGracefully) {
  85. $this->handlesHrefGracefully = 'JetBrains-JediTerm' !== getenv('TERMINAL_EMULATOR')
  86. && (!getenv('KONSOLE_VERSION') || (int) getenv('KONSOLE_VERSION') > 201100);
  87. }
  88. if (null !== $this->href && $this->handlesHrefGracefully) {
  89. $text = "\033]8;;$this->href\033\\$text\033]8;;\033\\";
  90. }
  91. return $this->color->apply($text);
  92. }
  93. }