Skip to content

Commit ec897dd

Browse files
authored
Merge pull request #45 from JBlond/cli-plain-output
add plain output for cli
2 parents 1ecdac5 + 89602af commit ec897dd

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed

README.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ composer require jblond/php-diff
2727

2828
## Example Use
2929

30-
<details><summary>Example Code</summary><br>
31-
3230
```PHP
3331
<?php
3432
use jblond\Diff;
@@ -45,6 +43,7 @@ $options = [
4543
'ignoreWhitespace' => true,
4644
'ignoreCase' => true,
4745
'context' => 2,
46+
'cliColor' => 'simple' // for cli output
4847
];
4948

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

63-
</details>
64-
6562
### Example Output
6663
A quick usage example can be found in the `example/` directory and under example.php. Included is a light theme and a dark theme.
6764

example/cli.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
// Include and instantiate autoloader.
88
require '../vendor/autoload.php';
99

10+
// jblond\cli\Cli
11+
$cli = new Cli();
12+
13+
1014
// Include two sample files for comparison.
1115
$a = file_get_contents(dirname(__FILE__) . '/a.txt');
1216
$b = file_get_contents(dirname(__FILE__) . '/b.txt');
@@ -26,6 +30,11 @@
2630
// \jblond\Diff\Renderer\Text
2731
$renderer = new UnifiedCli();
2832

29-
// jblond\cli\Cli
30-
$cli = new Cli();
33+
3134
$cli->output($diff->render($renderer));
35+
36+
echo "\n\n Now Colored\n\n";
37+
38+
$coloredRenderer = new UnifiedCli(['cliColor'=>'simple']);
39+
40+
$cli->output($diff->render($coloredRenderer));

lib/jblond/Diff/Renderer/Text/UnifiedCli.php

+42-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace jblond\Diff\Renderer\Text;
44

5+
use InvalidArgumentException;
56
use jblond\cli\CliColors;
67
use jblond\Diff\Renderer\RendererAbstract;
78

9+
810
/**
911
* Unified diff generator for PHP DiffLib.
1012
*
@@ -25,6 +27,11 @@ class UnifiedCli extends RendererAbstract
2527
*/
2628
private $colors;
2729

30+
/**
31+
* @var array
32+
*/
33+
protected $options;
34+
2835
/**
2936
* UnifiedCli constructor.
3037
* @param array $options
@@ -33,14 +40,45 @@ public function __construct(array $options = [])
3340
{
3441
parent::__construct($options);
3542
$this->colors = new CliColors();
43+
$this->options = $options;
3644
}
3745

3846
/**
3947
* Render and return a unified diff.
4048
*
4149
* @return string Direct Output to the console
50+
* @throws InvalidArgumentException
4251
*/
4352
public function render(): string
53+
{
54+
if (!isset($this->options['cliColor'])) {
55+
return $this->output();
56+
}
57+
if (isset($this->options['cliColor']) && $this->options['cliColor'] == 'simple') {
58+
return $this->output();
59+
}
60+
throw new InvalidArgumentException('Invalid cliColor option');
61+
}
62+
63+
64+
/**
65+
* @param $string
66+
* @param string $color
67+
* @return string
68+
*/
69+
private function colorizeString($string, $color = ''): string
70+
{
71+
if (isset($this->options['cliColor']) && $this->options['cliColor'] == 'simple') {
72+
return $this->colors->getColoredString($string, $color);
73+
}
74+
return $string;
75+
}
76+
77+
/**
78+
* Render and return a unified colored diff.
79+
* @return string
80+
*/
81+
private function output(): string
4482
{
4583
$diff = '';
4684
$opCodes = $this->diff->getGroupedOpCodes();
@@ -56,7 +94,7 @@ public function render(): string
5694
$i2 = -1;
5795
}
5896

59-
$diff .= $this->colors->getColoredString(
97+
$diff .= $this->colorizeString(
6098
'@@ -' . ($i1 + 1) . ',' . ($i2 - $i1) . ' +' . ($j1 + 1) . ',' . ($j2 - $j1) . " @@\n",
6199
'purple'
62100
);
@@ -66,22 +104,22 @@ public function render(): string
66104
"\n ",
67105
$this->diff->getArrayRange($this->diff->getVersion1(), $i1, $i2)
68106
);
69-
$diff .= $this->colors->getColoredString(' ' . $string . "\n", 'grey');
107+
$diff .= $this->colorizeString(' ' . $string . "\n", 'grey');
70108
continue;
71109
}
72110
if ($tag == 'replace' || $tag == 'delete') {
73111
$string = implode(
74112
"\n- ",
75113
$this->diff->getArrayRange($this->diff->getVersion1(), $i1, $i2)
76114
);
77-
$diff .= $this->colors->getColoredString('-' . $string . "\n", 'light_red');
115+
$diff .= $this->colorizeString('-' . $string . "\n", 'light_red');
78116
}
79117
if ($tag == 'replace' || $tag == 'insert') {
80118
$string = implode(
81119
"\n+",
82120
$this->diff->getArrayRange($this->diff->getVersion2(), $j1, $j2)
83121
);
84-
$diff .= $this->colors->getColoredString('+' . $string . "\n", 'light_green');
122+
$diff .= $this->colorizeString('+' . $string . "\n", 'light_green');
85123
}
86124
}
87125
}

0 commit comments

Comments
 (0)