Skip to content

Commit

Permalink
Fixes #76: Do not print a newline after output.
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-1-anderson committed Jan 27, 2019
1 parent 243ce46 commit f6d948d
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 11 deletions.
14 changes: 14 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- [\Consolidation\OutputFormatters\Formatters\StringFormatter](#class-consolidationoutputformattersformattersstringformatter)
- [\Consolidation\OutputFormatters\Formatters\VarExportFormatter](#class-consolidationoutputformattersformattersvarexportformatter)
- [\Consolidation\OutputFormatters\Formatters\YamlFormatter](#class-consolidationoutputformattersformattersyamlformatter)
- [\Consolidation\OutputFormatters\Formatters\ImplicitEolInterface (interface)](#interface-consolidationoutputformattersformattersimpliciteolinterface)
- [\Consolidation\OutputFormatters\Formatters\NoOutputFormatter](#class-consolidationoutputformattersformattersnooutputformatter)
- [\Consolidation\OutputFormatters\Formatters\TableFormatter](#class-consolidationoutputformattersformatterstableformatter)
- [\Consolidation\OutputFormatters\Formatters\XmlFormatter](#class-consolidationoutputformattersformattersxmlformatter)
Expand Down Expand Up @@ -329,6 +330,15 @@

<hr />

### Interface: \Consolidation\OutputFormatters\Formatters\ImplicitEolInterface

> This is a marker interface that should be implemented by all formatters that always print an end-of-line character at the end of their input.
| Visibility | Function |
|:-----------|:---------|

<hr />

### Class: \Consolidation\OutputFormatters\Formatters\NoOutputFormatter

> No output formatter This formatter never produces any output. It is useful in cases where a command should not produce any output by default, but may do so if the user explicitly includes a --format option.
Expand Down Expand Up @@ -448,8 +458,10 @@
| public | <strong>getInputOptions(</strong><em>array</em> <strong>$defaults</strong>)</strong> : <em>array</em><br /><em>Return all of the options from the provided $defaults array that exist in our InputInterface object.</em> |
| public | <strong>getOptions()</strong> : <em>array</em><br /><em>Return a reference to the user-specified options for this request.</em> |
| public | <strong>getXmlSchema()</strong> : <em>[\Consolidation\OutputFormatters\StructuredData\Xml\XmlSchema](#class-consolidationoutputformattersstructureddataxmlxmlschema)</em><br /><em>Return the XmlSchema to use with --format=xml for data types that support that. This is used when an array needs to be converted into xml.</em> |
| public | <strong>isInteractive()</strong> : <em>bool</em><br /><em>Check to see if we are in interactive mode. If we were never given a reference to the input object, then assume not.</em> |
| public | <strong>override(</strong><em>array</em> <strong>$configurationData</strong>)</strong> : <em>[\Consolidation\OutputFormatters\Options\FormatterOptions](#class-consolidationoutputformattersoptionsformatteroptions)</em><br /><em>Create a new FormatterOptions object with new configuration data (provided), and the same options data as this instance.</em> |
| public | <strong>parsePropertyList(</strong><em>string</em> <strong>$value</strong>)</strong> : <em>array</em><br /><em>Convert from a textual list to an array</em> |
| public | <strong>removeOption(</strong><em>mixed</em> <strong>$key</strong>)</strong> : <em>void</em><br /><em>Remove an option.</em> |
| public | <strong>setConfigurationData(</strong><em>array</em> <strong>$configurationData</strong>)</strong> : <em>[\Consolidation\OutputFormatters\Options\FormatterOptions](#class-consolidationoutputformattersoptionsformatteroptions)</em><br /><em>Change the configuration data for this formatter options object.</em> |
| public | <strong>setConfigurationDefault(</strong><em>string</em> <strong>$key</strong>, <em>mixed</em> <strong>$value</strong>)</strong> : <em>\Consolidation\OutputFormatters\Options\FormetterOptions</em><br /><em>Change one configuration value for this formatter option, but only if it does not already have a value set.</em> |
| public | <strong>setDefaultFields(</strong><em>mixed</em> <strong>$fields</strong>)</strong> : <em>void</em> |
Expand All @@ -465,6 +477,8 @@
| public | <strong>setRowLabels(</strong><em>mixed</em> <strong>$rowLabels</strong>)</strong> : <em>void</em> |
| public | <strong>setTableStyle(</strong><em>mixed</em> <strong>$style</strong>)</strong> : <em>void</em> |
| public | <strong>setWidth(</strong><em>mixed</em> <strong>$width</strong>)</strong> : <em>void</em> |
| public | <strong>shouldAppendNewline(</strong><em>\Symfony\Component\Console\Output\OutputInterface</em> <strong>$output</strong>)</strong> : <em>bool</em> |
| protected | <strong>defaultAppendEol(</strong><em>\Symfony\Component\Console\Output\OutputInterface</em> <strong>$output</strong>)</strong> : <em>void</em> |
| protected | <strong>defaultsForKey(</strong><em>string</em> <strong>$key</strong>, <em>array</em> <strong>$defaults</strong>, <em>bool</em> <strong>$default=false</strong>)</strong> : <em>array</em><br /><em>Reduce provided defaults to the single item identified by '$key', if it exists, or an empty array otherwise.</em> |
| protected | <strong>fetch(</strong><em>string</em> <strong>$key</strong>, <em>array</em> <strong>$defaults=array()</strong>, <em>bool/mixed</em> <strong>$default=false</strong>)</strong> : <em>mixed</em><br /><em>Look up a key, and return its raw value.</em> |
| protected | <strong>fetchRawValues(</strong><em>array</em> <strong>$defaults=array()</strong>)</strong> : <em>array</em><br /><em>Look up all of the items associated with the provided defaults.</em> |
Expand Down
6 changes: 6 additions & 0 deletions src/FormatterManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Consolidation\OutputFormatters\StructuredData\OriginalDataInterface;
use Consolidation\OutputFormatters\StructuredData\ListDataFromKeys;
use Consolidation\OutputFormatters\StructuredData\ConversionInterface;
use Consolidation\OutputFormatters\Formatters\ImplicitEolInterface;

/**
* Manage a collection of formatters; return one on request.
Expand Down Expand Up @@ -238,6 +239,11 @@ public function write(OutputInterface $output, $format, $structuredOutput, Forma
$formatter->writeMetadata($output, $structuredOutput, $options);
}
$formatter->write($output, $restructuredOutput, $options);
// In interactive mode, write an extra newline after the output,
// but only if the formatter has not already implicitly done so.
if ($options->shouldAppendNewline($output) && !$formatter instanceof ImplicitEolInterface) {
$output->writeln();
}
}

protected function validateAndRestructure(FormatterInterface $formatter, $structuredOutput, FormatterOptions $options)
Expand Down
11 changes: 11 additions & 0 deletions src/Formatters/ImplicitEolInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
namespace Consolidation\OutputFormatters\Formatters;

/**
* This is a marker interface that should be implemented by
* all formatters that always print an end-of-line character
* at the end of their input.
*/
interface ImplicitEolInterface
{
}
2 changes: 1 addition & 1 deletion src/Formatters/JsonFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class JsonFormatter implements FormatterInterface
*/
public function write(OutputInterface $output, $data, FormatterOptions $options)
{
$output->writeln(json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
$output->write(json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
}
2 changes: 1 addition & 1 deletion src/Formatters/ListFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ListFormatter implements FormatterInterface, OverrideRestructureInterface,
*/
public function write(OutputInterface $output, $data, FormatterOptions $options)
{
$output->writeln(implode("\n", $data));
$output->write(implode("\n", $data));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Formatters/PrintRFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class PrintRFormatter implements FormatterInterface
*/
public function write(OutputInterface $output, $data, FormatterOptions $options)
{
$output->writeln(print_r($data, true));
$output->write(print_r($data, true));
}
}
2 changes: 1 addition & 1 deletion src/Formatters/SerializeFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class SerializeFormatter implements FormatterInterface
*/
public function write(OutputInterface $output, $data, FormatterOptions $options)
{
$output->writeln(serialize($data));
$output->write(serialize($data));
}
}
2 changes: 1 addition & 1 deletion src/Formatters/StringFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function isValidDataType(\ReflectionClass $dataType)
public function write(OutputInterface $output, $data, FormatterOptions $options)
{
if (is_string($data)) {
return $output->writeln($data);
return $output->write($data);
}
return $this->reduceToSigleFieldAndWrite($output, $data, $options);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Formatters/VarDumpFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function write(OutputInterface $output, $data, FormatterOptions $options)
// VarDumper v2.
$stream = fopen('php://memory', 'r+b');
$dumper->dump($cloned_data, $stream);
$output->writeln(stream_get_contents($stream, -1, 0));
$output->write(stream_get_contents($stream, -1, 0));
fclose($stream);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Formatters/VarExportFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class VarExportFormatter implements FormatterInterface
*/
public function write(OutputInterface $output, $data, FormatterOptions $options)
{
$output->writeln(var_export($data, true));
$output->write(var_export($data, true));
}
}
2 changes: 1 addition & 1 deletion src/Formatters/XmlFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,6 @@ public function write(OutputInterface $output, $dom, FormatterOptions $options)
$dom = $schema->arrayToXML($dom);
}
$dom->formatOutput = true;
$output->writeln($dom->saveXML());
$output->write($dom->saveXML());
}
}
2 changes: 1 addition & 1 deletion src/Formatters/YamlFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ public function write(OutputInterface $output, $data, FormatterOptions $options)
$indent = 2;
// The level where you switch to inline YAML is set to PHP_INT_MAX to
// ensure this does not occur.
$output->writeln(Yaml::dump($data, PHP_INT_MAX, $indent, false, true));
$output->write(Yaml::dump($data, PHP_INT_MAX, $indent, false, true));
}
}
54 changes: 52 additions & 2 deletions src/Options/FormatterOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
namespace Consolidation\OutputFormatters\Options;

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

use Consolidation\OutputFormatters\Transformations\PropertyParser;
use Consolidation\OutputFormatters\StructuredData\Xml\XmlSchema;
use Consolidation\OutputFormatters\StructuredData\Xml\XmlSchemaInterface;
use Symfony\Component\Console\Output\ConsoleOutput;

/**
* FormetterOptions holds information that affects the way a formatter
Expand Down Expand Up @@ -95,8 +98,6 @@ public function setListDelimiter($listDelimiter)
return $this->setConfigurationValue(self::LIST_DELIMITER, $listDelimiter);
}



public function setIncludeFieldLables($includFieldLables)
{
return $this->setConfigurationValue(self::INCLUDE_FIELD_LABELS, $includFieldLables);
Expand Down Expand Up @@ -325,6 +326,14 @@ public function setOptions($options)
return $this;
}

/**
* Remove an option.
*/
public function removeOption($key)
{
unset($this->options[$key]);
}

/**
* Change one option value specified by the user for this request.
*
Expand Down Expand Up @@ -383,4 +392,45 @@ public function getInputOptions($defaults)
}
return $options;
}

public function shouldAppendNewline(OutputInterface $output)
{
return $this->fetch('append-eol', [], $this->defaultAppendEol($output));
}

protected function defaultAppendEol(OutputInterface $output)
{
// If we have a reference to the input object, and we are
// not in interactive mode, then by default do not append an eol.
if ($this->input && !$this->isInteractive()) {
return false;
}

// If we are not writing to the console, then by default
// do not append an eol.
if (!$output instanceof ConsoleOutput) {
return false;
}

// If we cannot tell whether output is being redirected or not,
// assume that it is not.
if (function_exists('posix_isatty')) {
return true;
}

// If stdout is a tty, then add an EOL. Otherwise, do not.
return posix_isatty(STDOUT);
}

/**
* Check to see if we are in interactive mode. If we were never
* given a reference to the input object, then assume not.
*/
public function isInteractive()
{
if (!$this->input) {
return false;
}
return $this->input->isInteractive();
}
}

0 comments on commit f6d948d

Please sign in to comment.