Skip to content

Commit 58191ea

Browse files
committed
feat: create basic style tag renderer
1 parent 68a3ea6 commit 58191ea

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

src/Sprout/Command.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function option(string $param)
6464
*/
6565
public function write(string $data)
6666
{
67-
echo $data;
67+
echo Style\Renderer::render($data);
6868
}
6969

7070
/**

src/Sprout/Style/Renderer.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Leaf\Sprout\Style;
6+
7+
class Renderer
8+
{
9+
protected static array $styleTagMap = [
10+
'error' => "\033[1;37;41m",
11+
'info' => "\033[1;34m",
12+
'comment' => "\033[1;33m",
13+
'question' => "\033[30;46m",
14+
'b' => "\033[1m",
15+
'u' => "\033[4m",
16+
'i' => "\033[3m",
17+
'reset' => "\033[0m",
18+
];
19+
20+
/**
21+
* Render text with styles
22+
* @param string $text The text to render
23+
* @return string
24+
*/
25+
public static function render(string $text)
26+
{
27+
return static::renderTags($text);
28+
}
29+
30+
/**
31+
* Render text with parsed style tags
32+
* @param string $text The text to render
33+
* @return string
34+
*/
35+
public static function renderTags(string $text): string
36+
{
37+
foreach (static::$styleTagMap as $tag => $ansi) {
38+
$text = preg_replace("/<$tag>/i", $ansi, $text);
39+
$text = preg_replace("/<\/$tag>/i", "\033[0m", $text); // reset on closing tag
40+
}
41+
42+
return $text;
43+
}
44+
45+
/**
46+
* Update tag map
47+
*/
48+
public static function tags(array $newTags)
49+
{
50+
static::$styleTagMap = array_merge(
51+
static::$styleTagMap,
52+
$newTags
53+
);
54+
}
55+
}

0 commit comments

Comments
 (0)