OutputFormatterStyle.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. public function setForeground(?string $color): void
  36. {
  37. $this->color = new Color($this->foreground = $color ?: '', $this->background, $this->options);
  38. }
  39. public function setBackground(?string $color): void
  40. {
  41. $this->color = new Color($this->foreground, $this->background = $color ?: '', $this->options);
  42. }
  43. public function setHref(string $url): void
  44. {
  45. $this->href = $url;
  46. }
  47. public function setOption(string $option): void
  48. {
  49. $this->options[] = $option;
  50. $this->color = new Color($this->foreground, $this->background, $this->options);
  51. }
  52. public function unsetOption(string $option): void
  53. {
  54. $pos = array_search($option, $this->options);
  55. if (false !== $pos) {
  56. unset($this->options[$pos]);
  57. }
  58. $this->color = new Color($this->foreground, $this->background, $this->options);
  59. }
  60. public function setOptions(array $options): void
  61. {
  62. $this->color = new Color($this->foreground, $this->background, $this->options = $options);
  63. }
  64. public function apply(string $text): string
  65. {
  66. $this->handlesHrefGracefully ??= 'JetBrains-JediTerm' !== getenv('TERMINAL_EMULATOR')
  67. && (!getenv('KONSOLE_VERSION') || (int) getenv('KONSOLE_VERSION') > 201100)
  68. && !isset($_SERVER['IDEA_INITIAL_DIRECTORY']);
  69. if (null !== $this->href && $this->handlesHrefGracefully) {
  70. $text = "\033]8;;$this->href\033\\$text\033]8;;\033\\";
  71. }
  72. return $this->color->apply($text);
  73. }
  74. }