8000 [Console] Add hyperlinks support by ostrolucky · Pull Request #29168 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console] Add hyperlinks support #29168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.3.0
-----

* added support for hyperlinks

4.2.0
-----

Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/Console/Formatter/OutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function formatAndWrap(string $message, int $width)
{
$offset = 0;
$output = '';
$tagRegex = '[a-z][a-z0-9,_=;-]*+';
$tagRegex = '[a-z][^<>]*+';
$currentLineLength = 0;
preg_match_all("#<(($tagRegex) | /($tagRegex)?)>#ix", $message, $matches, PREG_OFFSET_CAPTURE);
foreach ($matches[0] as $i => $match) {
Expand Down Expand Up @@ -215,6 +215,8 @@ private function createStyleFromString(string $string)
$style->setForeground($match[1]);
} elseif ('bg' == $match[0]) {
$style->setBackground($match[1]);
} elseif ('href' === $match[0]) {
$style->setHref($match[1]);
} elseif ('options' === $match[0]) {
preg_match_all('([^,;]+)', $match[1], $options);
$options = array_shift($options);
Expand Down
19 changes: 14 additions & 5 deletions src/Symfony/Component/Console/Formatter/OutputFormatterStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class OutputFormatterStyle implements OutputFormatterStyleInterface

private $foreground;
private $background;
private $href;
private $options = array();

/**
Expand Down Expand Up @@ -118,6 +119,11 @@ public function setBackground($color = null)
$this->background = static::$availableBackgroundColors[$color];
}

public function setHref(string $url): void
{
$this->href = $url;
}

/**
* Sets some specific style option.
*
Expand Down Expand Up @@ -187,11 +193,14 @@ public function apply($text)
$setCodes[] = $this->background['set'];
$unsetCodes[] = $this->background['unset'];
}
if (\count($this->options)) {
foreach ($this->options as $option) {
$setCodes[] = $option['set'];
$unsetCodes[] = $option['unset'];
}

foreach ($this->options as $option) {
$setCodes[] = $option['set'];
$unsetCodes[] = $option['unset'];
}

if (null !== $this->href) {
$text = "\033]8;;$this->href\033\\$text\033]8;;\033\\";
}

if (0 === \count($setCodes)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,47 +228,34 @@ public function testFormatToStringObject()
);
}

public function testNotDecoratedFormatter()
public function testFormatterHasStyles()
{
$formatter = new OutputFormatter(false);

$this->assertTrue($formatter->hasStyle('error'));
$this->assertTrue($formatter->hasStyle('info'));
$this->assertTrue($formatter->hasStyle('comment'));
$this->assertTrue($formatter->hasStyle('question'));
}

$this->assertEquals(
'some error', $formatter->format('<error>some error</error>')
);
$this->assertEquals(
'some info', $formatter->format('<info>some info</info>')
);
$this->assertEquals(
'some comment', $formatter->format('<comment>some comment</comment>')
);
$this->assertEquals(
'some question', $formatter->format('<question>some question</question>')
);
$this->assertEquals(
'some text with inline style', $formatter->format('<fg=red>some text with inline style</>')
);

$formatter->setDecorated(true);
/**
* @dataProvider provideDecoratedAndNonDecoratedOutput
*/
public function testNotDecoratedFormatter(string $input, string $expectedNonDecoratedOutput, string $expectedDecoratedOutput)
{
$this->assertEquals($expectedDecoratedOutput, (new OutputFormatter(true))->format($input));
$this->assertEquals($expectedNonDecoratedOutput, (new OutputFormatter(false))->format($input));
}

$this->assertEquals(
"\033[37;41msome error\033[39;49m", $formatter->format('<error>some error</error>')
);
$this->assertEquals(
"\033[32msome info\033[39m", $formatter->format('<info>some info</info>')
);
$this->assertEquals(
"\033[33msome comment\033[39m", $formatter->format('<comment>some comment</comment>')
);
$this->assertEquals(
"\033[30;46msome question\033[39;49m", $formatter->format('<question>some question</question>')
);
$this->assertEquals(
"\033[31msome text with inline style\033[39m", $formatter->format('<fg=red>some text with inline style</>')
public function provideDecoratedAndNonDecoratedOutput()
{
return array(
array('<error>some error</error>', 'some error', "\033[37;41msome error\033[39;49m"),
array('<info>some info</info>', 'some info', "\033[32msome info\033[39m"),
array('<comment>some comment</comment>', 'some comment', "\033[33msome comment\033[39m"),
array('<question>some question</question>', 'some question', "\033[30;46msome question\033[39;49m"),
array('<fg=red>some text with inline style</>', 'some text with inline style', "\033[31msome text with inline style\033[39m"),
array('<href=idea://open/?file=/path/somefile.php&line=12>some URL</>', 'some URL', "\033]8;;idea://open/?file=/path/somefile.php&line=12\033\\some URL\033]8;;\033\\"),
);
}

Expand Down
0