public static function strlenWithoutDecoration(Formatter $formatter, $string) { $isDecorated = $formatter->isDecorated(); $formatter->setDecorated(false); // remove <...> formatting $string = $formatter->format($string); // remove already formatted characters $string = preg_replace("/\\[[^m]*m/", '', $string); $formatter->setDecorated($isDecorated); return self::strlen($string); }
public function renderException(\Exception $e) { $stderr = $this->openErrorStream(); $decorated = $this->hasColorSupport($stderr); $this->formatter->setDecorated($decorated); do { $title = sprintf(' [%s] ', get_class($e)); $len = $this->stringWidth($title); $width = $this->getTerminalWidth() ? $this->getTerminalWidth() - 1 : PHP_INT_MAX; if (defined('HHVM_VERSION') && $width > 1 << 31) { $width = 1 << 31; } $lines = []; foreach (preg_split('/\\r?\\n/', $e->getMessage()) as $line) { foreach ($this->splitStringByWidth($line, $width - 4) as $line) { $lineLength = $this->stringWidth(preg_replace('/\\[[^m]*m/', '', $line)) + 4; $lines[] = [$line, $lineLength]; $len = max($lineLength, $len); } } $messages = ['', '']; $messages[] = $emptyLine = sprintf('<error>%s</error>', str_repeat(' ', $len)); $messages[] = sprintf('<error>%s%s</error>', $title, str_repeat(' ', max(0, $len - $this->stringWidth($title)))); foreach ($lines as $line) { $messages[] = sprintf('<error> %s %s</error>', $line[0], str_repeat(' ', $len - $line[1])); } $messages[] = $emptyLine; $messages[] = ''; $messages[] = ''; $this->write($messages, true, Output::OUTPUT_NORMAL, $stderr); if (Output::VERBOSITY_VERBOSE <= $this->output->getVerbosity()) { $this->write('<comment>Exception trace:</comment>', true, Output::OUTPUT_NORMAL, $stderr); // exception related properties $trace = $e->getTrace(); array_unshift($trace, ['function' => '', 'file' => $e->getFile() !== null ? $e->getFile() : 'n/a', 'line' => $e->getLine() !== null ? $e->getLine() : 'n/a', 'args' => []]); 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'; $this->write(sprintf(' %s%s%s() at <info>%s:%s</info>', $class, $type, $function, $file, $line), true, Output::OUTPUT_NORMAL, $stderr); } $this->write('', true, Output::OUTPUT_NORMAL, $stderr); $this->write('', true, Output::OUTPUT_NORMAL, $stderr); } } while ($e = $e->getPrevious()); }
/** * 设置消息作为文本块的格式 * @param string|array $messages 消息 * @param string $style 样式 * @param bool $large 是否返回一个大段文本 * @return string The formatter message */ public function formatBlock($messages, $style, $large = false) { if (!is_array($messages)) { $messages = [$messages]; } $len = 0; $lines = []; foreach ($messages as $message) { $message = OutputFormatter::escape($message); $lines[] = sprintf($large ? ' %s ' : ' %s ', $message); $len = max($this->strlen($message) + ($large ? 4 : 2), $len); } $messages = $large ? [str_repeat(' ', $len)] : []; for ($i = 0; isset($lines[$i]); ++$i) { $messages[] = $lines[$i] . str_repeat(' ', $len - $this->strlen($lines[$i])); } if ($large) { $messages[] = str_repeat(' ', $len); } for ($i = 0; isset($messages[$i]); ++$i) { $messages[$i] = sprintf('<%s>%s</%s>', $style, $messages[$i], $style); } return implode("\n", $messages); }