Skip to content

Commit 4228973

Browse files
committed
Use enums for styles
1 parent e59ac65 commit 4228973

File tree

5 files changed

+49
-45
lines changed

5 files changed

+49
-45
lines changed

src/CLI.php

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99
*/
1010
namespace Framework\CLI;
1111

12-
use InvalidArgumentException;
12+
use Framework\CLI\Styles\BackgroundColor;
13+
use Framework\CLI\Styles\ForegroundColor;
14+
use Framework\CLI\Styles\Format;
1315
use JetBrains\PhpStorm\Pure;
16+
use ValueError;
1417

1518
/**
1619
* Class CLI.
@@ -77,14 +80,14 @@ public static function wrap(string $text, int $width = null) : string
7780
public static function strlen(string $text) : int
7881
{
7982
$codes = [];
80-
foreach (static::$foregroundColors as $color) {
81-
$codes[] = $color;
83+
foreach (ForegroundColor::cases() as $case) {
84+
$codes[] = $case->getCode();
8285
}
83-
foreach (static::$backgroundColors as $background) {
84-
$codes[] = $background;
86+
foreach (BackgroundColor::cases() as $case) {
87+
$codes[] = $case->getCode();
8588
}
86-
foreach (static::$formats as $format) {
87-
$codes[] = $format;
89+
foreach (Format::cases() as $case) {
90+
$codes[] = $case->getCode();
8891
}
8992
$codes[] = static::$reset;
9093
$text = \str_replace($codes, '', $text);
@@ -95,39 +98,36 @@ public static function strlen(string $text) : int
9598
* Applies styles to a text.
9699
*
97100
* @param string $text The text to be styled
98-
* @param string|null $color Foreground color. One of the FG_* constants
99-
* @param string|null $background Background color. One of the BG_* constants
100-
* @param array<int,string> $formats The text format. A list of FM_* constants
101+
* @param ForegroundColor|string|null $color Foreground color
102+
* @param BackgroundColor|string|null $background Background color
103+
* @param array<Format|string> $formats The text formats
101104
*
102-
* @throws InvalidArgumentException For invalid color, background or format
105+
* @throws ValueError For invalid color, background or format
103106
*
104107
* @return string Returns the styled text
105108
*/
106109
public static function style(
107110
string $text,
108-
string $color = null,
109-
string $background = null,
111+
ForegroundColor | string $color = null,
112+
BackgroundColor | string $background = null,
110113
array $formats = []
111114
) : string {
112115
$string = '';
113116
if ($color !== null) {
114-
if (empty(static::$foregroundColors[$color])) {
115-
throw new InvalidArgumentException('Invalid color: ' . $color);
116-
}
117-
$string = static::$foregroundColors[$color];
117+
$string = \is_string($color)
118+
? ForegroundColor::from($color)->getCode()
119+
: $color->getCode();
118120
}
119121
if ($background !== null) {
120-
if (empty(static::$backgroundColors[$background])) {
121-
throw new InvalidArgumentException('Invalid background color: ' . $background);
122-
}
123-
$string .= static::$backgroundColors[$background];
122+
$string .= \is_string($background)
123+
? BackgroundColor::from($background)->getCode()
124+
: $background->getCode();
124125
}
125126
if ($formats) {
126127
foreach ($formats as $format) {
127-
if (empty(static::$formats[$format])) {
128-
throw new InvalidArgumentException('Invalid format: ' . $format);
129-
}
130-
$string .= static::$formats[$format];
128+
$string .= \is_string($format)
129+
? Format::from($format)->getCode()
130+
: $format->getCode();
131131
}
132132
}
133133
$string .= $text . static::$reset;
@@ -140,14 +140,14 @@ public static function style(
140140
* Optionally with styles and width wrapping.
141141
*
142142
* @param string $text The text to be written
143-
* @param string|null $color Foreground color. One of the FG_* constants
144-
* @param string|null $background Background color. One of the BG_* constants
143+
* @param ForegroundColor|string|null $color Foreground color
144+
* @param BackgroundColor|string|null $background Background color
145145
* @param int|null $width Width to wrap the text. Null to do not wrap.
146146
*/
147147
public static function write(
148148
string $text,
149-
string $color = null,
150-
string $background = null,
149+
ForegroundColor | string $color = null,
150+
BackgroundColor | string $background = null,
151151
int $width = null
152152
) : void {
153153
if ($width !== null) {
@@ -214,13 +214,13 @@ public static function beep(int $times = 1, int $usleep = 0) : void
214214
* Writes a message box.
215215
*
216216
* @param array<int,string>|string $lines One line as string or multi-lines as array
217-
* @param string $background Background color. One of the BG_* constants
218-
* @param string $color Foreground color. One of the FG_* constants
217+
* @param BackgroundColor|string $background Background color
218+
* @param ForegroundColor|string $color Foreground color
219219
*/
220220
public static function box(
221221
array | string $lines,
222-
string $background = CLI::BG_BLACK,
223-
string $color = CLI::FG_WHITE
222+
BackgroundColor | string $background = BackgroundColor::black,
223+
ForegroundColor | string $color = ForegroundColor::white
224224
) : void {
225225
$width = static::getWidth();
226226
$width -= 2;
@@ -260,7 +260,7 @@ public static function box(
260260
public static function error(string $message, ?int $exitCode = 1) : void
261261
{
262262
static::beep();
263-
\fwrite(\STDERR, static::style($message, static::FG_RED) . \PHP_EOL);
263+
\fwrite(\STDERR, static::style($message, ForegroundColor::red) . \PHP_EOL);
264264
if ($exitCode !== null) {
265265
exit($exitCode);
266266
}
@@ -319,7 +319,7 @@ public static function prompt(string $question, array | string $options = null)
319319
}
320320
if ($options) {
321321
$opt = $options;
322-
$opt[0] = static::style($opt[0], null, null, [static::FM_BOLD]);
322+
$opt[0] = static::style($opt[0], null, null, [Format::bold]);
323323
$optionsText = isset($opt[1])
324324
? \implode(', ', $opt)
325325
: $opt[0];

src/Commands/About.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Framework\CLI\CLI;
1313
use Framework\CLI\Command;
14+
use Framework\CLI\Styles\ForegroundColor;
1415

1516
/**
1617
* Class About.
@@ -22,7 +23,7 @@ class About extends Command
2223
public function run() : void
2324
{
2425
$lang = $this->console->getLanguage();
25-
CLI::write($lang->render('cli', 'about.line1'), CLI::FG_BRIGHT_GREEN);
26+
CLI::write($lang->render('cli', 'about.line1'), ForegroundColor::brightGreen);
2627
CLI::write($lang->render('cli', 'about.line2'));
2728
CLI::write($lang->render('cli', 'about.line3'));
2829
CLI::write($lang->render('cli', 'about.line4'));

src/Commands/Help.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Framework\CLI\CLI;
1313
use Framework\CLI\Command;
14+
use Framework\CLI\Styles\ForegroundColor;
1415

1516
/**
1617
* Class Help.
@@ -38,27 +39,27 @@ protected function showCommand(string $commandName) : void
3839
}
3940
CLI::write(CLI::style(
4041
$this->console->getLanguage()->render('cli', 'command') . ': ',
41-
CLI::FG_GREEN
42+
ForegroundColor::green
4243
) . $command->getName());
4344
$value = $command->getDescription();
4445
if ($value !== '') {
4546
CLI::write(CLI::style(
4647
$this->console->getLanguage()->render('cli', 'description') . ': ',
47-
CLI::FG_GREEN
48+
ForegroundColor::green
4849
) . $value);
4950
}
5051
$value = $command->getUsage();
5152
if ($value !== '') {
5253
CLI::write(CLI::style(
5354
$this->console->getLanguage()->render('cli', 'usage') . ': ',
54-
CLI::FG_GREEN
55+
ForegroundColor::green
5556
) . $value);
5657
}
5758
$value = $command->getOptions();
5859
if ($value) {
5960
CLI::write(
6061
$this->console->getLanguage()->render('cli', 'options') . ': ',
61-
CLI::FG_GREEN
62+
ForegroundColor::green
6263
);
6364
$lastKey = \array_key_last($value);
6465
foreach ($value as $option => $description) {
@@ -77,7 +78,7 @@ protected function renderOption(string $text) : string
7778
$text = \explode(',', $text);
7879
\sort($text);
7980
foreach ($text as &$item) {
80-
$item = CLI::style($item, CLI::FG_YELLOW);
81+
$item = CLI::style($item, ForegroundColor::yellow);
8182
}
8283
return \implode(', ', $text);
8384
}

src/Commands/Index.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Framework\CLI\CLI;
1313
use Framework\CLI\Command;
14+
use Framework\CLI\Styles\ForegroundColor;
1415

1516
/**
1617
* Class Index.
@@ -60,11 +61,11 @@ protected function listCommands() : void
6061
}
6162
CLI::write(
6263
$this->console->getLanguage()->render('cli', 'commands') . ':',
63-
CLI::FG_YELLOW
64+
ForegroundColor::yellow
6465
);
6566
foreach ($this->console->getCommands() as $name => $command) {
6667
CLI::write(
67-
' ' . CLI::style($name, CLI::FG_GREEN) . ' '
68+
' ' . CLI::style($name, ForegroundColor::green) . ' '
6869
. \str_repeat(' ', $width - $lengths[$name])
6970
. $command->getDescription()
7071
);
@@ -82,7 +83,7 @@ protected function showHeader() : void
8283
|_|
8384

8485
EOL;
85-
CLI::write($text, CLI::FG_GREEN);
86+
CLI::write($text, ForegroundColor::green);
8687
}
8788

8889
protected function showDate() : void

src/Console.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Framework\CLI\Commands\About;
1313
use Framework\CLI\Commands\Help;
1414
use Framework\CLI\Commands\Index;
15+
use Framework\CLI\Styles\ForegroundColor;
1516
use Framework\Language\Language;
1617
use JetBrains\PhpStorm\Pure;
1718

@@ -257,7 +258,7 @@ public function run() : void
257258
if ($command === null) {
258259
CLI::error(CLI::style(
259260
$this->getLanguage()->render('cli', 'commandNotFound', [$this->command]),
260-
CLI::FG_BRIGHT_RED
261+
ForegroundColor::brightRed
261262
), \defined('TESTING') ? null : 1);
262263
return;
263264
}

0 commit comments

Comments
 (0)