Skip to content

Commit bd4f38a

Browse files
committed
update for titile display. support custom ishell name prefix
1 parent 4800cf0 commit bd4f38a

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

src/AbstractApplication.php

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

1111
use ErrorException;
1212
use Inhere\Console\Component\ErrorHandler;
13+
use Inhere\Console\Component\Formatter\Title;
1314
use Inhere\Console\Concern\StyledOutputAwareTrait;
1415
use Inhere\Console\Contract\ApplicationInterface;
1516
use Inhere\Console\Contract\ErrorHandlerInterface;
@@ -34,6 +35,7 @@
3435
use function header;
3536
use function in_array;
3637
use function is_int;
38+
use function json_encode;
3739
use function memory_get_usage;
3840
use function microtime;
3941
use function register_shutdown_function;
@@ -90,6 +92,7 @@ abstract class AbstractApplication implements ApplicationInterface
9092
'publishAt' => '2017.03.24',
9193
'updateAt' => '2019.01.01',
9294
'rootPath' => '',
95+
'ishellName' => '', // name prefix on i-shell env.
9396
'strictMode' => false,
9497
'hideRootPath' => true,
9598
// global options
@@ -412,7 +415,9 @@ protected function startInteractiveShell(): void
412415
$in = $this->input;
413416
$out = $this->output;
414417

415-
$out->colored("Will start interactive shell for run application");
418+
$out->title("Welcome interactive shell for run application", [
419+
'titlePos' => Title::POS_MIDDLE,
420+
]);
416421

417422
if (!($hasPcntl = ProcessUtil::hasPcntl())) {
418423
$this->debugf('php is not enable "pcntl" extension, cannot listen CTRL+C signal');
@@ -426,14 +431,19 @@ protected function startInteractiveShell(): void
426431
});
427432
}
428433

434+
$prefix = $this->getParam('ishellName') ?: $this->getName();
435+
if (!$prefix) {
436+
$prefix = 'CMD';
437+
}
438+
429439
$exitKeys = [
430440
'q' => 1,
431441
'quit' => 1,
432442
'exit' => 1,
433443
];
434444

435445
while (true) {
436-
$line = Interact::readln('<comment>CMD ></comment> ');
446+
$line = Interact::readln("<comment>$prefix ></comment> ");
437447
if (strlen($line) < 5) {
438448
if (isset($exitKeys[$line])) {
439449
break;
@@ -451,9 +461,11 @@ protected function startInteractiveShell(): void
451461
}
452462

453463
$args = LineParser::parseIt($line);
464+
$this->debugf('input line: %s, parsed args: %s', $line, json_encode($args));
454465

455466
// reload and parse args
456467
$in->parse($args);
468+
$in->setFullScript($line);
457469

458470
// \vdump($in);
459471
$this->run(false);

src/Component/Formatter/Title.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Inhere\Console\Component\MessageFormatter;
66
use Inhere\Console\Console;
7+
use Toolkit\Cli\ColorTag;
78
use Toolkit\Stdlib\Str;
89
use Toolkit\Sys\Sys;
910
use function array_merge;
@@ -26,15 +27,16 @@ public static function show(string $title, array $opts = []): void
2627
'width' => 80,
2728
'char' => self::CHAR_EQUAL,
2829
'titlePos' => self::POS_LEFT,
29-
'indent' => 2,
30+
'titleStyle' => 'bold',
31+
'indent' => 0,
3032
'ucWords' => true,
3133
'showBorder' => true,
3234
], $opts);
3335

3436
// list($sW, $sH) = Helper::getScreenSize();
35-
$width = (int)$opts['width'];
36-
$char = trim($opts['char']);
37-
$indent = (int)$opts['indent'] >= 0 ? $opts['indent'] : 2;
37+
$width = (int)$opts['width'];
38+
$char = trim($opts['char']);
39+
$indent = (int)$opts['indent'] >= 0 ? $opts['indent'] : 2;
3840

3941
$indentStr = '';
4042
if ($indent > 0) {
@@ -61,8 +63,10 @@ public static function show(string $title, array $opts = []): void
6163
$titleIndent = Str::pad(self::CHAR_SPACE, $indent, self::CHAR_SPACE);
6264
}
6365

64-
$titleLine = "$titleIndent<bold>$title</bold>\n";
65-
$border = $indentStr . Str::pad($char, $width, $char);
66+
$titleText = ColorTag::wrap($title, $opts['titleStyle']);
67+
$titleLine = "$titleIndent{$titleText}\n";
68+
69+
$border = $indentStr . Str::pad($char, $width, $char);
6670

6771
Console::write($titleLine . $border);
6872
}

src/IO/AbstractInput.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use Inhere\Console\Concern\InputArgumentsTrait;
1212
use Inhere\Console\Concern\InputOptionsTrait;
13+
use Inhere\Console\Contract\InputInterface;
1314
use function getcwd;
1415
use function is_int;
1516
use function trim;
@@ -19,7 +20,7 @@
1920
*
2021
* @package Inhere\Console\IO
2122
*/
22-
abstract class AbstractInput implements \Inhere\Console\Contract\InputInterface
23+
abstract class AbstractInput implements InputInterface
2324
{
2425
use InputArgumentsTrait, InputOptionsTrait;
2526

0 commit comments

Comments
 (0)