Skip to content

Commit

Permalink
Cleanup of Console\Prompt\Select, allow Prompt\Char without text.
Browse files Browse the repository at this point in the history
- an extraneous newline was inserted by Select superclass Char, when there was no text.
- a non-configurable prompt text has been removed from Select.
- with $echo === true, Select will now display the value of option picked.
  • Loading branch information
Thinkscape committed Aug 8, 2012
1 parent d7bd31d commit 9d64a16
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
4 changes: 3 additions & 1 deletion library/Zend/Console/Prompt/Char.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ public function show()
if ($this->echo) {
echo trim($char)."\n";
} else {
echo "\n";
if ($this->promptText) {
echo "\n"; // skip to next line but only if we had any prompt text
}
}
break;
}
Expand Down
35 changes: 25 additions & 10 deletions library/Zend/Console/Prompt/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,28 +77,43 @@ public function __construct(
*/
public function show()
{
/**
* Show prompt text and available options
*/
// Show prompt text and available options
$console = $this->getConsole();
$console->writeLine($this->promptText);
foreach ($this->options as $k => $v) {
$console->writeLine(' '.$k.') '.$v);
$console->writeLine(' ' . $k . ') ' . $v);
}

/**
* Ask for selection
*/
$mask = implode("",array_keys($this->options));
// Prepare mask
$mask = implode("", array_keys($this->options));
if ($this->allowEmpty) {
$mask .= "\r\n";
}

// Prepare other params for parent class
$this->setAllowedChars($mask);
$oldPrompt = $this->promptText;
$this->promptText = 'Pick one option: ';
$oldPrompt = $this->promptText;
$oldEcho = $this->echo;
$this->echo = false;
$this->promptText = null;

// Retrieve a single character
$response = parent::show();

// Restore old params
$this->promptText = $oldPrompt;
$this->echo = $oldEcho;

// Display selected option if echo is enabled
if ($this->echo) {
if (isset($this->options[$response])) {
$console->writeLine($this->options[$response]);
} else {
$console->writeLine();
}
}

$this->lastResponse = $response;
return $response;
}

Expand Down

0 comments on commit 9d64a16

Please sign in to comment.