|
5 | 5 | namespace Butschster\ContextGenerator\Console\Renderer; |
6 | 6 |
|
7 | 7 | /** |
8 | | - * @deprecated Should be completely redesigned |
| 8 | + * Console output styling utilities |
9 | 9 | */ |
10 | 10 | final class Style |
11 | 11 | { |
12 | 12 | /** |
13 | | - * Format a section header (large titles) |
| 13 | + * Create a header styled text |
14 | 14 | */ |
15 | 15 | public static function header(string $text): string |
16 | 16 | { |
17 | | - return "\033[1;33m" . $text . "\033[0m"; |
| 17 | + return \sprintf('<fg=bright-blue;options=bold>%s</>', $text); |
18 | 18 | } |
19 | 19 |
|
20 | 20 | /** |
21 | | - * Format a title (section names) |
22 | | - */ |
23 | | - public static function title(string $text): string |
24 | | - { |
25 | | - return "\033[1;36m" . $text . "\033[0m"; |
26 | | - } |
27 | | - |
28 | | - /** |
29 | | - * Format a subtitle (smaller section names) |
| 21 | + * Create a separator line |
30 | 22 | */ |
31 | | - public static function subtitle(string $text): string |
| 23 | + public static function separator(string $char = '-', int $length = 80): string |
32 | 24 | { |
33 | | - return "\033[1;34m" . $text . "\033[0m"; |
| 25 | + return \sprintf('<fg=blue>%s</>', \str_repeat($char, $length)); |
34 | 26 | } |
35 | 27 |
|
36 | 28 | /** |
37 | | - * Format a property name |
| 29 | + * Create a property name styled text |
38 | 30 | */ |
39 | 31 | public static function property(string $text): string |
40 | 32 | { |
41 | | - return "\033[1;32m" . $text . "\033[0m"; |
42 | | - } |
43 | | - |
44 | | - /** |
45 | | - * Format a value |
46 | | - */ |
47 | | - public static function value(mixed $value): string |
48 | | - { |
49 | | - if (\is_bool($value)) { |
50 | | - return $value ? "\033[0;32mtrue\033[0m" : "\033[0;31mfalse\033[0m"; |
51 | | - } |
52 | | - |
53 | | - if (\is_string($value)) { |
54 | | - return "\033[0;33m\"" . $value . "\"\033[0m"; |
55 | | - } |
56 | | - |
57 | | - if (\is_null($value)) { |
58 | | - return "\033[0;90mnull\033[0m"; |
59 | | - } |
60 | | - |
61 | | - if (\is_numeric($value)) { |
62 | | - return "\033[0;36m" . (string) $value . "\033[0m"; |
63 | | - } |
64 | | - |
65 | | - return (string) $value; |
| 33 | + return \sprintf('<fg=bright-cyan>%s</>', $text); |
66 | 34 | } |
67 | 35 |
|
68 | 36 | /** |
69 | | - * Format array values |
| 37 | + * Create a label styled text |
70 | 38 | */ |
71 | | - public static function array(array $values): string |
| 39 | + public static function label(string $text): string |
72 | 40 | { |
73 | | - if (empty($values)) { |
74 | | - return "\033[0;90m[]\033[0m"; |
75 | | - } |
76 | | - |
77 | | - if (\array_keys($values) === \range(0, \count($values) - 1)) { |
78 | | - // Sequential array |
79 | | - $formattedItems = \array_map( |
80 | | - static fn($item) => \is_array($item) ? self::array($item) : self::value($item), |
81 | | - $values, |
82 | | - ); |
83 | | - return "\033[0;33m[" . \implode(", ", $formattedItems) . "]\033[0m"; |
84 | | - } |
85 | | - |
86 | | - // Associative array |
87 | | - $result = "{\n"; |
88 | | - foreach ($values as $key => $value) { |
89 | | - $formattedValue = \is_array($value) ? self::array($value) : self::value($value); |
90 | | - $result .= self::indent(self::property('"' . $key . '"') . ": " . $formattedValue) . "\n"; |
91 | | - } |
92 | | - $result .= '}'; |
93 | | - return $result; |
| 41 | + return \sprintf('<fg=yellow>%s</>', $text); |
94 | 42 | } |
95 | 43 |
|
96 | 44 | /** |
97 | | - * Format a key-value pair |
| 45 | + * Create a count styled text |
98 | 46 | */ |
99 | | - public static function keyValue(string $key, mixed $value): string |
| 47 | + public static function count(int $count): string |
100 | 48 | { |
101 | | - $formattedValue = \is_array($value) ? self::array($value) : self::value($value); |
102 | | - return self::property($key) . ": " . $formattedValue; |
| 49 | + return \sprintf('<fg=bright-magenta>%d</>', $count); |
103 | 50 | } |
104 | 51 |
|
105 | 52 | /** |
106 | | - * Format a count indicator |
| 53 | + * Create an item number styled text |
107 | 54 | */ |
108 | | - public static function count(int $count): string |
| 55 | + public static function itemNumber(int $current, int $total): string |
109 | 56 | { |
110 | | - return "\033[1;35m" . $count . "\033[0m"; |
| 57 | + return \sprintf('<fg=bright-yellow>%d/%d</>', $current, $total); |
111 | 58 | } |
112 | 59 |
|
113 | 60 | /** |
114 | | - * Format an item number in a list |
| 61 | + * Create indented text |
115 | 62 | */ |
116 | | - public static function itemNumber(int $number, int $total = 10): string |
| 63 | + public static function indent(string $text, int $spaces = 2): string |
117 | 64 | { |
118 | | - return "\033[1;35m" . $number . '/' . $total . "\033[0m"; |
| 65 | + $indent = \str_repeat(' ', $spaces); |
| 66 | + return $indent . \str_replace("\n", "\n" . $indent, $text); |
119 | 67 | } |
120 | 68 |
|
121 | 69 | /** |
122 | | - * Format path text |
| 70 | + * Create success styled text |
123 | 71 | */ |
124 | | - public static function path(string $path): string |
| 72 | + public static function success(string $text): string |
125 | 73 | { |
126 | | - return "\033[0;36m" . $path . "\033[0m"; |
| 74 | + return \sprintf('<fg=green>%s</>', $text); |
127 | 75 | } |
128 | 76 |
|
129 | 77 | /** |
130 | | - * Format a preview of content |
| 78 | + * Create error styled text |
131 | 79 | */ |
132 | | - public static function contentPreview(string $content): string |
| 80 | + public static function error(string $text): string |
133 | 81 | { |
134 | | - $preview = \substr($content, 0, 100); |
135 | | - if (\strlen($content) > 100) { |
136 | | - $preview .= '...'; |
137 | | - } |
138 | | - return "\033[0;90m\"" . \str_replace(["\r", "\n"], ["\\r", "\\n"], $preview) . "\"\033[0m"; |
| 82 | + return \sprintf('<fg=red>%s</>', $text); |
139 | 83 | } |
140 | 84 |
|
141 | 85 | /** |
142 | | - * Indent a text block |
| 86 | + * Create warning styled text |
143 | 87 | */ |
144 | | - public static function indent(string $text, int $level = 1): string |
| 88 | + public static function warning(string $text): string |
145 | 89 | { |
146 | | - $indent = \str_repeat(' ', $level); |
147 | | - return $indent . \str_replace("\n", "\n" . $indent, $text); |
| 90 | + return \sprintf('<fg=yellow>%s</>', $text); |
148 | 91 | } |
149 | 92 |
|
150 | 93 | /** |
151 | | - * Create a separator line |
| 94 | + * Create highlighted text |
152 | 95 | */ |
153 | | - public static function separator(string $char = '-', int $length = 30): string |
| 96 | + public static function highlight(string $text): string |
154 | 97 | { |
155 | | - return "\033[0;90m" . \str_repeat($char, $length) . "\033[0m"; |
| 98 | + return \sprintf('<fg=bright-green>%s</>', $text); |
156 | 99 | } |
157 | 100 | } |
0 commit comments