From d7bbe128f3437cbf85508ce759f806faf97ab67f Mon Sep 17 00:00:00 2001 From: JBlond Date: Mon, 13 Jul 2020 08:20:43 +0200 Subject: [PATCH 1/2] add plain output for cli --- README.md | 5 +-- example/cli.php | 17 +++++++- lib/jblond/Diff/Renderer/Text/UnifiedCli.php | 46 ++++++++++++++++++-- 3 files changed, 58 insertions(+), 10 deletions(-) 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..97a5b2cf 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,15 @@ // \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)); + +$coloredWordBasedRenderer = new UnifiedCli(['cliColor'=>'wordBased']); + +$cli->output($diff->render($coloredWordBasedRenderer)); 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'); } } } From 89602af6954d50b15c519cc5829686b5e7130e02 Mon Sep 17 00:00:00 2001 From: JBlond Date: Mon, 13 Jul 2020 11:32:16 +0200 Subject: [PATCH 2/2] remove output example --- example/cli.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/example/cli.php b/example/cli.php index 97a5b2cf..77cf8f50 100644 --- a/example/cli.php +++ b/example/cli.php @@ -38,7 +38,3 @@ $coloredRenderer = new UnifiedCli(['cliColor'=>'simple']); $cli->output($diff->render($coloredRenderer)); - -$coloredWordBasedRenderer = new UnifiedCli(['cliColor'=>'wordBased']); - -$cli->output($diff->render($coloredWordBasedRenderer));