Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0c82710

Browse files
committed
fix: cli-utils cannot render UpperCase color tag
1 parent 1e31c80 commit 0c82710

File tree

7 files changed

+166
-99
lines changed

7 files changed

+166
-99
lines changed

‎libs/cli-utils/src/App.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ class App
4343
*/
4444
private $messages = [];
4545

46-
/** @var int */
46+
/**
47+
* @var int
48+
*/
4749
private $keyWidth = 12;
4850

4951
/**
@@ -61,7 +63,6 @@ public function __construct(array $argv = null)
6163
$this->script = \array_shift($argv);
6264
// parse flags
6365
[$this->args, $this->opts] = Flags::simpleParseArgv($argv);
64-
6566
}
6667

6768
/**
@@ -178,6 +179,10 @@ public function addCommand(string $command, callable $handler, string $descripti
178179
throw new \InvalidArgumentException('Invalid arguments');
179180
}
180181

182+
if (($len = \strlen($command)) > $this->keyWidth) {
183+
$this->keyWidth = $len;
184+
}
185+
181186
$this->commands[$command] = $handler;
182187
$this->messages[$command] = \trim($description);
183188
}
@@ -219,7 +224,7 @@ public function displayHelp(string $err = ''): void
219224
$help = "Welcome to the Lite Console Application.\n\n<comment>Available Commands:</comment>\n";
220225

221226
foreach ($this->messages as $command => $desc) {
222-
$command = str_pad($command, $commandWidth, '');
227+
$command = \str_pad($command, $commandWidth, '');
223228
$desc = $desc ?: 'No description for the command';
224229
$help .= "$command$desc\n";
225230
}

‎libs/cli-utils/src/Cli.php

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,33 @@
88

99
namespace Toolkit\Cli;
1010

11+
use function getenv;
12+
1113
/**
1214
* Class Cli
1315
* @package Toolkit\Cli
1416
*/
1517
class Cli
1618
{
19+
public const LOG_LEVEL2TAG = [
20+
'info' => 'info',
21+
'warn' => 'warning',
22+
'warning' => 'warning',
23+
'debug' => 'cyan',
24+
'notice' => 'notice',
25+
'error' => 'error',
26+
];
27+
1728
/*******************************************************************************
1829
* read/write message
1930
******************************************************************************/
2031

2132
/**
22-
* @param mixed $message
33+
* @param string $message
2334
* @param bool $nl
2435
* @return string
2536
*/
26-
public static function read($message = null, $nl = false): string
37+
public static function read(string$message = '', bool $nl = false): string
2738
{
2839
if ($message) {
2940
self::write($message, $nl);
@@ -33,15 +44,24 @@ public static function read($message = null, $nl = false): string
3344
}
3445

3546
/**
36-
* write message to console
37-
* @param $messages
38-
* @param bool $nl
39-
* @param bool $quit
47+
* @param string $format
48+
* @param mixed ...$args
4049
*/
41-
public static function write($messages, $nl = true, $quit = false): void
50+
public static function writef(string $format, ...$args): void
51+
{
52+
self::write(\sprintf($format, ...$args));
53+
}
54+
55+
/**
56+
* Write message to console
57+
* @param string|array $messages
58+
* @param bool $nl
59+
* @param bool|int $quit
60+
*/
61+
public static function write($messages, bool $nl = true, $quit = false): void
4262
{
4363
if (\is_array($messages)) {
44-
$messages = implode($nl ? \PHP_EOL : '', $messages);
64+
$messages = \implode($nl ? \PHP_EOL : '', $messages);
4565
}
4666

4767
self::stdout(Color::parseTag($messages), $nl, $quit);
@@ -53,10 +73,10 @@ public static function write($messages, $nl = true, $quit = false): void
5373
* @param bool $nl
5474
* @param bool|int $quit
5575
*/
56-
public static function stdout(string $message, $nl = true, $quit = false): void
76+
public static function stdout(string $message, bool$nl = true, $quit = false): void
5777
{
58-
fwrite(\STDOUT, $message . ($nl ? \PHP_EOL : ''));
59-
fflush(\STDOUT);
78+
\fwrite(\STDOUT, $message . ($nl ? \PHP_EOL : ''));
79+
\fflush(\STDOUT);
6080

6181
if (($isTrue = true === $quit) || \is_int($quit)) {
6282
$code = $isTrue ? 0 : $quit;
@@ -72,8 +92,8 @@ public static function stdout(string $message, $nl = true, $quit = false): void
7292
*/
7393
public static function stderr(string $message, $nl = true, $quit = -1): void
7494
{
75-
fwrite(\STDERR, self::color('[ERROR] ', 'red') . $message . ($nl ? PHP_EOL : ''));
76-
fflush(\STDOUT);
95+
\fwrite(\STDERR, self::color('[ERROR] ', 'red') . $message . ($nl ? PHP_EOL : ''));
96+
\fflush(\STDOUT);
7797

7898
if (($isTrue = true === $quit) || \is_int($quit)) {
7999
$code = $isTrue ? 0 : $quit;
@@ -95,6 +115,47 @@ public static function color(string $text, $style = null): string
95115
return Color::render($text, $style);
96116
}
97117

118+
/**
119+
* print log to console
120+
* @param string $msg
121+
* @param array $data
122+
* @param string $type
123+
* @param array $opts
124+
* [
125+
* '_category' => 'application',
126+
* 'process' => 'work',
127+
* 'pid' => 234,
128+
* 'coId' => 12,
129+
* ]
130+
*/
131+
public static function log(string $msg, array $data = [], string $type = 'info', array $opts = []): void
132+
{
133+
if (isset(self::LOG_LEVEL2TAG[$type])) {
134+
$type = ColorTag::add(\strtoupper($type), self::LOG_LEVEL2TAG[$type]);
135+
}
136+
137+
$userOpts = [];
138+
139+
foreach ($opts as $n => $v) {
140+
if (\is_numeric($n) || \strpos($n, '_') === 0) {
141+
$userOpts[] = "[$v]";
142+
} else {
143+
$userOpts[] = "[$n:$v]";
144+
}
145+
}
146+
147+
$optString = $userOpts ? '' . \implode('', $userOpts) : '';
148+
149+
self::write(\sprintf(
150+
'%s [%s]%s %s %s',
151+
\date('Y/m/d H:i:s'),
152+
$type,
153+
$optString,
154+
\trim($msg),
155+
$data ? \PHP_EOL . \json_encode($data, \JSON_UNESCAPED_SLASHES | \JSON_PRETTY_PRINT) : ''
156+
));
157+
}
158+
98159
/*******************************************************************************
99160
* some helpers
100161
******************************************************************************/

‎libs/cli-utils/src/Color.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
*/
3535
class Color
3636
{
37+
public const RESET = 0;
3738
public const NORMAL = 0;
3839

3940
// Foreground color
@@ -164,7 +165,7 @@ class Color
164165
* @param array $args
165166
* @return string
166167
*/
167-
public static function __callStatic($method, array $args)
168+
public static function __callStatic(string$method, array $args)
168169
{
169170
if (isset(self::STYLES[$method])) {
170171
return self::render($args[0], $method);
@@ -173,12 +174,8 @@ public static function __callStatic($method, array $args)
173174
return '';
174175
}
175176

176-
/*******************************************************************************
177-
* color render
178-
******************************************************************************/
179-
180177
/**
181-
* apply style for text
178+
* Apply style for text
182179
* @param string $style
183180
* @param string $text
184181
* @return string
@@ -189,7 +186,21 @@ public static function apply(string $style, string $text): string
189186
}
190187

191188
/**
192-
* render text
189+
* Format and print to STDOUT
190+
* @param string $format
191+
* @param mixed ...$args
192+
*/
193+
public static function printf(string $format, ...$args): void
194+
{
195+
echo self::render(\sprintf($format, ...$args));
196+
}
197+
198+
/*******************************************************************************
199+
* color render
200+
******************************************************************************/
201+
202+
/**
203+
* Render text, apply color code
193204
* @param string $text
194205
* @param string|array $style
195206
* - string: 'green', 'blue'
@@ -214,7 +225,7 @@ public static function render(string $text, $style = null): string
214225
$color = \implode(';', $style);
215226

216227
// user color tag: <info>message</info>
217-
} elseif (\strpos($text, '<') !== false) {
228+
} elseif (\strpos($text, '</') > 0) {
218229
return self::parseTag($text);
219230
} else {
220231
return $text;
@@ -240,7 +251,7 @@ public static function parseTag(string $text)
240251
return static::clearColor($text);
241252
}
242253

243-
if (!\preg_match_all(self::COLOR_TAG, $text, $matches)) {
254+
if (!\preg_match_all(ColorTag::MATCH_TAG, $text, $matches)) {
244255
return $text;
245256
}
246257

‎libs/cli-utils/src/ColorTag.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
class ColorTag
1616
{
1717
// regex used for removing color tags
18-
private const STRIP_TAG = '/<[\/]?[a-zA-Z=;]+>/';
18+
public const STRIP_TAG = '/<[\/]?[a-zA-Z=;]+>/';
1919

2020
// Regex to match tags/
21-
private const COLOR_TAG = '/<([a-zA-Z=;]+)>(.*?)<\/\\1>/s';
21+
public const MATCH_TAG = '/<([a-zA-Z=;_]+)>(.*?)<\/\\1>/s';
2222

2323
/**
24-
* alias of the wrap()
24+
* Alias of the wrap()
2525
* @param string $text
26+
* @param string $tag
2627
* @return string
2728
*/
2829
public static function add(string $text, string $tag): string
@@ -51,7 +52,7 @@ public static function wrap(string $text, string $tag): string
5152
*/
5253
public static function matchAll(string $text): array
5354
{
54-
if (!\preg_match_all(self::COLOR_TAG, $text, $matches)) {
55+
if (!\preg_match_all(self::MATCH_TAG, $text, $matches)) {
5556
return [];
5657
}
5758

@@ -64,17 +65,17 @@ public static function parse(string $text): string
6465
}
6566

6667
/**
67-
* exists color tags
68+
* Exists color tags
6869
* @param string $text
6970
* @return bool
7071
*/
7172
public static function exists(string $text): bool
7273
{
73-
return false !== \strpos($text, '</');
74+
return \strpos($text, '</') > 0;
7475
}
7576

7677
/**
77-
* alias of the strip()
78+
* Alias of the strip()
7879
* @param string $text
7980
* @return string
8081
*/

‎libs/cli-utils/src/Console.php

Lines changed: 0 additions & 66 deletions
This file was deleted.

‎libs/cli-utils/test/ColorTagTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,26 @@ class ColorTagTest extends TestCase
2020
public function testMatchAll(): void
2121
{
2222
$ret = ColorTag::matchAll('<tag>text0</tag> or <info>text1</info>');
23-
2423
$this->assertCount(3, $ret);
2524
// tag
2625
$this->assertSame('tag', $ret[1][0]);
2726
$this->assertSame('info', $ret[1][1]);
2827
// content
2928
$this->assertSame('text0', $ret[2][0]);
29+
30+
$ret = ColorTag::matchAll('<some_tag>text</some_tag>');
31+
$this->assertCount(3, $ret);
32+
// tag
33+
$this->assertSame('some_tag', $ret[1][0]);
34+
// content
35+
$this->assertSame('text', $ret[2][0]);
36+
37+
$ret = ColorTag::matchAll('<someTag>text</someTag>');
38+
$this->assertCount(3, $ret);
39+
// tag
40+
$this->assertSame('someTag', $ret[1][0]);
41+
// content
42+
$this->assertSame('text', $ret[2][0]);
3043
}
3144

3245
public function testStrip(): void

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /