Skip to content

Commit 7f9f7bb

Browse files
committed
feat: add support for single question prompts
1 parent 85caf22 commit 7f9f7bb

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/Sprout/Prompt.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class Prompt
1919

2020
protected $isWindows = false;
2121

22+
protected $singlePrompt = false;
23+
2224
public function __construct(array $prompt)
2325
{
2426
$this->questions = $prompt;
@@ -30,8 +32,17 @@ public function onError()
3032
//
3133
}
3234

33-
public function ask(): array
35+
/**
36+
* Display the prompt and get answers
37+
* @return mixed The answers
38+
*/
39+
public function ask()
3440
{
41+
if (isset($this->questions['type'])) {
42+
$this->questions = [$this->questions];
43+
$this->singlePrompt = true;
44+
}
45+
3546
foreach ($this->questions as $key => $prompt) {
3647
if (is_callable($prompt['type'])) {
3748
$prompt['type'] = $prompt['type']($this->answers);
@@ -71,6 +82,10 @@ public function ask(): array
7182
$this->answers[$prompt['name']] = $answer;
7283
}
7384

85+
if ($this->singlePrompt) {
86+
return $this->answers[0];
87+
}
88+
7489
return $this->answers;
7590
}
7691

@@ -104,7 +119,7 @@ protected function getAnswer($prompt)
104119

105120
while (1) {
106121
$keyPress = $this->isWindows ? $this->readWindowsInput() : fgets($stdin);
107-
122+
108123
if ($keyPress || is_numeric($keyPress)) {
109124
if ($keyPress === "\n" || $keyPress === "\r" || ($prompt['type'] === 'select' && is_numeric($keyPress))) {
110125
if ($prompt['type'] === 'select') {

0 commit comments

Comments
 (0)