Skip to content

Commit 056f68a

Browse files
committed
Renamed namespace and functions
Signed-off-by: Jack Cherng <jfcherng@gmail.com>
1 parent e036a8d commit 056f68a

File tree

5 files changed

+82
-65
lines changed

5 files changed

+82
-65
lines changed

README.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ composer require jfcherng/php-color-output
3232
```php
3333
<?php
3434

35-
// a global alias to \Jfcherng\Color\Colorful::color
36-
function str_color(string $str, $colors = [], bool $autoReset = true): string
35+
// a global alias to \Jfcherng\Utility\CliColor::color
36+
function str_cli_color(string $str, $colors = [], bool $reset = true): string
3737

38-
// a global alias to \Jfcherng\Color\Colorful::noColor
39-
function str_nocolor(string $str): string
38+
// a global alias to \Jfcherng\Utility\CliColor::noColor
39+
function str_cli_nocolor(string $str): string
4040
```
4141

4242

@@ -48,13 +48,13 @@ function str_nocolor(string $str): string
4848
/**
4949
* Make a string colorful.
5050
*
51-
* @param string $str the string
52-
* @param array|string $colors the colors
53-
* @param bool $autoReset automatically reset at the end of the string?
51+
* @param string $str the string
52+
* @param string|string[] $colors the colors
53+
* @param bool $reset reset color at the end of the string?
5454
*
5555
* @return string the colored string
5656
*/
57-
\Jfcherng\Color\Colorful::color(string $str, $colors = [], bool $autoReset = true): string
57+
\Jfcherng\Utility\CliColor::color(string $str, $colors = [], bool $reset = true): string
5858

