8000 [Console] Support iterable in SymfonyStyle::write/writeln by ogizanagi · Pull Request #26863 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console] Support iterable in SymfonyStyle::write/writeln #26863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions src/Symfony/Component/Console/Style/SymfonyStyle.php
< 8000 td id="diff-a016121f5b0b9bd1b17bff1c650034b0fddb954b79637126dd294fc8875a7609R327" data-line-number="327" class="blob-num blob-num-addition js-linkable-line-number js-blob-rnum">
Original file line number Diff line number Diff line change
Expand Up @@ -306,23 +306,29 @@ public function askQuestion(Question $question)
*/
public function writeln($messages, $type = self::OUTPUT_NORMAL)
{
if ($messages instanceof \Traversable) {
$messages = iterator_to_array($messages, false);
if (!is_iterable($messages)) {
$messages = array($messages);
}

foreach ($messages as $message) {
parent::writeln($message, $type);
$this->writeBuffer($message, true, $type);
}
parent::writeln($messages, $type);
$this->bufferedOutput->writeln($this->reduceBuffer($messages), $type);
}

/**
* {@inheritdoc}
*/
public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL)
{
if ($messages instanceof \Traversable) {
$messages = iterator_to_array($messages, false);
if (!is_iterable($messages)) {
$messages = array($messages);
}

foreach ($messages as $message) {
parent::write($message, $newline, $type);
$this->writeBuffer($message, $newline, $type);
}
parent::write($messages, $newline, $type);
$this->bufferedOutput->write($this->reduceBuffer($messages), $newline, $type);
}

/**
Expand Down Expand Up @@ -375,13 +381,11 @@ private function autoPrependText(): void
}
}

private function reduceBuffer($messages): array
private function writeBuffer(string $message, bool $newLine, int $type): void
{
// We need to know if the two last chars are PHP_EOL
// Preserve the last 4 chars inserted (PHP_EOL on windows is two chars) in the history buffer
return array_map(function ($value) {
return substr($value, -4);
}, array_merge(array($this->bufferedOutput->fetch()), (array) $messages));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so the fetch part was useless all along?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wasn't useless, but as we now only handle strings one by one and are writing to the buffer directly (instead of concatenating arrays of buffered output), it is not required anymore.

$this->bufferedOutput->write(substr($message, -4), $newLine, $type);
}

private function createBlock(iterable $messages, string $type = null, string $style = null, string $prefix = ' ', bool $padding = false, bool $escape = false)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

//Ensure has single blank line after any text and a title
return function (InputInterface $input, OutputInterface $output) {
$output = new SymfonyStyle($input, $output);

$output->write('Lorem ipsum dolor sit amet');
$output->title('First title');

$output->writeln('Lorem ipsum dolor sit amet');
$output->title('Second title');

$output->write('Lorem ipsum dolor sit amet');
$output->write('');
$output->title('Third title');

//Ensure edge case by appending empty strings to history:
$output->write('Lorem ipsum dolor sit amet');
$output->write(new \ArrayIterator(array('', '', '')));
$output->title('Fourth title');

//Ensure have manual control over number of blank lines:
$output->writeln('Lorem ipsum dolor sit amet');
$output->writeln(new \ArrayIterator(array('', ''))); //Should append an extra blank line
$output->title('Fifth title');

$output->writeln('Lorem ipsum dolor sit amet');
$output->newLine(2); //Should append an extra blank line
$output->title('Fifth title');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sixth :)

};
73D0
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Lorem ipsum dolor sit amet

First title
===========

Lorem ipsum dolor sit amet

Second title
============

Lorem ipsum dolor sit amet

Third title
===========

Lorem ipsum dolor sit amet

Fourth title
============

Lorem ipsum dolor sit amet


Fifth title
===========

Lorem ipsum dolor sit amet


Fifth title
===========

0