Skip to content

Commit 8c5eb38

Browse files
committed
chore: update some for cli json render
1 parent 907e556 commit 8c5eb38

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

src/Component/Formatter/JSONPretty.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use function str_contains;
2424
use function str_ends_with;
2525
use function trim;
26+
use function vdump;
2627

2728
/**
2829
* class JSONPretty
@@ -36,6 +37,21 @@ class JSONPretty extends AbstractObj
3637
'boolVal' => 'red',
3738
];
3839

40+
public const THEME_ONE = [
41+
'keyName' => 'blue',
42+
'strVal' => 'cyan',
43+
'intVal' => 'red',
44+
'boolVal' => 'green',
45+
];
46+
47+
// json.cn
48+
public const THEME_TWO = [
49+
'keyName' => 'mga1',
50+
'strVal' => 'info',
51+
'intVal' => 'hiBlue',
52+
'boolVal' => 'red',
53+
];
54+
3955
/**
4056
* @var array{keyName: string, strVal: string, intVal: string, boolVal: string}
4157
*/
@@ -91,16 +107,16 @@ public function renderData(array $data): string
91107
$val = rtrim($val, ',');
92108
}
93109

94-
// bool val
95-
if ($val === 'true' || $val === 'false') {
110+
// NULL or BOOL val
111+
if ($val === 'null' || $val === 'true' || $val === 'false') {
96112
$val = ColorTag::wrap($val, $this->theme['boolVal']);
97113
} elseif (is_numeric($val)) { // number
98114
$val = ColorTag::wrap($val, $this->theme['intVal']);
99115
} else { // string
100116
$val = ColorTag::wrap($val, $this->theme['strVal']);
101117
}
102118

103-
$buf->writeln($key . ': ' . $val . ($hasEndComma ? ',' : ''));
119+
$buf->writeln($key . ': ' . $val . ($hasEndComma ? ',' : ''));
104120
}
105121

106122
return $buf->getAndClear();

src/Concern/CommandHelpTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ protected function setCommentsVar(string $name, $value): void
8888
public function parseCommentsVars(string $str): string
8989
{
9090
// not use vars
91-
if (false === strpos($str, self::HELP_VAR_LEFT)) {
91+
if (!str_contains($str, self::HELP_VAR_LEFT)) {
9292
return $str;
9393
}
9494

src/Concern/FormatOutputAwareTrait.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Toolkit\Stdlib\Helper\JsonHelper;
1414
use Toolkit\Stdlib\Php;
1515
use function array_merge;
16+
use function count;
1617
use function json_encode;
1718

1819
/**
@@ -36,6 +37,20 @@ public function write($messages, $nl = true, $quit = false, array $opts = []): i
3637
], $opts));
3738
}
3839

40+
// public function print(...$args): void
41+
// {
42+
// if (count($args) > 1) {
43+
// echo ;
44+
// }
45+
//
46+
//
47+
// }
48+
//
49+
// public function echo(...$args): void
50+
// {
51+
// Console::printf($format, ...$args);
52+
// }
53+
3954
/**
4055
* @param string $format
4156
* @param mixed ...$args
@@ -56,7 +71,7 @@ public function printf(string $format, ...$args): void
5671

5772
/**
5873
* @param string|mixed $text
59-
* @param bool $quit
74+
* @param bool|int $quit
6075
* @param array $opts
6176
*
6277
* @return int
@@ -130,14 +145,14 @@ public function prettyJSON($data, string $title = 'JSON:'): void
130145
*/
131146
public function dump(...$vars): void
132147
{
133-
Console::write(Php::dumpVars(...$vars));
148+
Console::writeRaw(Php::dumpVars(...$vars));
134149
}
135150

136151
/**
137152
* @param mixed ...$vars
138153
*/
139154
public function prints(...$vars): void
140155
{
141-
Console::write(Php::printVars(...$vars));
156+
Console::writeRaw(Php::printVars(...$vars));
142157
}
143158
}

0 commit comments

Comments
 (0)