Skip to content

Commit

Permalink
update: update some logic for show simple list
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed May 11, 2022
1 parent fdcf1c6 commit b57d62f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
14 changes: 14 additions & 0 deletions src/Decorate/SubCommandsWareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
use Inhere\Console\Handler\CommandWrapper;
use Inhere\Console\Util\Helper;
use InvalidArgumentException;
use RuntimeException;
use Toolkit\Cli\Color\ColorTag;
use Toolkit\PFlag\FlagsParser;
use Toolkit\Stdlib\Helper\Assert;
use Toolkit\Stdlib\Obj\Traits\NameAliasTrait;
use function array_keys;
Expand Down Expand Up @@ -272,6 +274,18 @@ public function getParent(): ?AbstractHandler
return $this->parent;
}

/**
* @return FlagsParser
*/
public function getParentFlags(): FlagsParser
{
if (!$this->parent) {
throw new RuntimeException('no parent command of the: ' . $this->getCommandName());
}

return $this->parent->getFlags();
}

/**********************************************************
* helper methods
**********************************************************/
Expand Down
40 changes: 25 additions & 15 deletions src/Util/FormatUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
use Toolkit\Sys\Sys;
use function array_merge;
use function array_shift;
use function count;
use function explode;
use function implode;
use function is_array;
use function is_bool;
use function is_int;
use function is_numeric;
use function is_scalar;
use function rtrim;
use function str_repeat;
use function strpos;
use function trim;
Expand Down Expand Up @@ -204,20 +204,9 @@ public static function spliceKeyValue(array $data, array $opts = []): string

$lines = [];

// if value is array, translate array to string
// if value is array, convert array to string
if (is_array($value)) {
$temp = '[';
foreach ($value as $k => $val) {
if (is_bool($val)) {
$val = $val ? '(True)' : '(False)';
} else {
$val = is_scalar($val) ? (string)$val : JsonHelper::unescaped($val);
}

$temp .= (!is_numeric($k) ? "$k: " : '') . "$val, ";
}

$value = rtrim($temp, ' ,') . ']';
$value = self::arr2str($value);
// } elseif (is_object($value)) {
// $value = get_class($value);
} elseif (is_bool($value)) {
Expand All @@ -242,7 +231,7 @@ public static function spliceKeyValue(array $data, array $opts = []): string

// value has multi line
if ($lines) {
$linePrefix = $opts['leftChar'] . Str::repeat(' ', $keyWidth + 1) . $opts['sepChar'];
$linePrefix = $opts['leftChar'] . Str::repeat(' ', $keyWidth) . $opts['sepChar'];
foreach ($lines as $line) {
$fmtLines[] = $linePrefix . $line;
}
Expand All @@ -255,4 +244,25 @@ public static function spliceKeyValue(array $data, array $opts = []): string

return implode("\n", $fmtLines);
}

public static function arr2str(array $arr): string
{
if (count($arr) === 0) {
return '[]';
}

$temp = "[\n";
foreach ($arr as $k => $val) {
if (is_bool($val)) {
$val = $val ? '(True)' : '(False)';
} else {
$val = is_scalar($val) ? (string)$val : JsonHelper::unescaped($val);
}

$temp .= (!is_numeric($k) ? " $k: " : '') . "$val,\n";
}

$temp .= " ]";
return $temp;
}
}

0 comments on commit b57d62f

Please sign in to comment.