Skip to content
This repository was archived by the owner on Jan 23, 2019. It is now read-only.

Commit dbcb029

Browse files
committed
some bug fixed. update
1 parent 5536a51 commit dbcb029

File tree

3 files changed

+50
-19
lines changed

3 files changed

+50
-19
lines changed

src/console/ConsoleHelper.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static function isInteractive($fileDescriptor)
4848
* ]
4949
* @return int
5050
*/
51-
public static function keyMaxWidth(array $data, $expactInt = true)
51+
public static function getKeyMaxWidth(array $data, $expactInt = true)
5252
{
5353
$keyMaxWidth = 0;
5454

@@ -71,18 +71,31 @@ public static function keyMaxWidth(array $data, $expactInt = true)
7171
* 'version' => '4.4.5',
7272
* ]
7373
* @param int $keyMaxWidth
74-
* @param string $sepChar e.g ' | ' => OUT: key | value
75-
* @param string $leftChar e.g ' * '
74+
* @param array $opts
7675
* @return string
7776
*/
78-
public static function spliceKeyValue(array $data, $keyMaxWidth, $sepChar = ' ', $leftChar='')
77+
public static function spliceKeyValue(array $data, array $opts = [])
7978
{
8079
$text = '';
80+
$opts = array_merge([
81+
'leftChar' => '', // e.g ' ', ' * '
82+
'sepChar' => ' ', // e.g ' | ' => OUT: key | value
83+
'keyStyle' => '', // e.g 'info','commont'
84+
'keyMaxWidth' => null, // if not set, will automatic calculation
85+
], $opts);
86+
87+
if ( !is_numeric($opts['keyMaxWidth']) ) {
88+
$opts['keyMaxWidth'] = self::getKeyMaxWidth($data);
89+
}
90+
91+
$keyStyle = trim($opts['keyStyle']);
92+
8193
foreach ($data as $key => $value) {
82-
$text .= $leftChar;
94+
$text .= $opts['leftChar'];
8395

84-
if ($keyMaxWidth) {
85-
$text .= str_pad($key, $keyMaxWidth, ' ') . $sepChar;
96+
if ($opts['keyMaxWidth']) {
97+
$key = str_pad($key, $opts['keyMaxWidth'], ' ');
98+
$text .= ( $keyStyle ? "<{$keyStyle}>$key</{$keyStyle}> " : $key ) . $opts['sepChar'];
8699
}
87100

88101
$text .= "$value\n";

src/console/Interact.php

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -305,39 +305,49 @@ public static function section($msg, $width = 50, $char = '-')
305305
* ]
306306
* @param array $examples The command usage example. e.g 'php server.php {start|reload|restart|stop} [-d]'
307307
* @param string $description The description text. e.g 'Composer version 1.3.2'
308+
* @param bool $showAfterQuit Show help after quit
308309
*/
309-
public static function consoleHelp($usage, $commands = [], $options = [], $examples = [], $description = '')
310+
public static function consoleHelp($usage, $commands = [], $options = [], $examples = [], $description = '', $showAfterQuit = true)
310311
{
311-
self::helpPanel($usage, $commands, $options, $examples, $description);
312+
self::helpPanel($usage, $commands, $options, $examples, $description, $showAfterQuit);
312313
}
313-
public static function helpPanel($usage, $commands = [], $options = [], $examples = [], $description = '')
314+
public static function helpPanel($usage, $commands = [], $options = [], $examples = [], $description = '', $showAfterQuit = true)
314315
{
316+
// description
317+
if ( $description ) {
318+
self::write($description . PHP_EOL);
319+
}
320+
315321
// usage
316322
self::write("<comment>Usage</comment>:\n {$usage}\n");
317323

318324
// options list
319325
if ( $options ) {
320326
// translate array to string
321327
if ( is_array($options)) {
322-
$optionMaxWidth = ConsoleHelper::keyMaxWidth($options);
323-
$options = ConsoleHelper::spliceKeyValue($options, $optionMaxWidth);
328+
$options = ConsoleHelper::spliceKeyValue($options, [
329+
'leftChar' => ' ',
330+
'keyStyle' => 'info',
331+
]);
324332
}
325333

326334
if ( is_string($options) ) {
327-
self::write("<comment>Options</comment>:\n {$options}\n");
335+
self::write("<comment>Options</comment>:\n{$options}");
328336
}
329337
}
330338

331339
// command list
332340
if ( $commands ) {
333341
// translate array to string
334342
if ( is_array($commands)) {
335-
$commandMaxWidth = ConsoleHelper::keyMaxWidth($commands);
336-
$commands = ConsoleHelper::spliceKeyValue($commands, $commandMaxWidth);
343+
$commands = ConsoleHelper::spliceKeyValue($commands, [
344+
'leftChar' => ' ',
345+
'keyStyle' => 'info',
346+
]);
337347
}
338348

339349
if ( is_string($commands) ) {
340-
self::write("<comment>Commands</comment>:\n {$commands}\n");
350+
self::write("<comment>Commands</comment>:\n{$commands}");
341351
}
342352
}
343353

@@ -346,6 +356,10 @@ public static function helpPanel($usage, $commands = [], $options = [], $example
346356
$examples = is_array($examples) ? implode(PHP_EOL, $examples) : $examples;
347357
self::write("<comment>Examples</comment>:\n {$examples}\n");
348358
}
359+
360+
if ($showAfterQuit) {
361+
exit(0);
362+
}
349363
}
350364

351365
/**
@@ -418,7 +432,11 @@ public static function panel($data, $title='Information Panel', $char = '*')
418432
self::write(' ' . $border);
419433

420434
// output panel body
421-
$panelStr = ConsoleHelper::spliceKeyValue($panelData, $labelMaxWidth, ' | ', " $char ");
435+
$panelStr = ConsoleHelper::spliceKeyValue($panelData, [
436+
'leftChar' => " $char ",
437+
'sepChar' => ' | ',
438+
'keyMaxWidth' => $labelMaxWidth,
439+
]);
422440

423441
// already exists "\n"
424442
self::write($panelStr, false);

src/console/Output.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ public function section($msg, $width = null)
5757
* helpPanel
5858
* @see Interact::helpPanel()
5959
*/
60-
public function helpPanel($usage, $commands = [], $options = [], $examples = [], $description = '')
60+
public function helpPanel($usage, $commands = [], $options = [], $examples = [], $description = '', $showAfterQuit = true)
6161
{
62-
Interact::helpPanel($msg, $width);
62+
Interact::helpPanel($usage, $commands, $options, $examples, $description, $showAfterQuit);
6363
}
6464

6565
/**

0 commit comments

Comments
 (0)