Skip to content

Commit

Permalink
feat: add an new simple interactive shell componnent
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 11, 2021
1 parent cb6f8c7 commit ce1e7f5
Show file tree
Hide file tree
Showing 12 changed files with 453 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/AbstractApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,8 @@ protected function startInteractiveShell(): void
$this->debugf('php is not enable "pcntl" extension, cannot listen CTRL+C signal');
}

// register signal.
if ($hasPcntl) {
// register signal.
ProcessUtil::installSignal(Signal::INT, static function () use ($out) {
$out->colored("\nQuit by CTRL+C");
exit(0);
Expand Down Expand Up @@ -460,8 +460,8 @@ protected function startInteractiveShell(): void
}
}

// listen signal.
if ($hasPcntl) {
// listen signal.
ProcessUtil::dispatchSignal();
}

Expand Down
43 changes: 43 additions & 0 deletions src/Component/Interact/AbstractQuestion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php declare(strict_types=1);

namespace Inhere\Console\Component\Interact;

use Inhere\Console\Component\InteractiveHandle;
use Toolkit\Stdlib\Str\StrObject;

/**
* Class AbstractQuestion
*
* @package Inhere\Console\Component\Interact
*/
abstract class AbstractQuestion extends InteractiveHandle
{
/**
* @var StrObject
*/
protected $answer;

/**
* @param string $str
*/
protected function createAnswer(string $str): void
{
$this->answer = StrObject::new($str)->trim();
}

/**
* @return StrObject
*/
public function getAnswer(): StrObject
{
return $this->answer;
}

/**
* @return int
*/
public function getInt(): int
{
return $this->answer->toInt();
}
}
6 changes: 3 additions & 3 deletions src/Component/Interact/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Inhere\Console\Component\Interact;

use Inhere\Console\Component\InteractMessage;
use Inhere\Console\Component\InteractiveHandle;
use Inhere\Console\Console;
use Inhere\Console\Util\Show;
use function array_filter;
Expand All @@ -17,7 +17,7 @@
*
* @package Inhere\Console\Component\Interact
*/
class Checkbox extends InteractMessage
class Checkbox extends InteractiveHandle
{
/**
* List multiple options and allow multiple selections
Expand All @@ -29,7 +29,7 @@ class Checkbox extends InteractMessage
*
* @return array
*/
public static function select(string $description, $options, $default = null, $allowExit = true): array
public static function select(string $description, $options, $default = null, bool $allowExit = true): array
{
if (!$description = trim($description)) {
Show::error('Please provide a description text!', 1);
Expand Down
8 changes: 4 additions & 4 deletions src/Component/Interact/Choose.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Inhere\Console\Component\Interact;

use Inhere\Console\Component\InteractMessage;
use Inhere\Console\Component\InteractiveHandle;
use Inhere\Console\Console;
use Inhere\Console\Util\Show;
use function array_key_exists;
Expand All @@ -15,7 +15,7 @@
*
* @package Inhere\Console\Component\Interact
*/
class Choose extends InteractMessage
class Choose extends InteractiveHandle
{
/**
* Choose one of several options
Expand Down Expand Up @@ -55,11 +55,11 @@ public static function one(string $description, $options, $default = null, bool
$text .= "\n <info>$key</info>) $value";
}

$defaultText = $default ? "[default:<comment>{$default}</comment>]" : '';
$defaultText = $default ? "[default:<comment>$default</comment>]" : '';
Console::write($text);

beginChoice:
$r = Console::readln("Your choice{$defaultText} : ");
$r = Console::readln("Your choice$defaultText : ");

// error, allow try again once.
if (!array_key_exists($r, $options)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Component/Interact/Confirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Inhere\Console\Component\Interact;

use Inhere\Console\Component\InteractMessage;
use Inhere\Console\Component\InteractiveHandle;
use Inhere\Console\Console;
use Inhere\Console\Util\Show;
use function stripos;
Expand All @@ -14,7 +14,7 @@
*
* @package Inhere\Console\Component\Interact
*/
class Confirm extends InteractMessage
class Confirm extends InteractiveHandle
{
/**
* Send a message request confirmation
Expand Down
Loading

0 comments on commit ce1e7f5

Please sign in to comment.