Skip to content

Commit 86ed098

Browse files
committed
update: move some interactive methods to class from Interact.php
1 parent 1ebb0b7 commit 86ed098

File tree

14 files changed

+671
-350
lines changed

14 files changed

+671
-350
lines changed

examples/Controller/InteractController.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected static function commandAliases(): array
4646
public function confirmCommand(): void
4747
{
4848
// can also: $this->confirm();
49-
$a = Interact::confirm('continue');
49+
$a = Interact::confirm('ensure continue');
5050

5151
$this->write('Your answer is: ' . ($a ? 'yes' : 'no'));
5252
}
@@ -81,10 +81,9 @@ public function multiSelectCommand(): void
8181
*/
8282
public function askCommand(): void
8383
{
84-
$a = Interact::ask('you name is: ', null, function ($val, &$err) {
84+
$a = Interact::ask('you name is: ', '', function ($val, &$err) {
8585
if (!preg_match('/^\w{2,}$/', $val)) {
8686
$err = 'Your input must match /^\w{2,}$/';
87-
8887
return false;
8988
}
9089

@@ -105,9 +104,9 @@ public function limitedAskCommand(): void
105104
$times = (int)$this->input->getOpt('limit', 3);
106105

107106
if ($this->input->getBoolOpt('nv')) {
108-
$a = Interact::limitedAsk('you name is: ', null, null, $times);
107+
$a = Interact::limitedAsk('you name is: ', '', null, $times);
109108
} else {
110-
$a = Interact::limitedAsk('you name is: ', null, function ($val) {
109+
$a = Interact::limitedAsk('you name is: ', '', function ($val) {
111110
if (!\preg_match('/^\w{2,}$/', $val)) {
112111
Show::error('Your input must match /^\w{2,}$/');
113112
return false;

src/Component/AutoCompDumper.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Inhere\Console\Component;
4+
5+
/**
6+
* Class AutoCompDumper Auto complete dumper for zsh/sh shell
7+
* @package Inhere\Console\Component
8+
*/
9+
class AutoCompDumper
10+
{
11+
12+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace Inhere\Console\Component\Interact;
4+
5+
use Inhere\Console\Component\InteractMessage;
6+
use Inhere\Console\Console;
7+
use Inhere\Console\Util\Show;
8+
9+
/**
10+
* Class Checkbox
11+
* @package Inhere\Console\Component\Interact
12+
*/
13+
class Checkbox extends InteractMessage
14+
{
15+
16+
/**
17+
* List multiple options and allow multiple selections
18+
*
19+
* @param string $description
20+
* @param string|array $options
21+
* @param null|mixed $default
22+
* @param bool $allowExit
23+
* @return array
24+
*/
25+
public static function select(string $description, $options, $default = null, $allowExit = true): array
26+
{
27+
if (!$description = \trim($description)) {
28+
Show::error('Please provide a description text!', 1);
29+
}
30+
31+
$sep = ','; // ',' ' '
32+
$options = \is_array($options) ? $options : \explode(',', $options);
33+
34+
// If default option is error
35+
if (null !== $default && !isset($options[$default])) {
36+
Show::error("The default option [{$default}] don't exists.", true);
37+
}
38+
39+
if ($allowExit) {
40+
$options['q'] = 'quit';
41+
}
42+
43+
$text = "<comment>$description</comment>";
44+
foreach ($options as $key => $value) {
45+
$text .= "\n <info>$key</info>) $value";
46+
}
47+
48+
Console::write($text);
49+
$defText = $default ? "[default:<comment>{$default}</comment>]" : '';
50+
$filter = function ($val) use ($options) {
51+
return $val !== 'q' && isset($options[$val]);
52+
};
53+
54+
beginChoice:
55+
$r = Console::readln("Your choice{$defText} : ");
56+
$r = $r !== '' ? \str_replace(' ', '', \trim($r, $sep)) : '';
57+
58+
// empty
59+
if ($r === '') {
60+
goto beginChoice;
61+
}
62+
63+
// exit
64+
if ($r === 'q') {
65+
Console::write("\n Quit,ByeBye.", true, true);
66+
}
67+
68+
$rs = \strpos($r, $sep) ? \array_filter(\explode($sep, $r), $filter) : [$r];
69+
70+
// error, try again
71+
if (!$rs) {
72+
goto beginChoice;
73+
}
74+
75+
return $rs;
76+
}
77+
78+
}

src/Component/Interact/Choose.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Inhere\Console\Component\Interact;
4+
5+
use Inhere\Console\Component\InteractMessage;
6+
use Inhere\Console\Console;
7+
use Inhere\Console\Util\Show;
8+
9+
/**
10+
* Class Choose
11+
* @package Inhere\Console\Component\Interact
12+
*/
13+
class Choose extends InteractMessage
14+
{
15+
/**
16+
* Choose one of several options
17+
* @param string $description
18+
* @param string|array $options Option data
19+
* e.g
20+
* [
21+
* // option => value
22+
* '1' => 'chengdu',
23+
* '2' => 'beijing'
24+
* ]
25+
* @param string|int $default Default option
26+
* @param bool $allowExit
27+
* @return string
28+
*/
29+
public static function one(string $description, $options, $default = null, bool $allowExit = true): string
30+
{
31+
if (!$description = \trim($description)) {
32+
Show::error('Please provide a description text!', 1);
33+
}
34+
35+
$options = \is_array($options) ? $options : \explode(',', $options);
36+
37+
// If default option is error
38+
if (null !== $default && !isset($options[$default])) {
39+
Show::error("The default option [{$default}] don't exists.", true);
40+
}
41+
42+
if ($allowExit) {
43+
$options['q'] = 'quit';
44+
}
45+
46+
$text = "<comment>$description</comment>";
47+
foreach ($options as $key => $value) {
48+
$text .= "\n <info>$key</info>) $value";
49+
}
50+
51+
$defaultText = $default ? "[default:<comment>{$default}</comment>]" : '';
52+
Console::write($text);
53+
54+
beginChoice:
55+
$r = Console::readln("Your choice{$defaultText} : ");
56+
57+
// error, allow try again once.
58+
if (!\array_key_exists($r, $options)) {
59+
goto beginChoice;
60+
}
61+
62+
// exit
63+
if ($r === 'q') {
64+
Console::write("\n Quit,ByeBye.", true, true);
65+
}
66+
67+
return $r;
68+
}
69+
}

src/Component/Interact/Confirm.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Inhere\Console\Component\Interact;
4+
5+
use Inhere\Console\Component\InteractMessage;
6+
use Inhere\Console\Console;
7+
use Inhere\Console\Util\Show;
8+
9+
/**
10+
* Class Confirm
11+
* @package Inhere\Console\Component\Interact
12+
*/
13+
class Confirm extends InteractMessage
14+
{
15+
/**
16+
* Send a message request confirmation
17+
* @param string $question The question message
18+
* @param bool $default Default value
19+
* @return bool
20+
*/
21+
public static function ask(string $question, bool $default = true): bool
22+
{
23+
if (!$question = \trim($question)) {
24+
Show::warning('Please provide a question message!', 1);
25+
return false;
26+
}
27+
28+
$defText = $default ? 'yes' : 'no';
29+
$question = \ucfirst(\trim($question, '?'));
30+
$message = "<comment>$question ?</comment>\nPlease confirm (yes|no)[default:<info>$defText</info>]: ";
31+
32+
while (true) {
33+
$answer = Console::readChar($message);
34+
35+
if ('' === $answer) {
36+
return $default;
37+
}
38+
39+
if (0 === \stripos($answer, 'y')) {
40+
return true;
41+
}
42+
43+
if (0 === \stripos($answer, 'n')) {
44+
return false;
45+
}
46+
}
47+
48+
return false;
49+
}
50+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace Inhere\Console\Component\Interact;
4+
5+
use Inhere\Console\Component\InteractMessage;
6+
use Inhere\Console\Console;
7+
use Inhere\Console\Util\Show;
8+
9+
/**
10+
* Class LimitedAsk
11+
* @package Inhere\Console\Component\Interact
12+
*/
13+
class LimitedAsk extends InteractMessage
14+
{
15+
16+
/**
17+
* Ask a question, ask for a limited number of times
18+
* 若输入了值且验证成功则返回 输入的结果
19+
* 否则,会连续询问 $times 次, 若仍然错误,退出
20+
* @param string $question 问题
21+
* @param string $default 默认值
22+
* @param \Closure $validator (默认验证输入是否为空)自定义回调验证输入是否符合要求; 验证成功返回true 否则 可返回错误消息
23+
* @example This is an example
24+
*
25+
* ```php
26+
* // no default value
27+
* Interact::limitedAsk('please entry you age?', null, function($age)
28+
* {
29+
* if ($age<1 || $age>100) {
30+
* Interact::error('Allow the input range is 1-100');
31+
* return false;
32+
* }
33+
* return true;
34+
* } );
35+
*
36+
* // has default value
37+
* Interact::limitedAsk('please entry you age?', 89, function($age)
38+
* {
39+
* if ($age<1 || $age>100) {
40+
* Interact::error('Allow the input range is 1-100');
41+
* return false;
42+
* }
43+
* return true;
44+
* } );
45+
* ```
46+
*
47+
* @param int $times Allow input times
48+
* @return string
49+
*/
50+
public static function ask(
51+
string $question,
52+
string $default = '',
53+
\Closure $validator = null,
54+
int $times = 3
55+
): string {
56+
if (!$question = \trim($question)) {
57+
Show::error('Please provide a question text!', 1);
58+
}
59+
60+
$answer = '';
61+
$back = $times = ($times > 6 || $times < 1) ? 3 : $times;
62+
63+
$question = \ucfirst($question);
64+
$hasDefault = '' !== $default;
65+
66+
if ($hasDefault) {
67+
$message = "<comment>{$question}</comment>(default: <info>$default</info>) ";
68+
} else {
69+
$message = "<comment>{$question}</comment>";
70+
Console::write($message);
71+
}
72+
73+
while ($times--) {
74+
if ($hasDefault) {
75+
$answer = Console::readln($message);
76+
77+
if ('' === $answer) {
78+
$answer = $default;
79+
break;
80+
}
81+
} else {
82+
$num = $times + 1;
83+
$answer = Console::readln(\sprintf('(You have [<bold>%s</bold>] chances to enter!) ', $num));
84+
}
85+
86+
// If setting verify callback
87+
if ($validator && true === $validator($answer) ) {
88+
break;
89+
}
90+
91+
// no setting verify callback
92+
if (!$validator && $answer !== '') {
93+
break;
94+
}
95+
}
96+
97+
if ('' !== $answer) {
98+
return $answer;
99+
}
100+
101+
if ($hasDefault) {
102+
return $default;
103+
}
104+
105+
Show::write("\n You've entered incorrectly <danger>$back</danger> times in a row. exit!", true, 1);
106+
return '';
107+
}
108+
}

0 commit comments

Comments
 (0)