| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\Console\Formatter; |
| 13 |
|
| 14 |
use Symfony\Component\Console\Color; |
| 15 |
|
| 16 |
/** |
| 17 |
* Formatter style class for defining styles. |
| 18 |
* |
| 19 |
* @author Konstantin Kudryashov <[email protected]> |
| 20 |
*/ |
| 21 |
class OutputFormatterStyle implements OutputFormatterStyleInterface |
| 22 |
{ |
| 23 |
private Color $color; |
| 24 |
private string $foreground; |
| 25 |
private string $background; |
| 26 |
private array $options; |
| 27 |
private ?string $href = null; |
| 28 |
private bool $handlesHrefGracefully; |
| 29 |
|
| 30 |
/** |
| 31 |
* Initializes output formatter style. |
| 32 |
* |
| 33 |
* @param string|null $foreground The style foreground color name |
| 34 |
* @param string|null $background The style background color name |
| 35 |
*/ |
| 36 |
public function __construct(?string $foreground = null, ?string $background = null, array $options = []) |
| 37 |
{ |
| 38 |
$this->color = new Color($this->foreground = $foreground ?: '', $this->background = $background ?: '', $this->options = $options); |
| 39 |
} |
| 40 |
|
| 41 |
public function setForeground(?string $color): void |
| 42 |
{ |
| 43 |
$this->color = new Color($this->foreground = $color ?: '', $this->background, $this->options); |
| 44 |
} |
| 45 |
|
| 46 |
public function setBackground(?string $color): void |
| 47 |
{ |
| 48 |
$this->color = new Color($this->foreground, $this->background = $color ?: '', $this->options); |
| 49 |
} |
| 50 |
|
| 51 |
public function setHref(string $url): void |
| 52 |
{ |
| 53 |
$this->href = $url; |
| 54 |
} |
| 55 |
|
| 56 |
public function setOption(string $option): void |
| 57 |
{ |
| 58 |
$this->options[] = $option; |
| 59 |
$this->color = new Color($this->foreground, $this->background, $this->options); |
| 60 |
} |
| 61 |
|
| 62 |
public function unsetOption(string $option): void |
| 63 |
{ |
| 64 |
$pos = array_search($option, $this->options); |
| 65 |
if (false !== $pos) { |
| 66 |
unset($this->options[$pos]); |
| 67 |
} |
| 68 |
|
| 69 |
$this->color = new Color($this->foreground, $this->background, $this->options); |
| 70 |
} |
| 71 |
|
| 72 |
public function setOptions(array $options): void |
| 73 |
{ |
| 74 |
$this->color = new Color($this->foreground, $this->background, $this->options = $options); |
| 75 |
} |
| 76 |
|
| 77 |
public function apply(string $text): string |
| 78 |
{ |
| 79 |
$this->handlesHrefGracefully ??= 'JetBrains-JediTerm' !== getenv('TERMINAL_EMULATOR') |
| 80 |
&& (!getenv('KONSOLE_VERSION') || (int) getenv('KONSOLE_VERSION') > 201100) |
| 81 |
&& !isset($_SERVER['IDEA_INITIAL_DIRECTORY']); |
| 82 |
|
| 83 |
if (null !== $this->href && $this->handlesHrefGracefully) { |
| 84 |
$text = "\033]8;;$this->href\033\\$text\033]8;;\033\\"; |
| 85 |
} |
| 86 |
|
| 87 |
return $this->color->apply($text); |
| 88 |
} |
| 89 |
} |