You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
2.8 KiB

3 years ago
  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. }