Skip to content

Commit 6d266d5

Browse files
committed
fix yes/no question pb
1 parent b2d123d commit 6d266d5

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

src/Ubiquity/devtools/cmd/Console.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,17 @@ public static function readline() {
2828
* @return string
2929
*/
3030
public static function question($prompt, array $propositions = null,array $options=[]) {
31+
$hiddenProposals=$options['hiddenProposals']??[];
32+
$continue=function($rep,$array){
33+
return \array_search($rep, $array)===false;
34+
};
3135
if(isset($options['default'])){
32-
$prompt.=" (default:".$options['default'].")";
36+
$prompt.=" (default:<b>".$options['default']."</b>)";
37+
}
38+
if(isset($options['ignoreCase'])){
39+
$continue=function($rep,$array){
40+
return \array_search(\strtolower($rep), \array_map('strtolower', $array))===false;
41+
};
3342
}
3443
echo ConsoleFormatter::colorize($prompt, ConsoleFormatter::BLACK, ConsoleFormatter::BG_YELLOW);
3544
if (\is_array($propositions)) {
@@ -45,9 +54,10 @@ public static function question($prompt, array $propositions = null,array $optio
4554
$answer = $propositions[(int) $answer - 1];
4655
} else {
4756
echo " (" . implode("/", $propositions) . ")\n";
57+
$propositions=array_merge($propositions,$hiddenProposals);
4858
do {
4959
$answer = self::readline();
50-
} while (\array_search($answer, $propositions) === false);
60+
} while ($continue($answer,$propositions));
5161
}
5262
} else {
5363
$answer = self::readline();
@@ -57,6 +67,10 @@ public static function question($prompt, array $propositions = null,array $optio
5767
}
5868
return $answer;
5969
}
70+
71+
public static function yesNoQuestion($prompt, array $propositions = null,array $options=[]){
72+
return self::question($prompt,$propositions,['ignoreCase'=>true,'hiddenProposals'=>['y','n']]);
73+
}
6074

6175
/**
6276
* Returns true if the answer is yes or y.
@@ -65,9 +79,9 @@ public static function question($prompt, array $propositions = null,array $optio
6579
* @return boolean
6680
*/
6781
public static function isYes($answer) {
68-
return \array_search($answer, [
69-
"yes",
70-
"y"
82+
return \array_search(\trim($answer), [
83+
'yes',
84+
'y'
7185
]) !== false;
7286
}
7387

@@ -78,9 +92,9 @@ public static function isYes($answer) {
7892
* @return boolean
7993
*/
8094
public static function isNo($answer) {
81-
return \array_search($answer, [
82-
"no",
83-
"n"
95+
return \array_search(\trim($answer), [
96+
'no',
97+
'n'
8498
]) !== false;
8599
}
86100

@@ -91,9 +105,9 @@ public static function isNo($answer) {
91105
* @return boolean
92106
*/
93107
public static function isCancel($answer) {
94-
return \array_search($answer, [
95-
"cancel",
96-
"z"
108+
return \array_search(\trim($answer), [
109+
'cancel',
110+
'z'
97111
]) !== false;
98112
}
99113
}

0 commit comments

Comments
 (0)