Skip to content

Commit b57d62f

Browse files
committed
update: update some logic for show simple list
1 parent fdcf1c6 commit b57d62f

File tree

2 files changed

+39
-15
lines changed

2 files changed

+39
-15
lines changed

Diff for: src/Decorate/SubCommandsWareTrait.php

+14
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
use Inhere\Console\Handler\CommandWrapper;
1717
use Inhere\Console\Util\Helper;
1818
use InvalidArgumentException;
19+
use RuntimeException;
1920
use Toolkit\Cli\Color\ColorTag;
21+
use Toolkit\PFlag\FlagsParser;
2022
use Toolkit\Stdlib\Helper\Assert;
2123
use Toolkit\Stdlib\Obj\Traits\NameAliasTrait;
2224
use function array_keys;
@@ -272,6 +274,18 @@ public function getParent(): ?AbstractHandler
272274
return $this->parent;
273275
}
274276

277+
/**
278+
* @return FlagsParser
279+
*/
280+
public function getParentFlags(): FlagsParser
281+
{
282+
if (!$this->parent) {
283+
throw new RuntimeException('no parent command of the: ' . $this->getCommandName());
284+
}
285+
286+
return $this->parent->getFlags();
287+
}
288+
275289
/**********************************************************
276290
* helper methods
277291
**********************************************************/

Diff for: src/Util/FormatUtil.php

+25-15
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
use Toolkit\Sys\Sys;
1818
use function array_merge;
1919
use function array_shift;
20+
use function count;
2021
use function explode;
2122
use function implode;
2223
use function is_array;
2324
use function is_bool;
2425
use function is_int;
2526
use function is_numeric;
2627
use function is_scalar;
27-
use function rtrim;
2828
use function str_repeat;
2929
use function strpos;
3030
use function trim;
@@ -204,20 +204,9 @@ public static function spliceKeyValue(array $data, array $opts = []): string
204204

205205
$lines = [];
206206

207-
// if value is array, translate array to string
207+
// if value is array, convert array to string
208208
if (is_array($value)) {
209-
$temp = '[';
210-
foreach ($value as $k => $val) {
211-
if (is_bool($val)) {
212-
$val = $val ? '(True)' : '(False)';
213-
} else {
214-
$val = is_scalar($val) ? (string)$val : JsonHelper::unescaped($val);
215-
}
216-
217-
$temp .= (!is_numeric($k) ? "$k: " : '') . "$val, ";
218-
}
219-
220-
$value = rtrim($temp, ' ,') . ']';
209+
$value = self::arr2str($value);
221210
// } elseif (is_object($value)) {
222211
// $value = get_class($value);
223212
} elseif (is_bool($value)) {
@@ -242,7 +231,7 @@ public static function spliceKeyValue(array $data, array $opts = []): string
242231

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

256245
return implode("\n", $fmtLines);
257246
}
247+
248+
public static function arr2str(array $arr): string
249+
{
250+
if (count($arr) === 0) {
251+
return '[]';
252+
}
253+
254+
$temp = "[\n";
255+
foreach ($arr as $k => $val) {
256+
if (is_bool($val)) {
257+
$val = $val ? '(True)' : '(False)';
258+
} else {
259+
$val = is_scalar($val) ? (string)$val : JsonHelper::unescaped($val);
260+
}
261+
262+
$temp .= (!is_numeric($k) ? " $k: " : '') . "$val,\n";
263+
}
264+
265+
$temp .= " ]";
266+
return $temp;
267+
}
258268
}

0 commit comments

Comments
 (0)