5959
/**
6060
* Remove all colors from a string.
@@ -63,7 +63,7 @@ function str_nocolor(string $str): string
6363
*
6464
* @return string the string without colors
6565
*/
66-
\Jfcherng\Color\Colorful::noColor(string $str): string
66+
\Jfcherng\Utility\CliColor::noColor(string $str): string
6767
```
6868

6969

@@ -75,25 +75,25 @@ function str_nocolor(string $str): string
7575
include __DIR__ . '/vendor/autoload.php';
7676

7777
// colors in a string using a comma as the delimiter
78-
echo str_color('foo', 'f_light_cyan, b_yellow'); // "\033[1;36;43mfoo\033[0m"
78+
echo str_cli_color('foo', 'f_light_cyan, b_yellow'); // "\033[1;36;43mfoo\033[0m"
7979

8080
echo PHP_EOL;
8181

8282
// colors in an array
83-
echo str_color('foo', ['f_white', 'b_magenta']); // "\033[1;37;45mfoo\033[0m"
83+
echo str_cli_color('foo', ['f_white', 'b_magenta']); // "\033[1;37;45mfoo\033[0m"
8484

8585
echo PHP_EOL;
8686

8787
// do not auto reset color at the end of string
88-
echo str_color('foo', ['f_red', 'b_green', 'b', 'blk'], false); // "\033[31;42;1;5mfoo"
88+
echo str_cli_color('foo', ['f_red', 'b_green', 'b', 'blk'], false); // "\033[31;42;1;5mfoo"
8989

9090
// manually add color reset
91-
echo str_color('', 'reset'); // "\033[0m"
91+
echo str_cli_color('', 'reset'); // "\033[0m"
9292

9393
echo PHP_EOL;
9494

9595
// remove all color codes from a string
96-
echo str_nocolor("\033[31;42;5mfoo\033[0mbar"); // "foobar"
96+
echo str_cli_nocolor("\033[31;42;5mfoo\033[0mbar"); // "foobar"
9797

9898
echo PHP_EOL;
9999
```

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@
1919
"prefer-stable": true,
2020
"autoload": {
2121
"psr-4": {
22-
"Jfcherng\\Color\\": "src/"
22+
"Jfcherng\\Utility\\": "src/"
2323
},
2424
"files": [
2525
"src/helpers.php"
2626
]
2727
},
2828
"autoload-dev": {
2929
"psr-4": {
30-
"Jfcherng\\Color\\Test\\": "tests/"
30+
"Jfcherng\\Utility\\Test\\": "tests/"
3131
}
3232
},
3333
"require": {

src/Colorful.php renamed to src/CliColor.php

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Jfcherng\Color;
5+
namespace Jfcherng\Utility;
66

77
/**
88
* Make your PHP command-line application colorful.
@@ -11,7 +11,7 @@
1111
*
1212
* @author Jack Cherng <jfcherng@gmail.com>
1313
*/
14-
class Colorful
14+
final class CliColor
1515
{
1616
const COLOR_BEGIN = "\033[";
1717
const COLOR_END = 'm';
@@ -22,7 +22,7 @@ class Colorful
2222
/**
2323
* @var array the color map
2424
*/
25-
protected static $colorMap = [
25+
private static $colorMap = [
2626
// background
2727
'b_black' => '40',
2828
'b_blue' => '44',
@@ -82,32 +82,32 @@ class Colorful
8282
*/
8383
public static function getColorMap(): array
8484
{
85-
return static::$colorMap;
85+
return self::$colorMap;
8686
}
8787

8888
/**
8989
* Make a string colorful.
9090
*
91-
* @param string $str the string
92-
* @param array|string $colors the colors
93-
* @param bool $autoReset automatically reset at the end of the string?
91+
* @param string $str the string
92+
* @param string|string[] $colors the colors
93+
* @param bool $reset reset color at the end of the string?
9494
*
9595
* @return string the colored string
9696
*/
97-
public static function color(string $str, $colors = [], bool $autoReset = true): string
97+
public static function color(string $str, $colors = [], bool $reset = true): string
9898
{
9999
// always convert $colors into an array
100100
if (\is_string($colors)) {
101101
$colors = \explode(',', $colors);
102102
}
103103

104-
$colored = static::getColorCode($colors) . $str;
104+
$colored = self::getColorCode($colors) . $str;
105105

106-
if ($autoReset) {
107-
$colored .= static::getColorCode(['reset']);
106+
if ($reset) {
107+
$colored .= self::getColorCode(['reset']);
108108
}
109109

110-
return static::simplifyColoredString($colored);
110+
return self::simplifyColoredString($colored);
111111
}
112112

113113
/**
@@ -120,7 +120,7 @@ public static function color(string $str, $colors = [], bool $autoReset = true):
120120
public static function noColor(string $str): string
121121
{
122122
return \preg_replace(
123-
'~' . static::getColorCode(['regex_any'], true) . '~uS',
123+
'~' . self::getColorCode(['regex_any'], true) . '~uS',
124124
'',
125125
$str
126126
);
@@ -134,18 +134,20 @@ public static function noColor(string $str): string
134134
*
135135
* @return string the color code
136136
*/
137-
protected static function getColorCode(array $colors, bool $returnRegex = false): string
137+
private static function getColorCode(array $colors, bool $returnRegex = false): string
138138
{
139-
$colors = static::sanitizeColors($colors);
139+
$colors = self::sanitizeColors($colors);
140140

141141
if (empty($colors)) {
142142
return '';
143143
}
144144

145+
// convert color into color code
145146
$colorCodes = \array_map(
146147
function (string $color): string {
147-
while (isset(static::$colorMap[$color])) {
148-
$color = static::$colorMap[$color];
148+
// resolve color alias
149+
while (isset(self::$colorMap[$color])) {
150+
$color = self::$colorMap[$color];
149151
}
150152

151153
return $color;
@@ -154,8 +156,8 @@ function (string $color): string {
154156
);
155157

156158
$closures = $returnRegex
157-
? [static::COLOR_BEGIN_REGEX, static::COLOR_END_REGEX]
158-
: [static::COLOR_BEGIN, static::COLOR_END];
159+
? [self::COLOR_BEGIN_REGEX, self::COLOR_END_REGEX]
160+
: [self::COLOR_BEGIN, self::COLOR_END];
159161

160162
return $closures[0] . \implode(';', $colorCodes) . $closures[1];
161163
}
@@ -167,12 +169,12 @@ function (string $color): string {
167169
*
168170
* @return array the sanitized colors
169171
*/
170-
protected static function sanitizeColors(array $colors): array
172+
private static function sanitizeColors(array $colors): array
171173
{
172-
return \array_unique(\array_filter(
174+
return self::listUnique(\array_filter(
173175
\array_map('trim', $colors),
174176
function (string $color): bool {
175-
return isset(static::$colorMap[$color]);
177+
return isset(self::$colorMap[$color]);
176178
}
177179
));
178180
}
@@ -184,11 +186,11 @@ function (string $color): bool {
184186
*
185187
* @return string the simplified colored string
186188
*/
187-
protected static function simplifyColoredString(string $str): string
189+
private static function simplifyColoredString(string $str): string
188190
{
189191
// replace multiple consecutive resets with a single reset
190192
$str = \preg_replace(
191-
'~(' . static::getColorCode(['reset'], true) . '){2,}~uS',
193+
'~(' . self::getColorCode(['reset'], true) . '){2,}~uS',
192194
'$1',
193195
$str
194196
);
@@ -197,8 +199,8 @@ protected static function simplifyColoredString(string $str): string
197199
$str = \preg_replace(
198200
(
199201
'~' .
200-
'(' . static::getColorCode(['regex_any'], true) . ')' .
201-
'(' . static::getColorCode(['reset'], true) . ')' .
202+
'(' . self::getColorCode(['regex_any'], true) . ')' .
203+
'(' . self::getColorCode(['reset'], true) . ')' .
202204
'~uS'
203205
),
204206
'$2',
@@ -207,4 +209,18 @@ protected static function simplifyColoredString(string $str): string
207209

208210
return $str;
209211
}
212+
213+
/**
214+
* The fastest array_unique() implementation for a non-associative array AFAIK.
215+
*
216+
* @see https://stackoverflow.com/questions/8321620/array-unique-vs-array-flip
217+
*
218+
* @param array $array the array
219+
*
220+
* @return array
221+
*/
222+
private static function listUnique(array $array): array
223+
{
224+
return \array_keys(\array_count_values($array));
225+
}
210226
}

src/helpers.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,36 @@
22

33
declare(strict_types=1);
44

5-
if (!\function_exists('str_color')) {
5+
if (!\function_exists('str_cli_color')) {
66
/**
77
* Make a string colorful.
88
*
9-
* A global alias to \Jfcherng\Color\Colorful::color.
9+
* A global alias to \Jfcherng\Utility\CliColor::color.
1010
*
1111
* @param string $str the string
1212
* @param array|string $colors the colors
1313
* @param bool $autoReset automatically reset at the end of the string?
1414
*
1515
* @return string the colored string
1616
*/
17-
function str_color(string $str, $colors = [], bool $autoReset = true): string
17+
function str_cli_color(string $str, $colors = [], bool $autoReset = true): string
1818
{
19-
return \Jfcherng\Color\Colorful::color($str, $colors, $autoReset);
19+
return \Jfcherng\Utility\CliColor::color($str, $colors, $autoReset);
2020
}
2121
}
2222

23-
if (!\function_exists('str_nocolor')) {
23+
if (!\function_exists('str_cli_nocolor')) {
2424
/**
2525
* Remove all colors from a string.
2626
*
27-
* A global alias to \Jfcherng\Color\Colorful::noColor
27+
* A global alias to \Jfcherng\Utility\CliColor::noColor
2828
*
2929
* @param string $str the string
3030
*
3131
* @return string the string without colors
3232
*/
33-
function str_nocolor(string $str): string
33+
function str_cli_nocolor(string $str): string
3434
{
35-
return \Jfcherng\Color\Colorful::noColor($str);
35+
return \Jfcherng\Utility\CliColor::noColor($str);
3636
}
3737
}

tests/ColorfulTest.php renamed to tests/CliColorTest.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22

33
declare(strict_types=1);
44

5-
namespace Jfcherng\Color\Test;
5+
namespace Jfcherng\Utility\Test;
66

7-
use Jfcherng\Color\Colorful;
7+
use Jfcherng\Utility\CliColor;
88
use PHPUnit\Framework\TestCase;
99

1010
/**
11-
* @covers \Jfcherng\Color\Colorful
11+
* @covers \Jfcherng\Utility\CliColor
1212
*
1313
* @internal
1414
*/
15-
final class ColorfulTest extends TestCase
15+
final class CliColorTest extends TestCase
1616
{
1717
/**
18-
* Provide testcases for testing Colorful::color.
18+
* Provide testcases for testing CliColor::color.
1919
*
2020
* @return array the testcases
2121
*/
@@ -47,7 +47,8 @@ public function colorTestcaseProvider(): array
4747
'foo' . "\033[0m",
4848
],
4949
[
50-
['foo', 'b_green, b_green'],
50+
// repeated colors only output once
51+
['foo', 'b_green, b_green, b_green, b_green'],
5152
"\033[42m" . 'foo' . "\033[0m",
5253
],
5354
[
@@ -70,7 +71,7 @@ public function colorTestcaseProvider(): array
7071
}
7172

7273
/**
73-
* Provide testcases for testing Colorful::noColor.
74+
* Provide testcases for testing CliColor::noColor.
7475
*
7576
* @return array the testcases
7677
*/
@@ -85,28 +86,28 @@ public function noColorTestcaseProvider(): array
8586
}
8687

8788
/**
88-
* Test Colorful::color.
89+
* Test CliColor::color.
8990
*
9091
* @dataProvider colorTestcaseProvider
9192
*
92-
* @param array $inputs The inputs
93-
* @param string $expected The expected
93+
* @param array $inputs the inputs
94+
* @param string $expected the expected
9495
*/
9596
public function testColor(array $inputs, string $expected): void
9697
{
97-
static::assertSame($expected, Colorful::color(...$inputs));
98+
static::assertSame($expected, CliColor::color(...$inputs));
9899
}
99100

100101
/**
101-
* Test Colorful::noColor.
102+
* Test CliColor::noColor.
102103
*
103104
* @dataProvider noColorTestcaseProvider
104105
*
105-
* @param string $inputs The input
106-
* @param string $expected The expected
106+
* @param string $inputs the input
107+
* @param string $expected the expected
107108
*/
108109
public function testNoColor(string $input, string $expected): void
109110
{
110-
static::assertSame($expected, Colorful::noColor($input));
111+
static::assertSame($expected, CliColor::noColor($input));
111112
}
112113
}

0 commit comments

Comments
 (0)