1
0
Fork 0

overwriteError + ask[X]() to stderr

pull/3715/head
Rob Bast 2015-02-18 10:03:45 +01:00
parent ec434e4576
commit 3c7a617753
3 changed files with 91 additions and 17 deletions

View File

@ -108,6 +108,11 @@ class ConsoleIO extends BaseIO
$this->doWrite($messages, $newline, true); $this->doWrite($messages, $newline, true);
} }
/**
* @param array $messages
* @param boolean $newline
* @param boolean $stderr
*/
private function doWrite($messages, $newline, $stderr) private function doWrite($messages, $newline, $stderr)
{ {
if (null !== $this->startTime) { if (null !== $this->startTime) {
@ -133,12 +138,38 @@ class ConsoleIO extends BaseIO
*/ */
public function overwrite($messages, $newline = true, $size = null) public function overwrite($messages, $newline = true, $size = null)
{ {
if (!$this->output->isDecorated()) { $this->doOverwrite($messages, $newline, $size, false);
}
/**
* {@inheritDoc}
*/
public function overwriteError($messages, $newline = true, $size = null)
{
$this->doOverwrite($messages, $newline, $size, true);
}
/**
* @param array $messages
* @param boolean $newline
* @param integer $size
* @param boolean $stderr
*/
private function doOverwrite($messages, $newline, $size, $stderr)
{
if (true === $stderr && $this->output instanceof ConsoleOutputInterface) {
$output = $this->output->getErrorOutput();
} else {
$output = $this->output;
}
if (!$output->isDecorated()) {
if (!$messages) { if (!$messages) {
return; return;
} }
return $this->write($messages, count($messages) === 1 || $newline); $this->doWrite($messages, count($messages) === 1 || $newline, $stderr);
return;
} }
// messages can be an array, let's convert it to string anyway // messages can be an array, let's convert it to string anyway
@ -147,24 +178,24 @@ class ConsoleIO extends BaseIO
// since overwrite is supposed to overwrite last message... // since overwrite is supposed to overwrite last message...
if (!isset($size)) { if (!isset($size)) {
// removing possible formatting of lastMessage with strip_tags // removing possible formatting of lastMessage with strip_tags
$size = strlen(strip_tags($this->lastMessage)); $size = strlen(strip_tags($stderr ? $this->lastMessageErr : $this->lastMessage));
} }
// ...let's fill its length with backspaces // ...let's fill its length with backspaces
$this->write(str_repeat("\x08", $size), false); $this->doWrite(str_repeat("\x08", $size), false, $stderr);
// write the new message // write the new message
$this->write($messages, false); $this->doWrite($messages, false, $stderr);
$fill = $size - strlen(strip_tags($messages)); $fill = $size - strlen(strip_tags($messages));
if ($fill > 0) { if ($fill > 0) {
// whitespace whatever has left // whitespace whatever has left
$this->write(str_repeat(' ', $fill), false); $this->doWrite(str_repeat(' ', $fill), false, $stderr);
// move the cursor back // move the cursor back
$this->write(str_repeat("\x08", $fill), false); $this->doWrite(str_repeat("\x08", $fill), false, $stderr);
} }
if ($newline) { if ($newline) {
$this->write(''); $this->doWrite('', true, $stderr);
} }
$this->lastMessage = $messages; $this->lastMessage = $messages;
} }
@ -174,7 +205,16 @@ class ConsoleIO extends BaseIO
*/ */
public function ask($question, $default = null) public function ask($question, $default = null)
{ {
return $this->helperSet->get('dialog')->ask($this->output, $question, $default); $output = $this->output;
if ($output instanceof ConsoleOutputInterface) {
$output = $output->getErrorOutput();
}
/** @var \Symfony\Component\Console\Helper\DialogHelper $dialog */
$dialog = $this->helperSet->get('dialog');
return $dialog->ask($output, $question, $default);
} }
/** /**
@ -182,7 +222,16 @@ class ConsoleIO extends BaseIO
*/ */
public function askConfirmation($question, $default = true) public function askConfirmation($question, $default = true)
{ {
return $this->helperSet->get('dialog')->askConfirmation($this->output, $question, $default); $output = $this->output;
if ($output instanceof ConsoleOutputInterface) {
$output = $output->getErrorOutput();
}
/** @var \Symfony\Component\Console\Helper\DialogHelper $dialog */
$dialog = $this->helperSet->get('dialog');
return $dialog->askConfirmation($output, $question, $default);
} }
/** /**
@ -190,7 +239,16 @@ class ConsoleIO extends BaseIO
*/ */
public function askAndValidate($question, $validator, $attempts = false, $default = null) public function askAndValidate($question, $validator, $attempts = false, $default = null)
{ {
return $this->helperSet->get('dialog')->askAndValidate($this->output, $question, $validator, $attempts, $default); $output = $this->output;
if ($output instanceof ConsoleOutputInterface) {
$output = $output->getErrorOutput();
}
/** @var \Symfony\Component\Console\Helper\DialogHelper $dialog */
$dialog = $this->helperSet->get('dialog');
return $dialog->askAndValidate($output, $question, $validator, $attempts, $default);
} }
/** /**
@ -204,9 +262,9 @@ class ConsoleIO extends BaseIO
// use bash if it's present // use bash if it's present
if ($finder->find('bash') && $finder->find('stty')) { if ($finder->find('bash') && $finder->find('stty')) {
$this->write($question, false); $this->writeError($question, false);
$value = rtrim(shell_exec('bash -c "stty -echo; read -n0 discard; read -r mypassword; stty echo; echo $mypassword"')); $value = rtrim(shell_exec('bash -c "stty -echo; read -n0 discard; read -r mypassword; stty echo; echo $mypassword"'));
$this->write(''); $this->writeError('');
return $value; return $value;
} }
@ -230,9 +288,9 @@ class ConsoleIO extends BaseIO
$exe = $tmpExe; $exe = $tmpExe;
} }
$this->write($question, false); $this->writeError($question, false);
$value = rtrim(shell_exec($exe)); $value = rtrim(shell_exec($exe));
$this->write(''); $this->writeError('');
// clean up // clean up
if (isset($tmpExe)) { if (isset($tmpExe)) {
@ -252,11 +310,11 @@ class ConsoleIO extends BaseIO
} }
} }
if (isset($shell)) { if (isset($shell)) {
$this->write($question, false); $this->writeError($question, false);
$readCmd = ($shell === 'csh') ? 'set mypassword = $<' : 'read -r mypassword'; $readCmd = ($shell === 'csh') ? 'set mypassword = $<' : 'read -r mypassword';
$command = sprintf("/usr/bin/env %s -c 'stty -echo; %s; stty echo; echo \$mypassword'", $shell, $readCmd); $command = sprintf("/usr/bin/env %s -c 'stty -echo; %s; stty echo; echo \$mypassword'", $shell, $readCmd);
$value = rtrim(shell_exec($command)); $value = rtrim(shell_exec($command));
$this->write(''); $this->writeError('');
return $value; return $value;
} }

View File

@ -81,6 +81,15 @@ interface IOInterface
*/ */
public function overwrite($messages, $newline = true, $size = null); public function overwrite($messages, $newline = true, $size = null);
/**
* Overwrites a previous message to the error output.
*
* @param string|array $messages The message as an array of lines or a single string
* @param bool $newline Whether to add a newline or not
* @param integer $size The size of line
*/
public function overwriteError($messages, $newline = true, $size = null);
/** /**
* Asks a question to the user. * Asks a question to the user.
* *

View File

@ -80,6 +80,13 @@ class NullIO extends BaseIO
{ {
} }
/**
* {@inheritDoc}
*/
public function overwriteError($messages, $newline = true, $size = 80)
{
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */