/** * Show a list of options and prompt the user to select one of them. * * @return string Selected option */ public function show() { // Show prompt text and available options $console = $this->getConsole(); $console->writeLine($this->promptText); foreach ($this->options as $k => $v) { $console->writeLine(' ' . $k . ') ' . $v); } // 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; $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; }
public function testCanPromptCharWithoutIgnoreCase() { fwrite($this->adapter->stream, 'FOObar'); $char = new Char(); $char->setEcho(false); $char->setConsole($this->adapter); $char->setIgnoreCase(false); ob_start(); $response = $char->show(); ob_get_clean(); $this->assertEquals('b', $response); }
/** * Show a list of options and prompt the user to select one of them. * * @return string Selected option */ public function show() { /** * Show prompt text and available options */ $console = $this->getConsole(); $console->writeLine($this->promptText); foreach ($this->options as $k => $v) { $console->writeLine(' ' . $k . ') ' . $v); } /** * Ask for selection */ $mask = implode("", array_keys($this->options)); if ($this->allowEmpty) { $mask .= "\r\n"; } $this->setAllowedChars($mask); $oldPrompt = $this->promptText; $this->promptText = 'Pick one option: '; $response = parent::show(); $this->promptText = $oldPrompt; return $response; }
/** * Show the confirmation message and return result. * * @return bool */ public function show() { $response = parent::show() === $this->yesChar; return $this->lastResponse = $response; }