-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(console): add support for printing hyperlinks (#850)
- Loading branch information
Showing
4 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
src/Tempest/Console/src/Highlight/TempestConsoleLanguage/Injections/LinkInjection.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Console\Highlight\TempestConsoleLanguage\Injections; | ||
|
||
use Tempest\Highlight\Highlighter; | ||
use Tempest\Highlight\Injection; | ||
use Tempest\Highlight\ParsedInjection; | ||
use function Tempest\Support\str; | ||
|
||
final readonly class LinkInjection implements Injection | ||
{ | ||
public function parse(string $content, Highlighter $highlighter): ParsedInjection | ||
{ | ||
return new ParsedInjection(preg_replace_callback( | ||
subject: $content, | ||
pattern: '/(?<match>\<href=\"(?<href>.+)\"\>(?:(?!\<href).)*?\<\/href\>)/', | ||
callback: function (array $matches) { | ||
$match = $matches['match']; | ||
$href = $matches['href']; | ||
|
||
return str($match) | ||
->replaceFirst("<href=\"{$href}\">", "\x1b]8;;{$href}\x1b\\") | ||
->replaceLast('</href>', "\x1b]8;;\x1b\\") | ||
->toString(); | ||
}, | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/Tempest/Console/tests/TempestConsoleLanguage/Injections/LinkInjectionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Console\Tests\TempestConsoleLanguage\Injections; | ||
|
||
use PHPUnit\Framework\Attributes\Test; | ||
use PHPUnit\Framework\Attributes\TestWith; | ||
use PHPUnit\Framework\TestCase; | ||
use Tempest\Console\Highlight\TempestConsoleLanguage\Injections\LinkInjection; | ||
use Tempest\Console\Highlight\TempestTerminalTheme; | ||
use Tempest\Highlight\Highlighter; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class LinkInjectionTest extends TestCase | ||
{ | ||
#[Test] | ||
#[TestWith(['<href="https://tempestphp.com">Tempest</href>', "\e]8;;https://tempestphp.com\e\Tempest\e]8;;\e\\"])] | ||
#[TestWith(['<href="http://example.com/path?param=value!@#$%^&*()_+-={}:<>?,./">My link</href>', "\e]8;;http://example.com/path?param=value!@#$%^&*()_+-={}:<>?,./\e\My link\e]8;;\e\\"])] | ||
#[TestWith(['<href="tel:+1234567890">My link</href>', "\e]8;;tel:+1234567890\e\My link\e]8;;\e\\"])] | ||
#[TestWith(['<href="mailto:[email protected]">My link</href>', "\e]8;;mailto:[email protected]\e\My link\e]8;;\e\\"])] | ||
public function language(string $content, string $expected): void | ||
{ | ||
$highlighter = new Highlighter(new TempestTerminalTheme()); | ||
|
||
$this->assertSame( | ||
$expected, | ||
(new LinkInjection())->parse($content, $highlighter)->content, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters