Skip to content

Commit 28f6cd0

Browse files
committed
up: update the list data show logic
1 parent 6934fc4 commit 28f6cd0

File tree

5 files changed

+46
-33
lines changed

5 files changed

+46
-33
lines changed

src/AbstractApplication.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ abstract class AbstractApplication implements ApplicationInterface
9090
'debug' => Console::VERB_ERROR,
9191
'profile' => false,
9292
'version' => '0.5.1',
93+
'homepage' => '', // can provide you app homepage url
9394
'publishAt' => '2017.03.24',
9495
'updateAt' => '2019.01.01',
9596
'rootPath' => '',
@@ -384,7 +385,7 @@ public function handleError(int $num, string $str, string $file, int $line): voi
384385
*
385386
* @throws InvalidArgumentException
386387
*/
387-
public function handleException($e): void
388+
public function handleException(Throwable $e): void
388389
{
389390
// you can log error on sub class ...
390391
$this->errorHandler->handle($e, $this);
@@ -501,7 +502,6 @@ protected function startInteractiveShell(): void
501502
$in->parse($args);
502503
$in->setFullScript($line);
503504

504-
// \vdump($in);
505505
$this->run(false);
506506
$out->println('');
507507
}

src/Component/Formatter/SingleList.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ class SingleList extends MessageFormatter
2929
* ];
3030
* ```
3131
*
32-
* @param array $data
33-
* @param string $title
34-
* @param array $opts More {@see FormatUtil::spliceKeyValue()}
32+
* @param array|mixed $data
33+
* @param string $title
34+
* @param array $opts More {@see FormatUtil::spliceKeyValue()}
3535
*
3636
* @return int|string
3737
*/
@@ -52,7 +52,7 @@ public static function show($data, string $title = 'Information', array $opts =
5252

5353
// title
5454
if ($title) {
55-
$title = $opts['ucTitleWords'] ? Str::ucwords(trim($title)) : $title;
55+
$title = $opts['ucTitleWords'] ? Str::ucwords(trim($title)) : $title;
5656
$string .= ColorTag::wrap($title, $opts['titleStyle']) . PHP_EOL;
5757
}
5858

src/Concern/ApplicationHelpTrait.php

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Inhere\Console\Contract\CommandInterface;
1515
use Inhere\Console\IO\Input;
1616
use Inhere\Console\IO\Output;
17-
use Inhere\Console\Router;
1817
use Inhere\Console\Util\FormatUtil;
1918
use Inhere\Console\Util\Show;
2019
use Toolkit\Cli\ColorTag;
@@ -39,6 +38,7 @@
3938
use const PHP_EOL;
4039
use const PHP_OS;
4140
use const PHP_VERSION;
41+
use const STR_PAD_LEFT;
4242

4343
/**
4444
* Trait ApplicationHelpTrait
@@ -61,32 +61,46 @@ trait ApplicationHelpTrait
6161
*/
6262
public function showVersionInfo(): void
6363
{
64-
$os = PHP_OS;
65-
$date = date('Y.m.d');
64+
$this->fire(ConsoleEvent::BEFORE_RENDER_APP_VERSION, $this);
65+
66+
Show::aList($this->buildVersionInfo(), '', [
67+
'leftChar' => '',
68+
'sepChar' => ' : ',
69+
'keyPadPos' => STR_PAD_LEFT,
70+
]);
71+
}
72+
73+
/**
74+
* @return string[]
75+
*/
76+
protected function buildVersionInfo(): array
77+
{
6678
$logo = '';
79+
$date = date('Y.m.d');
6780
$name = $this->getParam('name', 'Console Application');
6881

69-
$version = $this->getParam('version', 'Unknown');
70-
$publishAt = $this->getParam('publishAt', 'Unknown');
71-
$updateAt = $this->getParam('updateAt', 'Unknown');
72-
$phpVersion = PHP_VERSION;
82+
$osName = PHP_OS;
83+
$phpVer = PHP_VERSION;
84+
$version = $this->getParam('version', 'Unknown');
7385

74-
$this->fire(ConsoleEvent::BEFORE_RENDER_APP_VERSION, $this);
86+
$updateAt = $this->getParam('updateAt', 'Unknown');
87+
$publishAt = $this->getParam('publishAt', 'Unknown');
7588

7689
if ($logoTxt = $this->getLogoText()) {
7790
$logo = ColorTag::wrap($logoTxt, $this->getLogoStyle());
7891
}
7992

80-
/** @var Output $out */
81-
$out = $this->output;
82-
$out->aList([
93+
$info = [
8394
"$logo\n <info>$name</info>, Version <comment>$version</comment>\n",
84-
'System Info' => "PHP version <info>$phpVersion</info>, on <info>$os</info> system",
95+
'System Info' => "PHP version <info>$phpVer</info>, on <info>$osName</info> system",
8596
'Application Info' => "Update at <info>$updateAt</info>, publish at <info>$publishAt</info>(current $date)",
86-
], '', [
87-
'leftChar' => '',
88-
'sepChar' => ' : '
89-
]);
97+
];
98+
99+
if ($hUrl = $this->getParam('homepage')) {
100+
$info['Homepage URL'] = $hUrl;
101+
}
102+
103+
return $info;
90104
}
91105

92106
/**
@@ -183,8 +197,6 @@ public function showCommandList(): void
183197

184198
$this->logf(Console::VERB_DEBUG, 'Display the application commands list');
185199

186-
/** @var Output $output */ // $output = $this->output;
187-
/** @var Router $router */
188200
$router = $this->getRouter();
189201

190202
$hasGroup = $hasCommand = false;
@@ -306,7 +318,6 @@ protected function dumpAutoCompletion(string $shellEnv, array $data): void
306318
$input = $this->input;
307319
/** @var Output $output */
308320
$output = $this->output;
309-
/** @var Router $router */
310321
$router = $this->getRouter();
311322

312323
// info

src/Util/FormatUtil.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010

1111
use Toolkit\Cli\ColorTag;
1212
use Toolkit\Stdlib\Helper\JsonHelper;
13+
use Toolkit\Stdlib\Str;
1314
use Toolkit\Sys\Sys;
1415
use function array_keys;
1516
use function array_merge;
1617
use function count;
1718
use function date;
1819
use function explode;
1920
use function floor;
20-
use function gettype;
2121
use function implode;
2222
use function is_array;
2323
use function is_bool;
@@ -26,13 +26,13 @@
2626
use function is_scalar;
2727
use function rtrim;
2828
use function sprintf;
29-
use function str_pad;
3029
use function str_repeat;
3130
use function str_replace;
3231
use function strpos;
3332
use function trim;
3433
use function ucfirst;
3534
use function wordwrap;
35+
use const STR_PAD_RIGHT;
3636

3737
/**
3838
* Class FormatUtil
@@ -232,7 +232,7 @@ public static function howLongAgo(int $secs): string
232232
}
233233

234234
/**
235-
* splice Array
235+
* Splice array
236236
*
237237
* @param array $data
238238
* e.g [
@@ -251,12 +251,13 @@ public static function spliceKeyValue(array $data, array $opts = []): string
251251
'sepChar' => ' ', // e.g ' | ' OUT: key | value
252252
'keyStyle' => '', // e.g 'info','comment'
253253
'valStyle' => '', // e.g 'info','comment'
254+
'keyPadPos' => STR_PAD_RIGHT,
254255
'keyMinWidth' => 8,
255-
'keyMaxWidth' => null, // if not set, will automatic calculation
256+
'keyMaxWidth' => 0, // if not set, will automatic calculation
256257
'ucFirst' => true, // upper first char for value
257258
], $opts);
258259

259-
if (!is_numeric($opts['keyMaxWidth'])) {
260+
if ($opts['keyMaxWidth'] < 1) {
260261
$opts['keyMaxWidth'] = Helper::getKeyMaxWidth($data);
261262
}
262263

@@ -265,14 +266,15 @@ public static function spliceKeyValue(array $data, array $opts = []): string
265266
$opts['keyMaxWidth'] = $opts['keyMinWidth'];
266267
}
267268

268-
$keyStyle = trim($opts['keyStyle']);
269+
$keyStyle = trim($opts['keyStyle']);
270+
$keyPadPos = (int)$opts['keyPadPos'];
269271

270272
foreach ($data as $key => $value) {
271273
$hasKey = !is_int($key);
272274
$text .= $opts['leftChar'];
273275

274276
if ($hasKey && $opts['keyMaxWidth']) {
275-
$key = str_pad((string)$key, $opts['keyMaxWidth'], ' ');
277+
$key = Str::pad((string)$key, $opts['keyMaxWidth'], ' ', $keyPadPos);
276278
$text .= ColorTag::wrap($key, $keyStyle) . $opts['sepChar'];
277279
}
278280

src/Util/Show.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public static function padding(array $data, string $title = '', array $opts = []
288288
* ];
289289
* ```
290290
*
291-
* @param array $data
291+
* @param array|object $data
292292
* @param string $title
293293
* @param array $opts More {@see FormatUtil::spliceKeyValue()}
294294
*

0 commit comments

Comments
 (0)