-
-
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): support dynamic style injections (#703)
- Loading branch information
Showing
8 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
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
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
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,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Console\Highlight; | ||
|
||
use Tempest\Highlight\Themes\TerminalStyle; | ||
use Tempest\Highlight\Tokens\TokenType; | ||
use function Tempest\Support\str; | ||
|
||
final readonly class DynamicTokenType implements TokenType | ||
{ | ||
public function __construct( | ||
private string $style, | ||
) { | ||
} | ||
|
||
public function getStyle(): TerminalStyle | ||
{ | ||
$normalizedStyle = str($this->style) | ||
->lower() | ||
->replace(['_', '-'], ''); | ||
|
||
foreach (TerminalStyle::cases() as $case) { | ||
$normalizedCase = str($case->name) | ||
->lower() | ||
->replace(['_', '-'], ''); | ||
|
||
if ($normalizedCase->equals($normalizedStyle)) { | ||
return $case; | ||
} | ||
} | ||
|
||
return TerminalStyle::RESET; | ||
} | ||
|
||
public function getValue(): string | ||
{ | ||
return ''; | ||
} | ||
|
||
public function canContain(TokenType $other): bool | ||
{ | ||
return false; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/Tempest/Console/src/Highlight/TempestConsoleLanguage/Injections/DynamicInjection.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,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Console\Highlight\TempestConsoleLanguage\Injections; | ||
|
||
use Tempest\Console\Highlight\ConsoleTokenType; | ||
use Tempest\Console\Highlight\DynamicTokenType; | ||
use Tempest\Highlight\Highlighter; | ||
use Tempest\Highlight\Injection; | ||
use Tempest\Highlight\ParsedInjection; | ||
use function Tempest\Support\str; | ||
|
||
final readonly class DynamicInjection implements Injection | ||
{ | ||
public function getTokenType(): ConsoleTokenType | ||
{ | ||
return ConsoleTokenType::COMMENT; | ||
} | ||
|
||
public function parse(string $content, Highlighter $highlighter): ParsedInjection | ||
{ | ||
$pattern = '/(?<match>\<style=\"(?<styles>(?:[a-z-]+\s*)+)\"\>(.|\n)*\<\/style\>)/'; | ||
|
||
$result = preg_replace_callback( | ||
pattern: $pattern, | ||
callback: function ($matches) use ($highlighter, $pattern) { | ||
$theme = $highlighter->getTheme(); | ||
$content = $matches['match']; | ||
$styles = $matches['styles']; | ||
$before = ''; | ||
$after = ''; | ||
|
||
foreach (explode(' ', $styles) as $style) { | ||
$token = new DynamicTokenType($style); | ||
$before .= $theme->before($token); | ||
$after .= $theme->after($token); | ||
} | ||
|
||
$result = str_replace( | ||
search: $content, | ||
replace: str($content) | ||
->replaceFirst("<style=\"{$styles}\">", $before) | ||
->replaceLast("</style>", $after) | ||
->toString(), | ||
subject: $matches[0], | ||
); | ||
|
||
if (preg_match($pattern, $result)) { | ||
return $this->parse($result, $highlighter)->content; | ||
} | ||
|
||
return $result; | ||
}, | ||
subject: $content, | ||
); | ||
|
||
return new ParsedInjection($result ?? $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
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
38 changes: 38 additions & 0 deletions
38
src/Tempest/Console/tests/TempestConsoleLanguage/Injections/DynamicInjectionTest.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,38 @@ | ||
<?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\DynamicInjection; | ||
use Tempest\Console\Highlight\TempestTerminalTheme; | ||
use Tempest\Highlight\Highlighter; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class DynamicInjectionTest extends TestCase | ||
{ | ||
#[TestWith(['<style="fg-cyan">foo</style>', "\e[96mfoo\e[0m"])] | ||
#[TestWith(['<style="bg-red">foo</style>', "\e[101mfoo\e[0m"])] | ||
#[TestWith(['<style="bold">foo</style>', "\e[1mfoo\e[0m"])] | ||
#[TestWith(['<style="underline">foo</style>', "\e[4mfoo\e[0m"])] | ||
#[TestWith(['<style="reset">foo</style>', "\e[0mfoo\e[0m"])] | ||
#[TestWith(['<style="reverse-text">foo</style>', "\e[7mfoo\e[0m"])] | ||
#[TestWith(['<style="bg-darkcyan fg-cyan underline">Tempest</style>', "\e[46m\e[96m\e[4mTempest\e[0m\e[0m\e[0m"])] | ||
#[TestWith(['<style="bg-dark-cyan fg-cyan underline">Tempest</style>', "\e[46m\e[96m\e[4mTempest\e[0m\e[0m\e[0m"])] | ||
#[TestWith(['<style="fg-cyan"><style="bg-dark-red">foo</style></style>', "\e[96m\e[41mfoo\e[0m\e[0m"])] | ||
#[Test] | ||
public function language(string $content, string $expected): void | ||
{ | ||
$highlighter = new Highlighter(new TempestTerminalTheme()); | ||
|
||
$this->assertSame( | ||
$expected, | ||
(new DynamicInjection())->parse($content, $highlighter)->content, | ||
); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
tests/Integration/Console/Highlight/TempestConsoleLanguage/TempestConsoleLanguageTest.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,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Tempest\Integration\Console\Highlight\LogLanguage; | ||
|
||
use PHPUnit\Framework\Attributes\Test; | ||
use PHPUnit\Framework\Attributes\TestWith; | ||
use PHPUnit\Framework\TestCase; | ||
use Tempest\Console\Highlight\TempestConsoleLanguage\TempestConsoleLanguage; | ||
use Tempest\Console\Highlight\TempestTerminalTheme; | ||
use Tempest\Highlight\Highlighter; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class TempestConsoleLanguageTest extends TestCase | ||
{ | ||
#[TestWith(['<style="fg-cyan">foo</style>', "\e[96mfoo\e[0m"])] | ||
#[TestWith(['<style="bg-red">foo</style>', "\e[101mfoo\e[0m"])] | ||
#[TestWith(['<style="bold">foo</style>', "\e[1mfoo\e[0m"])] | ||
#[TestWith(['<style="underline">foo</style>', "\e[4mfoo\e[0m"])] | ||
#[TestWith(['<style="reset">foo</style>', "\e[0mfoo\e[0m"])] | ||
#[TestWith(['<style="reverse-text">foo</style>', "\e[7mfoo\e[0m"])] | ||
#[TestWith(['<style="bg-darkcyan fg-cyan underline">Tempest</style>', "\e[46m\e[96m\e[4mTempest\e[0m\e[0m\e[0m"])] | ||
#[TestWith(['<style="bg-dark-cyan fg-cyan underline">Tempest</style>', "\e[46m\e[96m\e[4mTempest\e[0m\e[0m\e[0m"])] | ||
#[TestWith(['<style="fg-cyan"><style="bg-dark-red">foo</style></style>', "\e[96m\e[41mfoo\e[0m\e[0m"])] | ||
#[Test] | ||
public function language(string $content, string $expected): void | ||
{ | ||
$highlighter = new Highlighter(new TempestTerminalTheme()); | ||
|
||
$this->assertSame( | ||
$expected, | ||
$highlighter->parse($content, new TempestConsoleLanguage()) | ||
); | ||
} | ||
} |