Cleanup value formatting code
This commit is contained in:
parent
9e880467d9
commit
4eae7bedad
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace SlotDb\Cli\Command\Group;
|
namespace SlotDb\Cli\Command\Group;
|
||||||
|
|
||||||
|
use SlotDb\Cli\Tool\Format;
|
||||||
use SlotDb\Client\SlotDbClient;
|
use SlotDb\Client\SlotDbClient;
|
||||||
use Symfony\Component\Console\Attribute\AsCommand;
|
use Symfony\Component\Console\Attribute\AsCommand;
|
||||||
use Symfony\Component\Console\Command\Command;
|
use Symfony\Component\Console\Command\Command;
|
||||||
@ -44,12 +45,7 @@ class GroupFindCommand extends Command
|
|||||||
foreach ($keys as $key) {
|
foreach ($keys as $key) {
|
||||||
$row[] = isset($group[$key]) ? $group[$key] : null;
|
$row[] = isset($group[$key]) ? $group[$key] : null;
|
||||||
}
|
}
|
||||||
$row = array_map(fn($v) => match(true) {
|
$row = array_map(Format::formatValue(...), $row);
|
||||||
is_bool($v) => "<fg=magenta>".($v?"true":"false")."</>",
|
|
||||||
is_int($v) => "<fg=cyan>{$v}</>",
|
|
||||||
is_string($v) => "<fg=yellow>{$v}</>",
|
|
||||||
default => $v,
|
|
||||||
}, $row);
|
|
||||||
$table->addRow($row);
|
$table->addRow($row);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace SlotDb\Cli\Command\Slot;
|
namespace SlotDb\Cli\Command\Slot;
|
||||||
|
|
||||||
|
use SlotDb\Cli\Tool\Format;
|
||||||
use SlotDb\Client\SlotDbClient;
|
use SlotDb\Client\SlotDbClient;
|
||||||
use Symfony\Component\Console\Attribute\AsCommand;
|
use Symfony\Component\Console\Attribute\AsCommand;
|
||||||
use Symfony\Component\Console\Command\Command;
|
use Symfony\Component\Console\Command\Command;
|
||||||
@ -57,12 +58,7 @@ class SlotFindCommand extends Command
|
|||||||
foreach ($keys as $key) {
|
foreach ($keys as $key) {
|
||||||
$row[] = isset($slot[$key]) ? $slot[$key] : null;
|
$row[] = isset($slot[$key]) ? $slot[$key] : null;
|
||||||
}
|
}
|
||||||
$row = array_map(fn($v) => match(true) {
|
$row = array_map(Format::formatValue(...), $row);
|
||||||
is_bool($v) => "<fg=magenta>".($v?"true":"false")."</>",
|
|
||||||
is_int($v) => "<fg=cyan>{$v}</>",
|
|
||||||
is_string($v) => "<fg=yellow>{$v}</>",
|
|
||||||
default => $v,
|
|
||||||
}, $row);
|
|
||||||
$table->addRow($row);
|
$table->addRow($row);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace SlotDb\Cli\Command\Slot;
|
namespace SlotDb\Cli\Command\Slot;
|
||||||
|
|
||||||
|
use SlotDb\Cli\Tool\Format;
|
||||||
use SlotDb\Client\SlotDbClient;
|
use SlotDb\Client\SlotDbClient;
|
||||||
use Symfony\Component\Console\Attribute\AsCommand;
|
use Symfony\Component\Console\Attribute\AsCommand;
|
||||||
use Symfony\Component\Console\Command\Command;
|
use Symfony\Component\Console\Command\Command;
|
||||||
@ -42,9 +43,9 @@ class SlotQueryCommand extends Command
|
|||||||
$output->writeln(
|
$output->writeln(
|
||||||
sprintf(
|
sprintf(
|
||||||
str_starts_with($k,"_")
|
str_starts_with($k,"_")
|
||||||
? "<fg=green>%{$len}s</>: <fg=gray>%s</>"
|
? "<fg=green>%{$len}s</>: %s"
|
||||||
: "<fg=green;options=bold>%{$len}s</>: <fg=yellow>%s</>",
|
: "<fg=green;options=bold>%{$len}s</>: %s",
|
||||||
$k, json_encode($v)
|
$k, Format::formatValue($v)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
26
src/Tool/Format.php
Normal file
26
src/Tool/Format.php
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SlotDb\Cli\Tool;
|
||||||
|
|
||||||
|
class Format
|
||||||
|
{
|
||||||
|
public static function formatValue(mixed $v): string
|
||||||
|
{
|
||||||
|
return match(true) {
|
||||||
|
is_array($v) =>
|
||||||
|
"[<fg=gray></><fg=green>".
|
||||||
|
join(",", array_map(Format::formatValue(...), $v)).
|
||||||
|
"</>]<fg=gray>(".count($v).")</>",
|
||||||
|
is_bool($v) =>
|
||||||
|
"<fg=cyan>".($v?"true":"false")."</>",
|
||||||
|
is_int($v) =>
|
||||||
|
"<fg=yellow>{$v}</>",
|
||||||
|
is_string($v) =>
|
||||||
|
"<fg=green>{$v}</>",
|
||||||
|
is_null($v) =>
|
||||||
|
"<fg=blue>null</>",
|
||||||
|
default =>
|
||||||
|
$v,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user