OutputFormatterStyle.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 $color;
  20. private string $foreground;
  21. private string $background;
  22. private array $options;
  23. private ?string $href = null;
  24. private bool $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. * @return void
  37. */
  38. public function setForeground(?string $color = null)
  39. {
  40. if (1 > \func_num_args()) {
  41. trigger_deprecation('symfony/console', '6.2', 'Calling "%s()" without any arguments is deprecated, pass null explicitly instead.', __METHOD__);
  42. }
  43. $this->color = new Color($this->foreground = $color ?: '', $this->background, $this->options);
  44. }
  45. /**
  46. * @return void
  47. */
  48. public function setBackground(?string $color = null)
  49. {
  50. if (1 > \func_num_args()) {
  51. trigger_deprecation('symfony/console', '6.2', 'Calling "%s()" without any arguments is deprecated, pass null explicitly instead.', __METHOD__);
  52. }
  53. $this->color = new Color($this->foreground, $this->background = $color ?: '', $this->options);
  54. }
  55. public function setHref(string $url): void
  56. {
  57. $this->href = $url;
  58. }
  59. /**
  60. * @return void
  61. */
  62. public function setOption(string $option)
  63. {
  64. $this->options[] = $option;
  65. $this->color = new Color($this->foreground, $this->background, $this->options);
  66. }
  67. /**
  68. * @return void
  69. */
  70. public function unsetOption(string $option)
  71. {
  72. $pos = array_search($option, $this->options);
  73. if (false !== $pos) {
  74. unset($this->options[$pos]);
  75. }
  76. $this->color = new Color($this->foreground, $this->background, $this->options);
  77. }
  78. /**
  79. * @return void
  80. */
  81. public function setOptions(array $options)
  82. {
  83. $this->color = new Color($this->foreground, $this->background, $this->options = $options);
  84. }
  85. public function apply(string $text): string
  86. {
  87. $this->handlesHrefGracefully ??= 'JetBrains-JediTerm' !== getenv('TERMINAL_EMULATOR')
  88. && (!getenv('KONSOLE_VERSION') || (int) getenv('KONSOLE_VERSION') > 201100)
  89. && !isset($_SERVER['IDEA_INITIAL_DIRECTORY']);
  90. if (null !== $this->href && $this->handlesHrefGracefully) {
  91. $text = "\033]8;;$this->href\033\\$text\033]8;;\033\\";
  92. }
  93. return $this->color->apply($text);
  94. }
  95. }