Skip to content

Commit b656353

Browse files
committed
up: update some color tag logic
1 parent 70a75bc commit b656353

File tree

8 files changed

+22
-1071
lines changed

8 files changed

+22
-1071
lines changed

Diff for: src/App.php

-721
This file was deleted.

Diff for: src/Cli.php

+12-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use RuntimeException;
1313
use Toolkit\Cli\Color\Alert;
1414
use Toolkit\Cli\Color\Prompt;
15+
use Toolkit\Cli\Color\ColorTag;
1516
use Toolkit\Cli\Traits\ReadMessageTrait;
1617
use Toolkit\Cli\Traits\WriteMessageTrait;
1718
use function count;
@@ -24,6 +25,7 @@
2425
use function is_numeric;
2526
use function json_encode;
2627
use function preg_replace;
28+
use function sprintf;
2729
use function stream_isatty;
2830
use function strtoupper;
2931
use function trim;
@@ -135,23 +137,27 @@ public static function color(string $text, array|string $style = null): string
135137
* @param string $msg
136138
* @param array $data
137139
* @param string $type
138-
* @param array $opts
140+
* @param array{writeOpts:array} $labels
139141
* [
140142
* '_category' => 'application',
141143
* 'process' => 'work',
142144
* 'pid' => 234,
143145
* 'coId' => 12,
144146
* ]
145147
*/
146-
public static function clog(string $msg, array $data = [], string $type = 'info', array $opts = []): void
148+
public static function clog(string $msg, array $data = [], string $type = 'info', array $labels = []): void
147149
{
148150
if (isset(self::LOG_LEVEL2TAG[$type])) {
149151
$type = ColorTag::add(strtoupper($type), self::LOG_LEVEL2TAG[$type]);
150152
}
151153

152-
$userOpts = [];
154+
$userOpts = $writeOpt = [];
155+
if (isset($labels['writeOpts'])) {
156+
$writeOpt = $labels['writeOpts'];
157+
unset($labels['writeOpts']);
158+
}
153159

154-
foreach ($opts as $n => $v) {
160+
foreach ($labels as $n => $v) {
155161
if (is_numeric($n) || str_starts_with($n, '_')) {
156162
$userOpts[] = "[$v]";
157163
} else {
@@ -162,7 +168,8 @@ public static function clog(string $msg, array $data = [], string $type = 'info'
162168
$optString = $userOpts ? ' ' . implode(' ', $userOpts) : '';
163169
$dataString = $data ? PHP_EOL . json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) : '';
164170

165-
self::writef("%s [%s]%s %s %s\n", date('Y/m/d H:i:s'), $type, $optString, trim($msg), $dataString);
171+
$msg = sprintf("%s [%s]%s %s %s", date('Y/m/d H:i:s'), $type, $optString, trim($msg), $dataString);
172+
self::writeln($msg, false, $writeOpt);
166173
}
167174

168175
/*******************************************************************************

Diff for: src/Color.php

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Toolkit\Cli\Color\ANSICode;
1313
use Toolkit\Cli\Color\ColorCode;
14+
use Toolkit\Cli\Color\ColorTag;
1415
use function array_filter;
1516
use function array_keys;
1617
use function implode;

Diff for: src/ColorTag.php

-12
This file was deleted.

0 commit comments

Comments
 (0)