Skip to content

Commit 4f04cf8

Browse files
ostroluckyfabpot
authored andcommitted
[Console] Add hyperlinks support
1 parent 0a26a2e commit 4f04cf8

File tree

4 files changed

+41
-38
lines changed

4 files changed

+41
-38
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.3.0
5+
-----
6+
7+
* added support for hyperlinks
8+
49
4.2.0
510
-----
611

Formatter/OutputFormatter.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public function formatAndWrap(string $message, int $width)
141141
{
142142
$offset = 0;
143143
$output = '';
144-
$tagRegex = '[a-z][a-z0-9,_=;-]*+';
144+
$tagRegex = '[a-z][^<>]*+';
145145
$currentLineLength = 0;
146146
preg_match_all("#<(($tagRegex) | /($tagRegex)?)>#ix", $message, $matches, PREG_OFFSET_CAPTURE);
147147
foreach ($matches[0] as $i => $match) {
@@ -215,6 +215,8 @@ private function createStyleFromString(string $string)
215215
$style->setForeground($match[1]);
216216
} elseif ('bg' == $match[0]) {
217217
$style->setBackground($match[1]);
218+
} elseif ('href' === $match[0]) {
219+
$style->setHref($match[1]);
218220
} elseif ('options' === $match[0]) {
219221
preg_match_all('([^,;]+)', $match[1], $options);
220222
$options = array_shift($options);

Formatter/OutputFormatterStyle.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class OutputFormatterStyle implements OutputFormatterStyleInterface
5252

5353
private $foreground;
5454
private $background;
55+
private $href;
5556
private $options = array();
5657

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

122+
public function setHref(string $url): void
123+
{
124+
$this->href = $url;
125+
}
126+
121127
/**
122128
* Sets some specific style option.
123129
*
@@ -187,11 +193,14 @@ public function apply($text)
187193
$setCodes[] = $this->background['set'];
188194
$unsetCodes[] = $this->background['unset'];
189195
}
190-
if (\count($this->options)) {
191-
foreach ($this->options as $option) {
192-
$setCodes[] = $option['set'];
193-
$unsetCodes[] = $option['unset'];
194-
}
196+
197+
foreach ($this->options as $option) {
198+
$setCodes[] = $option['set'];
199+
$unsetCodes[] = $option['unset'];
200+
}
201+
202+
if (null !== $this->href) {
203+
$text = "\033]8;;$this->href\033\\$text\033]8;;\033\\";
195204
}
196205

197206
if (0 === \count($setCodes)) {

Tests/Formatter/OutputFormatterTest.php

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -228,47 +228,34 @@ public function testFormatToStringObject()
228228
);
229229
}
230230

231-
public function testNotDecoratedFormatter()
231+
public function testFormatterHasStyles()
232232
{
233233
$formatter = new OutputFormatter(false);
234234

235235
$this->assertTrue($formatter->hasStyle('error'));
236236
$this->assertTrue($formatter->hasStyle('info'));
237237
$this->assertTrue($formatter->hasStyle('comment'));
238238
$this->assertTrue($formatter->hasStyle('question'));
239+
}
239240

240-
$this->assertEquals(
241-
'some error', $formatter->format('<error>some error</error>')
242-
);
243-
$this->assertEquals(
244-
'some info', $formatter->format('<info>some info</info>')
245-
);
246-
$this->assertEquals(
247-
'some comment', $formatter->format('<comment>some comment</comment>')
248-
);
249-
$this->assertEquals(
250-
'some question', $formatter->format('<question>some question</question>')
251-
);
252-
$this->assertEquals(
253-
'some text with inline style', $formatter->format('<fg=red>some text with inline style</>')
254-
);
255-
256-
$formatter->setDecorated(true);
241+
/**
242+
* @dataProvider provideDecoratedAndNonDecoratedOutput
243+
*/
244+
public function testNotDecoratedFormatter(string $input, string $expectedNonDecoratedOutput, string $expectedDecoratedOutput)
245+
{
246+
$this->assertEquals($expectedDecoratedOutput, (new OutputFormatter(true))->format($input));
247+
$this->assertEquals($expectedNonDecoratedOutput, (new OutputFormatter(false))->format($input));
248+
}
257249

258-
$this->assertEquals(
259-
"\033[37;41msome error\033[39;49m", $formatter->format('<error>some error</error>')
260-
);
261-
$this->assertEquals(
262-
"\033[32msome info\033[39m", $formatter->format('<info>some info</info>')
263-
);
264-
$this->assertEquals(
265-
"\033[33msome comment\033[39m", $formatter->format('<comment>some comment</comment>')
266-
);
267-
$this->assertEquals(
268-
"\033[30;46msome question\033[39;49m", $formatter->format('<question>some question</question>')
269-
);
270-
$this->assertEquals(
271-
"\033[31msome text with inline style\033[39m", $formatter->format('<fg=red>some text with inline style</>')
250+
public function provideDecoratedAndNonDecoratedOutput()
251+
{
252+
return array(
253+
array('<error>some error</error>', 'some error', "\033[37;41msome error\033[39;49m"),
254+
array('<info>some info</info>', 'some info', "\033[32msome info\033[39m"),
255+
array('<comment>some comment</comment>', 'some comment', "\033[33msome comment\033[39m"),
256+
array('<question>some question</question>', 'some question', "\033[30;46msome question\033[39;49m"),
257+
array('<fg=red>some text with inline style</>', 'some text with inline style', "\033[31msome text with inline style\033[39m"),
258+
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\\"),
272259
);
273260
}
274261

0 commit comments

Comments
 (0)