Cleanup value formatting code

This commit is contained in:
Chris 2025-03-16 21:37:03 +01:00
parent 9e880467d9
commit 4eae7bedad
4 changed files with 34 additions and 15 deletions

View File

@ -2,6 +2,7 @@
namespace SlotDb\Cli\Command\Group;
use SlotDb\Cli\Tool\Format;
use SlotDb\Client\SlotDbClient;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
@ -44,12 +45,7 @@ class GroupFindCommand extends Command
foreach ($keys as $key) {
$row[] = isset($group[$key]) ? $group[$key] : null;
}
$row = array_map(fn($v) => match(true) {
is_bool($v) => "<fg=magenta>".($v?"true":"false")."</>",
is_int($v) => "<fg=cyan>{$v}</>",
is_string($v) => "<fg=yellow>{$v}</>",
default => $v,
}, $row);
$row = array_map(Format::formatValue(...), $row);
$table->addRow($row);
}

View File

@ -2,6 +2,7 @@
namespace SlotDb\Cli\Command\Slot;
use SlotDb\Cli\Tool\Format;
use SlotDb\Client\SlotDbClient;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
@ -57,12 +58,7 @@ class SlotFindCommand extends Command
foreach ($keys as $key) {
$row[] = isset($slot[$key]) ? $slot[$key] : null;
}
$row = array_map(fn($v) => match(true) {
is_bool($v) => "<fg=magenta>".($v?"true":"false")."</>",
is_int($v) => "<fg=cyan>{$v}</>",
is_string($v) => "<fg=yellow>{$v}</>",
default => $v,
}, $row);
$row = array_map(Format::formatValue(...), $row);
$table->addRow($row);
}

View File

@ -2,6 +2,7 @@
namespace SlotDb\Cli\Command\Slot;
use SlotDb\Cli\Tool\Format;
use SlotDb\Client\SlotDbClient;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
@ -42,9 +43,9 @@ class SlotQueryCommand extends Command
$output->writeln(
sprintf(
str_starts_with($k,"_")
? "<fg=green>%{$len}s</>: <fg=gray>%s</>"
: "<fg=green;options=bold>%{$len}s</>: <fg=yellow>%s</>",
$k, json_encode($v)
? "<fg=green>%{$len}s</>: %s"
: "<fg=green;options=bold>%{$len}s</>: %s",
$k, Format::formatValue($v)
)
);
}

26
src/Tool/Format.php Normal file
View 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,
};
}
}