Beispiel #1
0
 /**
  * Overwrites a previous message to the output.
  *
  * @param string $message The message
  */
 private function overwrite($message)
 {
     $lines = explode("\n", $message);
     // append whitespace to match the line's length
     if (null !== $this->lastMessagesLength) {
         foreach ($lines as $i => $line) {
             if ($this->lastMessagesLength > Helper::strlenWithoutDecoration($this->output->getFormatter(), $line)) {
                 $lines[$i] = str_pad($line, $this->lastMessagesLength, " ", STR_PAD_RIGHT);
             }
         }
     }
     // move back to the beginning of the progress bar before redrawing it
     $this->output->write("\r");
     if ($this->formatLineCount) {
         $this->output->write(sprintf("[%dA", $this->formatLineCount));
     }
     $this->output->write(implode("\n", $lines));
     $this->lastMessagesLength = 0;
     foreach ($lines as $line) {
         $len = Helper::strlenWithoutDecoration($this->output->getFormatter(), $line);
         if ($len > $this->lastMessagesLength) {
             $this->lastMessagesLength = $len;
         }
     }
 }
Beispiel #2
0
 /**
  * Renders a caught exception.
  *
  * @param \Exception      $e      An exception instance
  * @param OutputInterface $output An OutputInterface instance
  */
 public function renderException($e, $output)
 {
     do {
         $title = sprintf('  [%s]  ', get_class($e));
         $len = $this->stringWidth($title);
         $width = $this->getTerminalWidth() ? $this->getTerminalWidth() - 1 : PHP_INT_MAX;
         // HHVM only accepts 32 bits integer in str_split, even when PHP_INT_MAX is a 64 bit integer: https://github.com/facebook/hhvm/issues/1327
         if (defined('HHVM_VERSION') && $width > 1 << 31) {
             $width = 1 << 31;
         }
         $formatter = $output->getFormatter();
         $lines = array();
         foreach (preg_split('/\\r?\\n/', $e->getMessage()) as $line) {
             foreach ($this->splitStringByWidth($line, $width - 4) as $line) {
                 // pre-format lines to get the right string length
                 $lineLength = $this->stringWidth(preg_replace('/\\[[^m]*m/', '', $formatter->format($line))) + 4;
                 $lines[] = array($line, $lineLength);
                 $len = max($lineLength, $len);
             }
         }
         $messages = array('', '');
         $messages[] = $emptyLine = $formatter->format(sprintf('<error>%s</error>', str_repeat(' ', $len)));
         $messages[] = $formatter->format(sprintf('<error>%s%s</error>', $title, str_repeat(' ', max(0, $len - $this->stringWidth($title)))));
         foreach ($lines as $line) {
             $messages[] = $formatter->format(sprintf('<error>  %s  %s</error>', $line[0], str_repeat(' ', $len - $line[1])));
         }
         $messages[] = $emptyLine;
         $messages[] = '';
         $messages[] = '';
         $output->writeln($messages, OutputInterface::OUTPUT_RAW);
         if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
             $output->writeln('<comment>Exception trace:</comment>');
             // exception related properties
             $trace = $e->getTrace();
             array_unshift($trace, array('function' => '', 'file' => $e->getFile() != null ? $e->getFile() : 'n/a', 'line' => $e->getLine() != null ? $e->getLine() : 'n/a', 'args' => array()));
             for ($i = 0, $count = count($trace); $i < $count; $i++) {
                 $class = isset($trace[$i]['class']) ? $trace[$i]['class'] : '';
                 $type = isset($trace[$i]['type']) ? $trace[$i]['type'] : '';
                 $function = $trace[$i]['function'];
                 $file = isset($trace[$i]['file']) ? $trace[$i]['file'] : 'n/a';
                 $line = isset($trace[$i]['line']) ? $trace[$i]['line'] : 'n/a';
                 $output->writeln(sprintf(' %s%s%s() at <info>%s:%s</info>', $class, $type, $function, $file, $line));
             }
             $output->writeln("");
             $output->writeln("");
         }
     } while ($e = $e->getPrevious());
     if (null !== $this->runningCommand) {
         $output->writeln(sprintf('<info>%s</info>', sprintf($this->runningCommand->getSynopsis(), $this->getName())));
         $output->writeln("");
         $output->writeln("");
     }
 }
