Skip to content

Commit

Permalink
chore: update some for cli json render
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 1, 2021
1 parent 907e556 commit 8c5eb38
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
22 changes: 19 additions & 3 deletions src/Component/Formatter/JSONPretty.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use function str_contains;
use function str_ends_with;
use function trim;
use function vdump;

/**
* class JSONPretty
Expand All @@ -36,6 +37,21 @@ class JSONPretty extends AbstractObj
'boolVal' => 'red',
];

public const THEME_ONE = [
'keyName' => 'blue',
'strVal' => 'cyan',
'intVal' => 'red',
'boolVal' => 'green',
];

// json.cn
public const THEME_TWO = [
'keyName' => 'mga1',
'strVal' => 'info',
'intVal' => 'hiBlue',
'boolVal' => 'red',
];

/**
* @var array{keyName: string, strVal: string, intVal: string, boolVal: string}
*/
Expand Down Expand Up @@ -91,16 +107,16 @@ public function renderData(array $data): string
$val = rtrim($val, ',');
}

// bool val
if ($val === 'true' || $val === 'false') {
// NULL or BOOL val
if ($val === 'null' || $val === 'true' || $val === 'false') {
$val = ColorTag::wrap($val, $this->theme['boolVal']);
} elseif (is_numeric($val)) { // number
$val = ColorTag::wrap($val, $this->theme['intVal']);
} else { // string
$val = ColorTag::wrap($val, $this->theme['strVal']);
}

$buf->writeln($key . ': ' . $val . ($hasEndComma ? ',' : ''));
$buf->writeln($key . ': ' . $val . ($hasEndComma ? ',' : ''));
}

return $buf->getAndClear();
Expand Down
2 changes: 1 addition & 1 deletion src/Concern/CommandHelpTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected function setCommentsVar(string $name, $value): void
public function parseCommentsVars(string $str): string
{
// not use vars
if (false === strpos($str, self::HELP_VAR_LEFT)) {
if (!str_contains($str, self::HELP_VAR_LEFT)) {
return $str;
}

Expand Down
21 changes: 18 additions & 3 deletions src/Concern/FormatOutputAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Toolkit\Stdlib\Helper\JsonHelper;
use Toolkit\Stdlib\Php;
use function array_merge;
use function count;
use function json_encode;

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

// public function print(...$args): void
// {
// if (count($args) > 1) {
// echo ;
// }
//
//
// }
//
// public function echo(...$args): void
// {
// Console::printf($format, ...$args);
// }

/**
* @param string $format
* @param mixed ...$args
Expand All @@ -56,7 +71,7 @@ public function printf(string $format, ...$args): void

/**
* @param string|mixed $text
* @param bool $quit
* @param bool|int $quit
* @param array $opts
*
* @return int
Expand Down Expand Up @@ -130,14 +145,14 @@ public function prettyJSON($data, string $title = 'JSON:'): void
*/
public function dump(...$vars): void
{
Console::write(Php::dumpVars(...$vars));
Console::writeRaw(Php::dumpVars(...$vars));
}

/**
* @param mixed ...$vars
*/
public function prints(...$vars): void
{
Console::write(Php::printVars(...$vars));
Console::writeRaw(Php::printVars(...$vars));
}
}

0 comments on commit 8c5eb38

Please sign in to comment.