diff --git a/README.md b/README.md index 4314dbc9..8ddbab70 100644 --- a/README.md +++ b/README.md @@ -27,8 +27,6 @@ composer require jblond/php-diff ## Example Use -
Example Code
- ```PHP true, 'ignoreCase' => true, 'context' => 2, + 'cliColor' => 'simple' // for cli output ]; // Initialize the diff class. @@ -60,8 +59,6 @@ $renderer = new SideBySide([ echo $diff->Render($renderer); ``` -
- ### 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. diff --git a/example/cli.php b/example/cli.php index 189ab2a0..77cf8f50 100644 --- a/example/cli.php +++ b/example/cli.php @@ -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'); @@ -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)); diff --git a/lib/jblond/Diff/Renderer/Text/UnifiedCli.php b/lib/jblond/Diff/Renderer/Text/UnifiedCli.php index 32e879a6..3e85bf68 100644 --- a/lib/jblond/Diff/Renderer/Text/UnifiedCli.php +++ b/lib/jblond/Diff/Renderer/Text/UnifiedCli.php @@ -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. * @@ -25,6 +27,11 @@ class UnifiedCli extends RendererAbstract */ private $colors; + /** + * @var array + */ + protected $options; + /** * UnifiedCli constructor. * @param array $options @@ -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(); @@ -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' ); @@ -66,7 +104,7 @@ 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') { @@ -74,14 +112,14 @@ public function render(): string "\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'); } } }