Skip to content

Commit

Permalink
Merge pull request #45 from JBlond/cli-plain-output
Browse files Browse the repository at this point in the history
add plain output for cli
  • Loading branch information
JBlond authored Jul 13, 2020
2 parents 1ecdac5 + 89602af commit ec897dd
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ composer require jblond/php-diff

## Example Use

<details><summary>Example Code</summary><br>

```PHP
<?php
use jblond\Diff;
Expand All @@ -45,6 +43,7 @@ $options = [
'ignoreWhitespace' => true,
'ignoreCase' => true,
'context' => 2,
'cliColor' => 'simple' // for cli output
];

// Initialize the diff class.
Expand All @@ -60,8 +59,6 @@ $renderer = new SideBySide([
echo $diff->Render($renderer);
```

</details>

### Example Output
A quick usage example can be found in the `example/` directory and under example.php. Included is a light theme and a dark theme.

Expand Down
13 changes: 11 additions & 2 deletions example/cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
// Include and instantiate autoloader.
require '../vendor/autoload.php';

// jblond\cli\Cli
$cli = new Cli();


// Include two sample files for comparison.
$a = file_get_contents(dirname(__FILE__) . '/a.txt');
$b = file_get_contents(dirname(__FILE__) . '/b.txt');
Expand All @@ -26,6 +30,11 @@
// \jblond\Diff\Renderer\Text
$renderer = new UnifiedCli();

// jblond\cli\Cli
$cli = new Cli();

$cli->output($diff->render($renderer));

echo "\n\n Now Colored\n\n";

$coloredRenderer = new UnifiedCli(['cliColor'=>'simple']);

$cli->output($diff->render($coloredRenderer));
46 changes: 42 additions & 4 deletions lib/jblond/Diff/Renderer/Text/UnifiedCli.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace jblond\Diff\Renderer\Text;

use InvalidArgumentException;
use jblond\cli\CliColors;
use jblond\Diff\Renderer\RendererAbstract;


/**
* Unified diff generator for PHP DiffLib.
*
Expand All @@ -25,6 +27,11 @@ class UnifiedCli extends RendererAbstract
*/
private $colors;

/**
* @var array
*/
protected $options;

/**
* UnifiedCli constructor.
* @param array $options
Expand All @@ -33,14 +40,45 @@ public function __construct(array $options = [])
{
parent::__construct($options);
$this->colors = new CliColors();
$this->options = $options;
}

/**
* Render and return a unified diff.
*
* @return string Direct Output to the console
* @throws InvalidArgumentException
*/
public function render(): string
{
if (!isset($this->options['cliColor'])) {
return $this->output();
}
if (isset($this->options['cliColor']) && $this->options['cliColor'] == 'simple') {
return $this->output();
}
throw new InvalidArgumentException('Invalid cliColor option');
}


/**
* @param $string
* @param string $color
* @return string
*/
private function colorizeString($string, $color = ''): string
{
if (isset($this->options['cliColor']) && $this->options['cliColor'] == 'simple') {
return $this->colors->getColoredString($string, $color);
}
return $string;
}

/**
* Render and return a unified colored diff.
* @return string
*/
private function output(): string
{
$diff = '';
$opCodes = $this->diff->getGroupedOpCodes();
Expand All @@ -56,7 +94,7 @@ public function render(): string
$i2 = -1;
}

$diff .= $this->colors->getColoredString(
$diff .= $this->colorizeString(
'@@ -' . ($i1 + 1) . ',' . ($i2 - $i1) . ' +' . ($j1 + 1) . ',' . ($j2 - $j1) . " @@\n",
'purple'
);
Expand All @@ -66,22 +104,22 @@ public function render(): string
"\n ",
$this->diff->getArrayRange($this->diff->getVersion1(), $i1, $i2)
);
$diff .= $this->colors->getColoredString(' ' . $string . "\n", 'grey');
$diff .= $this->colorizeString(' ' . $string . "\n", 'grey');
continue;
}
if ($tag == 'replace' || $tag == 'delete') {
$string = implode(
"\n- ",
$this->diff->getArrayRange($this->diff->getVersion1(), $i1, $i2)
);
$diff .= $this->colors->getColoredString('-' . $string . "\n", 'light_red');
$diff .= $this->colorizeString('-' . $string . "\n", 'light_red');
}
if ($tag == 'replace' || $tag == 'insert') {
$string = implode(
"\n+",
$this->diff->getArrayRange($this->diff->getVersion2(), $j1, $j2)
);
$diff .= $this->colors->getColoredString('+' . $string . "\n", 'light_green');
$diff .= $this->colorizeString('+' . $string . "\n", 'light_green');
}
}
}
Expand Down

0 comments on commit ec897dd

Please sign in to comment.