Skip to content

Commit 8d97115

Browse files
authored
Merge pull request #38 from JBlond/console-support
Add Console support
2 parents 39044ed + 8fcc5fb commit 8d97115

File tree

8 files changed

+152
-52
lines changed

8 files changed

+152
-52
lines changed

README.md

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88

99
A comprehensive library for generating differences between two hashable objects (strings or arrays).
1010
Generated differences can be rendered in all of the standard formats including:
11+
1112
* Unified
1213
* Context
1314
* Inline HTML
1415
* Side by Side HTML
1516
* Unified HTML
17+
* Unified Commandline colored output
1618

17-
The logic behind the core of the diff engine (ie, the sequence matcher) is primarily based on the Python difflib
19+
The logic behind the core of the diff engine (ie, the sequence matcher) is primarily based on the Python difflib
1820
package. The reason for doing so is primarily because of its high degree of accuracy.
1921

2022
## Install
@@ -29,16 +31,11 @@ composer require jblond/php-diff
2931

3032
```PHP
3133
<?php
32-
use jblond\Autoloader;
3334
use jblond\Diff;
3435
use jblond\Diff\Renderer\Html\SideBySide;
3536

3637
// Installed via composer...
3738
require 'vendor/autoload.php';
38-
// ...or installed manually.
39-
require dirname(__FILE__).'/../lib/Autoloader.php';
40-
41-
new Autoloader();
4239

4340
$a = file_get_contents(dirname(__FILE__).'/a.txt');
4441
$b = file_get_contents(dirname(__FILE__).'/b.txt');
@@ -68,25 +65,46 @@ echo $diff->Render($renderer);
6865
### Example Output
6966
A quick usage example can be found in the `example/` directory and under example.php. Included is a light theme and a dark theme.
7067

71-
<details><summary>Example Pictures</summary><br>
72-
7368
#### HTML Side By Side Example
69+
7470
![HTML Side By Side Example](htmlSideBySide.png "HTML Side By Side Example")
71+
72+
<details><summary>More Example Pictures</summary><br>
73+
7574
#### HTML Inline Example
75+
7676
![HTML Inline Example](htmlInline.png "HTML Inline Example")
77+
7778
#### HTML Unified Example
79+
7880
![HTML Unified Example](htmlUnified.png "HTML Unified Example")
81+
7982
#### Text Unified Example
83+
8084
![Text Unified Example](textUnified.png "Text Unified Example")
85+
8186
#### Text Context Example
87+
8288
![Text Context Example](textContext.png "Text Context Example")
8389

90+
#### Text Unified Console Example
91+
92+
![Text Unified Console Example](textUnifiedCli.png "Text Unified Console Example")
93+
94+
</details>
95+
96+
<details><summary>HTML Side By Side Dark Theme Example</summary><br>
97+
98+
99+
![HTML Side By Side Dark Theme Example](htmlSidebySideDarkTheme.png "HTML Side By Side Dark Theme Example")
100+
84101
</details>
85102

86103
## Requirements
87104

88105
* PHP 7.2 or greater
89106
* PHP Multibyte String
107+
* [jblond/php-cli](https://github.com/jblond/php-cli)
90108

91109
## Merge files using jQuery
92110

@@ -97,7 +115,7 @@ Have a look at [jQuery-Merge-for-php-diff](https://github.com/Xiphe/jQuery-Merge
97115

98116
* Ability to ignore blank line changes
99117
* 3 way diff support
100-
118+
101119
## Contributors
102120

103121
Contributors since I forked the repo.

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
],
2626
"require": {
2727
"php": ">=7.2",
28-
"ext-mbstring": "*"
28+
"ext-mbstring": "*",
29+
"jblond/php-cli": "^1.0"
2930
},
3031
"require-dev": {
3132
"phpunit/phpunit": "8.*",

example/cli.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use jblond\cli\Cli;
4+
use jblond\Diff;
5+
use jblond\Diff\Renderer\Text\UnifiedCli;
6+
7+
// Include and instantiate autoloader.
8+
require '../vendor/autoload.php';
9+
10+
// Include two sample files for comparison.
11+
$a = file_get_contents(dirname(__FILE__) . '/a.txt');
12+
$b = file_get_contents(dirname(__FILE__) . '/b.txt');
13+
14+
$customOptions = [
15+
'context' => 2,
16+
'trimEqual' => false,
17+
'ignoreWhitespace' => true,
18+
'ignoreCase' => true,
19+
];
20+
21+
// Choose one of the initializations.
22+
$diff = new Diff($a, $b);
23+
24+
25+
// Generate a unified diff.
26+
// \jblond\Diff\Renderer\Text
27+
$renderer = new UnifiedCli();
28+
29+
// jblond\cli\Cli
30+
$cli = new Cli();
31+
$cli->output($diff->render($renderer));

example/example.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
use jblond\Autoloader;
43
use jblond\Diff;
54
use jblond\Diff\Renderer\Html\Inline;
65
use jblond\Diff\Renderer\Html\Unified as HtmlUnified;
@@ -9,8 +8,7 @@
98
use jblond\Diff\Renderer\Text\Unified;
109

1110
// Include and instantiate autoloader.
12-
require dirname(__FILE__) . '/../lib/Autoloader.php';
13-
new Autoloader();
11+
require '../vendor/autoload.php';
1412

1513
// Include two sample files for comparison.
1614
$a = file_get_contents(dirname(__FILE__) . '/a.txt');
@@ -66,6 +64,7 @@ function changeCSS(cssFile, cssLinkIndex) {
6664
'title2' => 'Custom title for version2',
6765
]);
6866
echo $diff->Render($renderer);
67+
$cli = new \jblond\cli\Cli();
6968
?>
7069

7170
<h2>HTML Inline Diff</h2>

htmlSidebySideDarkTheme.png

138 KB
Loading

lib/Autoloader.php

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace jblond\Diff\Renderer\Text;
4+
5+
use jblond\cli\CliColors;
6+
use jblond\Diff\Renderer\RendererAbstract;
7+
8+
/**
9+
* Unified diff generator for PHP DiffLib.
10+
*
11+
* PHP version 7.2 or greater
12+
*
13+
* @package jblond\Diff\Renderer\Text
14+
* @author Mario Brandt <[email protected]>
15+
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
16+
* @version 1.18
17+
* @link https://github.com/JBlond/php-diff
18+
*/
19+
20+
class UnifiedCli extends RendererAbstract
21+
{
22+
23+
/**
24+
* @var CliColors
25+
*/
26+
private $colors;
27+
28+
/**
29+
* UnifiedCli constructor.
30+
* @param array $options
31+
*/
32+
public function __construct(array $options = [])
33+
{
34+
parent::__construct($options);
35+
$this->colors = new CliColors();
36+
}
37+
38+
/**
39+
* Render and return a unified diff.
40+
*
41+
* @return string Direct Output to the console
42+
*/
43+
public function render(): string
44+
{
45+
$diff = '';
46+
$opCodes = $this->diff->getGroupedOpCodes();
47+
foreach ($opCodes as $group) {
48+
$lastItem = count($group) - 1;
49+
$i1 = $group['0']['1'];
50+
$i2 = $group[$lastItem]['2'];
51+
$j1 = $group['0']['3'];
52+
$j2 = $group[$lastItem]['4'];
53+
54+
if ($i1 == 0 && $i2 == 0) {
55+
$i1 = -1;
56+
$i2 = -1;
57+
}
58+
59+
$diff .= $this->colors->getColoredString(
60+
'@@ -' . ($i1 + 1) . ',' . ($i2 - $i1) . ' +' . ($j1 + 1) . ',' . ($j2 - $j1) . " @@\n",
61+
'purple'
62+
);
63+
foreach ($group as [$tag, $i1, $i2, $j1, $j2]) {
64+
if ($tag == 'equal') {
65+
$string = implode(
66+
"\n ",
67+
$this->diff->getArrayRange($this->diff->getVersion1(), $i1, $i2)
68+
);
69+
$diff .= $this->colors->getColoredString(' ' . $string . "\n", 'grey');
70+
continue;
71+
}
72+
if ($tag == 'replace' || $tag == 'delete') {
73+
$string = implode(
74+
"\n- ",
75+
$this->diff->getArrayRange($this->diff->getVersion1(), $i1, $i2)
76+
);
77+
$diff .= $this->colors->getColoredString('-' . $string . "\n", 'light_red');
78+
}
79+
if ($tag == 'replace' || $tag == 'insert') {
80+
$string = implode(
81+
"\n+",
82+
$this->diff->getArrayRange($this->diff->getVersion2(), $j1, $j2)
83+
);
84+
$diff .= $this->colors->getColoredString('+' . $string . "\n", 'light_green');
85+
}
86+
}
87+
}
88+
return $diff;
89+
}
90+
}

textUnifiedCli.png

162 KB
Loading

0 commit comments

Comments
 (0)