Skip to content

Commit 76ce20e

Browse files
committed
add new function: multi select
1 parent 9a8455b commit 76ce20e

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

examples/Controllers/HomeController.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ public function defArgCommand()
379379
*/
380380
public function confirmCommand()
381381
{
382+
// can also: $this->confirm();
382383
$a = Interact::confirm('continue');
383384

384385
$this->write('Your answer is: ' . ($a ? 'yes' : 'no'));
@@ -390,11 +391,25 @@ public function confirmCommand()
390391
public function selectCommand()
391392
{
392393
$opts = ['john', 'simon', 'rose'];
394+
// can also: $this->select();
393395
$a = Interact::select('you name is', $opts);
394396

395397
$this->write('Your answer is: ' . $opts[$a]);
396398
}
397399

400+
/**
401+
* This is a demo for use <magenta>Interact::multiSelect()</magenta> method
402+
*/
403+
public function msCommand()
404+
{
405+
$opts = ['john', 'simon', 'rose', 'tom'];
406+
407+
// can also: $a = Interact::multiSelect('Your friends are', $opts);
408+
$a = $this->multiSelect('Your friends are', $opts);
409+
410+
$this->write('Your answer is: ' . json_encode($a));
411+
}
412+
398413
/**
399414
* This is a demo for use <magenta>Interact::ask()</magenta> method
400415
*/

src/Traits/UserInteractAwareTrait.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
* @method string readRow($message = null, $nl = false)
1919
* @method string read($message = null, $nl = false, array $opts = [])
2020
*
21+
* @method array checkbox(string $description, $options, $default = null, $allowExit = true)
22+
* @method array multiSelect(string $description, $options, $default = null, $allowExit = true)
23+
*
2124
* @method string askHiddenInput(string $prompt = 'Enter Password:')
2225
* @method string promptSilent(string $prompt = 'Enter Password:')
2326
* @method string askPassword(string $prompt = 'Enter Password:')

src/Utils/Interact.php

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,55 @@ public static function checkbox(string $description, $options, $default = null,
142142
*/
143143
public static function multiSelect(string $description, $options, $default = null, $allowExit = true)
144144
{
145-
return [];
145+
if (!$description = trim($description)) {
146+
self::error('Please provide a description text!', 1);
147+
}
148+
149+
$sep = ','; // ',' ' '
150+
$options = \is_array($options) ? $options : explode(',', $options);
151+
152+
// If default option is error
153+
if (null !== $default && !isset($options[$default])) {
154+
self::error("The default option [{$default}] don't exists.", true);
155+
}
156+
157+
if ($allowExit) {
158+
$options['q'] = 'quit';
159+
}
160+
161+
$text = "<comment>$description</comment>";
162+
foreach ($options as $key => $value) {
163+
$text .= "\n <info>$key</info>) $value";
164+
}
165+
166+
self::write($text);
167+
$defaultText = $default ? "[default:<comment>{$default}</comment>]" : '';
168+
$filter = function ($val) use($options){
169+
return $val !== 'q' && isset($options[$val]);
170+
};
171+
172+
beginChoice:
173+
$r = self::read("Your choice{$defaultText} : ");
174+
$r = $r !== '' ? str_replace(' ', '', trim($r, $sep)) : '';
175+
176+
// empty
177+
if ($r === '') {
178+
goto beginChoice;
179+
}
180+
181+
// exit
182+
if ($r === 'q') {
183+
self::write("\n Quit,ByeBye.", true, true);
184+
}
185+
186+
$rs = strpos($r, $sep) ? array_filter(explode($sep, $r), $filter) : [$r];
187+
188+
// error, try again
189+
if (!$rs) {
190+
goto beginChoice;
191+
}
192+
193+
return $rs;
146194
}
147195

148196
/**
@@ -333,7 +381,7 @@ public static function limitedAsk($question, $default = null, \Closure $validato
333381
}
334382
} else {
335383
$num = $times + 1;
336-
$answer = self::read(sprintf('(You have a [<bold>%s</bold>] chance to enter!) ', $num));
384+
$answer = self::read(sprintf('(You have [<bold>%s</bold>] chances to enter!) ', $num));
337385
}
338386

339387
// If setting verify callback

0 commit comments

Comments
 (0)