12 namespace Symfony\Component\Console\Style;
51 $this->lineLength = min($this->
getTerminalWidth() - (
int) (DIRECTORY_SEPARATOR ===
'\\'), self::MAX_LINE_LENGTH);
53 parent::__construct($output);
65 public function block($messages, $type = null, $style = null, $prefix =
' ', $padding =
false)
68 $messages = is_array($messages) ? array_values($messages) : array($messages);
73 $messages[0] = sprintf(
'[%s] %s', $type, $messages[0]);
77 foreach ($messages as $key => $message) {
79 $lines = array_merge($lines, explode(PHP_EOL, wordwrap($message, $this->lineLength -
Helper::strlen($prefix), PHP_EOL,
true)));
81 if (count($messages) > 1 && $key < count($messages) - 1) {
87 array_unshift($lines,
'');
91 foreach ($lines as &$line) {
92 $line = sprintf(
'%s%s', $prefix, $line);
96 $line = sprintf(
'<%s>%s</>', $style, $line);
111 sprintf(
'<comment>%s</>', $message),
112 sprintf(
'<comment>%s</>', str_repeat(
'=', strlen($message))),
124 sprintf(
'<comment>%s</>', $message),
125 sprintf(
'<comment>%s</>', str_repeat(
'-', strlen($message))),
136 $elements = array_map(
function ($element) {
137 return sprintf(
' * %s', $element);
151 if (!is_array($message)) {
152 $this->
writeln(sprintf(
' // %s', $message));
157 foreach ($message as $element) {
158 $this->
text($element);
167 $this->
block($message,
'OK',
'fg=white;bg=green',
' ',
true);
175 $this->
block($message,
'ERROR',
'fg=white;bg=red',
' ',
true);
183 $this->
block($message,
'WARNING',
'fg=white;bg=red',
' ',
true);
191 $this->
block($message,
'NOTE',
'fg=yellow',
' ! ');
199 $this->
block($message,
'CAUTION',
'fg=white;bg=red',
' ! ',
true);
205 public function table(array $headers, array $rows)
207 $headers = array_map(
function ($value) {
return sprintf(
'<info>%s</>', $value); }, $headers);
209 $table =
new Table($this);
210 $table->setHeaders($headers);
211 $table->setRows($rows);
212 $table->setStyle(
'symfony-style-guide');
221 public function ask($question, $default = null, $validator = null)
223 $question =
new Question($question, $default);
224 $question->setValidator($validator);
234 $question =
new Question($question);
236 $question->setHidden(
true);
237 $question->setValidator($validator);
245 public function confirm($question, $default =
true)
253 public function choice($question, array $choices, $default = null)
255 if (null !== $default) {
256 $values = array_flip($choices);
257 $default = $values[$default];
269 $this->progressBar->start();
287 $this->progressBar = null;
297 if (
'\\' === DIRECTORY_SEPARATOR) {
313 if ($this->input->isInteractive()) {
317 if (!$this->questionHelper) {
321 $answer = $this->questionHelper->ask($this->input, $this, $question);
323 if ($this->input->isInteractive()) {
325 $this->bufferedOutput->write(
"\n");
334 public function writeln($messages, $type = self::OUTPUT_NORMAL)
336 parent::writeln($messages, $type);
337 $this->bufferedOutput->writeln($this->
reduceBuffer($messages), $type);
343 public function write($messages, $newline =
false, $type = self::OUTPUT_NORMAL)
345 parent::write($messages, $newline, $type);
346 $this->bufferedOutput->write($this->
reduceBuffer($messages), $newline, $type);
354 parent::newLine($count);
355 $this->bufferedOutput->write(str_repeat(
"\n", $count));
363 if (!$this->progressBar) {
364 throw new \RuntimeException(
'The ProgressBar is not started.');
373 $dimensions = $application->getTerminalDimensions();
375 return $dimensions[0] ?: self::MAX_LINE_LENGTH;
380 $chars = substr(str_replace(PHP_EOL,
"\n", $this->bufferedOutput->fetch()), -2);
382 if (!isset($chars[0])) {
386 $this->
newLine(2 - substr_count($chars,
"\n"));
391 $fetched = $this->bufferedOutput->fetch();
393 if (
"\n" !== substr($fetched, -1)) {
402 return array_map(
function ($value) {
403 return substr($value, -4);
404 }, array_merge(array($this->bufferedOutput->fetch()), (array) $messages));