Beispiel #3
0
 /**
  * Autocompletes a question.
  *
  * @param OutputInterface $output
  * @param Question $question
  *
  * @return string
  */
 private function autocomplete(OutputInterface $output, Question $question, $inputStream)
 {
     $autocomplete = $question->getAutocompleterValues();
     $ret = '';
     $i = 0;
     $ofs = -1;
     $matches = $autocomplete;
     $numMatches = count($matches);
     $sttyMode = shell_exec('stty -g');
     // Disable icanon (so we can fread each keypress) and echo (we'll do echoing here instead)
     shell_exec('stty -icanon -echo');
     // Add highlighted text style
     $output->getFormatter()->setStyle('hl', new OutputFormatterStyle('black', 'white'));
     // Read a keypress
     while (!feof($inputStream)) {
         $c = fread($inputStream, 1);
         // Backspace Character
         if ("" === $c) {
             if (0 === $numMatches && 0 !== $i) {
                 $i--;
                 // Move cursor backwards
                 $output->write("");
             }
             if ($i === 0) {
                 $ofs = -1;
                 $matches = $autocomplete;
                 $numMatches = count($matches);
             } else {
                 $numMatches = 0;
             }
             // Pop the last character off the end of our string
             $ret = substr($ret, 0, $i);
         } elseif ("" === $c) {
             // Did we read an escape sequence?
             $c .= fread($inputStream, 2);
             // A = Up Arrow. B = Down Arrow
             if (isset($c[2]) && ('A' === $c[2] || 'B' === $c[2])) {
                 if ('A' === $c[2] && -1 === $ofs) {
                     $ofs = 0;
                 }
                 if (0 === $numMatches) {
                     continue;
                 }
                 $ofs += 'A' === $c[2] ? -1 : 1;
                 $ofs = ($numMatches + $ofs) % $numMatches;
             }
         } elseif (ord($c) < 32) {
             if ("\t" === $c || "\n" === $c) {
                 if ($numMatches > 0 && -1 !== $ofs) {
                     $ret = $matches[$ofs];
                     // Echo out remaining chars for current match
                     $output->write(substr($ret, $i));
                     $i = strlen($ret);
                 }
                 if ("\n" === $c) {
                     $output->write($c);
                     break;
                 }
                 $numMatches = 0;
             }
             continue;
         } else {
             $output->write($c);
             $ret .= $c;
             $i++;
             $numMatches = 0;
             $ofs = 0;
             foreach ($autocomplete as $value) {
                 // If typed characters match the beginning chunk of value (e.g. [AcmeDe]moBundle)
                 if (0 === strpos($value, $ret) && $i !== strlen($value)) {
                     $matches[$numMatches++] = $value;
                 }
             }
         }
         // Erase characters from cursor to end of line
         $output->write("");
         if ($numMatches > 0 && -1 !== $ofs) {
             // Save cursor position
             $output->write("7");
             // Write highlighted text
             $output->write('<hl>' . substr($matches[$ofs], $i) . '</hl>');
             // Restore cursor position
             $output->write("8");
         }
     }
     // Reset stty so it behaves normally again
     shell_exec(sprintf('stty %s', $sttyMode));
     return $ret;
 }
Beispiel #4
0
 /**
  * Gets cell width.
  *
  * @param array   $row
  * @param int     $column
  *
  * @return int
  */
 private function getCellWidth(array $row, $column)
 {
     return isset($row[$column]) ? Helper::strlenWithoutDecoration($this->output->getFormatter(), $row[$column]) : 0;
 }