forked from inhere/php-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInteract.php
More file actions
295 lines (246 loc) · 8.21 KB
/
Copy pathInteract.php
File metadata and controls
295 lines (246 loc) · 8.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
/**
* Created by PhpStorm.
* User: Inhere
* Date: 15-4-1
* Time: 上午10:08
* Used: CliInteract 命令行交互
* file: CliInteract.php
*/
namespace inhere\console\utils;
/**
* Class Interact
* @package inhere\console\utils
*/
class Interact extends Show
{
/////////////////////////////////////////////////////////////////
/// Interactive method (select/confirm/question/loopAsk)
/////////////////////////////////////////////////////////////////
/**
* Select one of the options 在多个选项中选择一个
* @param string $description 说明
* @param mixed $options 选项数据
* e.g
* [
* // option => value
* '1' => 'chengdu',
* '2' => 'beijing'
* ]
* @param mixed $default 默认选项
* @param bool $allowExit 有退出选项 默认 true
* @return string
*/
public static function select($description, $options, $default = null, $allowExit=true)
{
return self::choice($description, $options, $default, $allowExit);
}
public static function choice($description, $options, $default = null, $allowExit=true)
{
if ( !$description = trim($description) ) {
self::error('Please provide a description text!', 1);
}
$options = is_array($options) ? $options : explode(',', $options);
// If default option is error
if ( null !== $default && !isset($options[$default]) ) {
self::error("The default option [{$default}] don't exists.", true);
}
if ($allowExit) {
$options['q'] = 'quit';
}
beginChoice:
$text = " <comment>$description</comment>";
foreach ($options as $key => $value) {
$text .= "\n <info>$key</info>) $value";
}
$defaultText = $default ? "[default:<comment>{$default}</comment>]" : '';
$r = self::read($text . "\n You choice{$defaultText} : ");
// error, allow try again once.
if ( !array_key_exists($r, $options) ) {
goto beginChoice;
}
// exit
if ( $r === 'q' ) {
self::write("\n Quit,ByeBye.", true, true);
}
return $r;
}
/**
* 确认, 发出信息要求确认
* @param string $question 发出的信息
* @param bool $default Default value
* @return bool
*/
public static function confirm($question, $default = true)
{
if ( !$question = trim($question) ) {
self::error('Please provide a question text!', 1);
}
$question = ucfirst(trim($question, '?'));
$default = (bool)$default;
$defaultText = $default ? 'yes' : 'no';
$message = " <comment>$question ?</comment>\n Please confirm (yes|no) [default:<info>$defaultText</info>]: ";
while (true) {
$answer = self::read($message);
if ( empty($answer) ) {
return $default;
}
if ( 0 === stripos($answer, 'y') ) {
return true;
}
if ( 0 === stripos($answer, 'n') ) {
return false;
}
}
return false;
}
/**
* 询问,提出问题;返回 输入的结果
* @param string $question 问题
* @param null|string $default 默认值
* @param \Closure $validator The validate callback. It must return bool.
* @example This is an example
*
* ```
* $answer = Interact::ask('Please input your name?', null, function ($answer) {
* if ( !preg_match('/\w+/', $answer) ) {
* Interact::error('The name must match "/\w+/"');
*
* return false;
* }
*
* return true;
* });
* ```
*
* @return string
*/
public static function ask($question, $default = null, \Closure $validator = null)
{
return self::question($question, $default, $validator);
}
public static function question($question, $default = null, \Closure $validator = null)
{
if ( !$question = trim($question) ) {
self::error('Please provide a question text!', 1);
}
$defaultText = null !== $default ? "(default: <info>$default</info>)" : '';
$answer = self::read( '<comment>' . ucfirst($question) . "</comment>$defaultText " );
if ( '' === $answer ) {
if ( null === $default) {
self::error('A value is required.', false);
return static::question($question, $default, $validator);
}
return $default;
}
if ( $validator ) {
return $validator($answer) ? $answer : static::question($question, $default, $validator);
}
return $answer;
}
/**
* 有次数限制的询问,提出问题
* 若输入了值且验证成功则返回 输入的结果
* 否则,会连续询问 $allowed 次, 若仍然错误,退出
* @param string $question 问题
* @param null|string $default 默认值
* @param \Closure $validator (默认验证输入是否为空)自定义回调验证输入是否符合要求; 验证成功返回true 否则 可返回错误消息
* @example This is an example
*
* ```
* // no default value
* Interact::loopAsk('please entry you age?', null, function($age)
* {
* if ($age<1 || $age>100) {
* Interact::error('Allow the input range is 1-100');
* return false;
* }
*
* return true;
* } );
*
* // has default value
* Interact::loopAsk('please entry you age?', 89, function($age)
* {
* if ($age<1 || $age>100) {
* Interact::error('Allow the input range is 1-100');
* return false;
* }
*
* return true;
* } );
* ```
*
* @param int $times Allow input times
* @return string
*/
public static function loopAsk($question, $default = null, \Closure $validator = null, $times=3)
{
if ( !$question = trim($question) ) {
self::error('Please provide a question text!', 1);
}
$result = false;
$answer = '';
$question = ucfirst($question);
$back = $times = ((int)$times > 6 || $times < 1) ? 3 : (int)$times;
$defaultText = null !== $default ? "(default: <info>$default</info>)" : '';
while ($times--) {
if ( $defaultText ) {
$answer = self::read("<comment>{$question}</comment>{$defaultText} ");
if ( '' === $answer ) {
$answer = $default;
$result = true;
break;
}
} else {
$num = $times + 1;
$answer = self::read("<comment>{$question}</comment>\n(You have a [<bold>$num</bold>] chance to enter!) ");
}
// If setting verify callback
if ($validator && ($result = $validator($answer)) === true ) {
break;
}
// no setting verify callback
if ( !$validator && $answer !== '') {
$result = true;
break;
}
}
if ( !$result ) {
if ( null !== $default ) {
return $default;
}
self::write("\n You've entered incorrectly <danger>$back</danger> times in a row. exit!\n", true, 1);
}
return $answer;
}
public static function progressBarSetting()
{
}
public static function progressBarStart()
{
}
public static function progressBarUp()
{
}
public static function progressBarEnd()
{
}
/**
* 读取输入信息
* @param string $message 若不为空,则先输出文本
* @param bool $nl true 会添加换行符 false 原样输出,不添加换行符
* @return string
*/
public static function readRow($message = null, $nl = false)
{
return self::read($message, $nl);
}
public static function read($message = null, $nl = false)
{
if ( $message ) {
self::write($message, $nl);
}
return trim(fgets(STDIN));
}
} // end class