Skip to content

Add Console support #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@

A comprehensive library for generating differences between two hashable objects (strings or arrays).
Generated differences can be rendered in all of the standard formats including:

* Unified
* Context
* Inline HTML
* Side by Side HTML
* Unified HTML
* Unified Commandline colored output

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

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

```PHP
<?php
use jblond\Autoloader;
use jblond\Diff;
use jblond\Diff\Renderer\Html\SideBySide;

// Installed via composer...
require 'vendor/autoload.php';
// ...or installed manually.
require dirname(__FILE__).'/../lib/Autoloader.php';

new Autoloader();

$a = file_get_contents(dirname(__FILE__).'/a.txt');
$b = file_get_contents(dirname(__FILE__).'/b.txt');
Expand Down Expand Up @@ -68,25 +65,46 @@ 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.

<details><summary>Example Pictures</summary><br>

#### HTML Side By Side Example

![HTML Side By Side Example](htmlSideBySide.png "HTML Side By Side Example")

<details><summary>More Example Pictures</summary><br>

#### HTML Inline Example

![HTML Inline Example](htmlInline.png "HTML Inline Example")

#### HTML Unified Example

![HTML Unified Example](htmlUnified.png "HTML Unified Example")

#### Text Unified Example

![Text Unified Example](textUnified.png "Text Unified Example")

#### Text Context Example

![Text Context Example](textContext.png "Text Context Example")

#### Text Unified Console Example

![Text Unified Console Example](textUnifiedCli.png "Text Unified Console Example")

</details>

<details><summary>HTML Side By Side Dark Theme Example</summary><br>


![HTML Side By Side Dark Theme Example](htmlSidebySideDarkTheme.png "HTML Side By Side Dark Theme Example")

</details>

## Requirements

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

## Merge files using jQuery

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

* Ability to ignore blank line changes
* 3 way diff support

## Contributors

Contributors since I forked the repo.
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
],
"require": {
"php": ">=7.2",
"ext-mbstring": "*"
"ext-mbstring": "*",
"jblond/php-cli": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "8.*",
Expand Down
31 changes: 31 additions & 0 deletions example/cli.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use jblond\cli\Cli;
use jblond\Diff;
use jblond\Diff\Renderer\Text\UnifiedCli;

// Include and instantiate autoloader.
require '../vendor/autoload.php';

// Include two sample files for comparison.
$a = file_get_contents(dirname(__FILE__) . '/a.txt');
$b = file_get_contents(dirname(__FILE__) . '/b.txt');

$customOptions = [
'context' => 2,
'trimEqual' => false,
'ignoreWhitespace' => true,
'ignoreCase' => true,
];

// Choose one of the initializations.
$diff = new Diff($a, $b);


// Generate a unified diff.
// \jblond\Diff\Renderer\Text
$renderer = new UnifiedCli();

// jblond\cli\Cli
$cli = new Cli();
$cli->output($diff->render($renderer));
5 changes: 2 additions & 3 deletions example/example.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

use jblond\Autoloader;
use jblond\Diff;
use jblond\Diff\Renderer\Html\Inline;
use jblond\Diff\Renderer\Html\Unified as HtmlUnified;
Expand All @@ -9,8 +8,7 @@
use jblond\Diff\Renderer\Text\Unified;

// Include and instantiate autoloader.
require dirname(__FILE__) . '/../lib/Autoloader.php';
new Autoloader();
require '../vendor/autoload.php';

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

<h2>HTML Inline Diff</h2>
Expand Down
Binary file added htmlSidebySideDarkTheme.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 0 additions & 39 deletions lib/Autoloader.php

This file was deleted.

90 changes: 90 additions & 0 deletions lib/jblond/Diff/Renderer/Text/UnifiedCli.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace jblond\Diff\Renderer\Text;

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

/**
* Unified diff generator for PHP DiffLib.
*
* PHP version 7.2 or greater
*
* @package jblond\Diff\Renderer\Text
* @author Mario Brandt <[email protected]>
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
* @version 1.18
* @link https://github.com/JBlond/php-diff
*/

class UnifiedCli extends RendererAbstract
{

/**
* @var CliColors
*/
private $colors;

/**
* UnifiedCli constructor.
* @param array $options
*/
public function __construct(array $options = [])
{
parent::__construct($options);
$this->colors = new CliColors();
}

/**
* Render and return a unified diff.
*
* @return string Direct Output to the console
*/
public function render(): string
{
$diff = '';
$opCodes = $this->diff->getGroupedOpCodes();
foreach ($opCodes as $group) {
$lastItem = count($group) - 1;
$i1 = $group['0']['1'];
$i2 = $group[$lastItem]['2'];
$j1 = $group['0']['3'];
$j2 = $group[$lastItem]['4'];

if ($i1 == 0 && $i2 == 0) {
$i1 = -1;
$i2 = -1;
}

$diff .= $this->colors->getColoredString(
'@@ -' . ($i1 + 1) . ',' . ($i2 - $i1) . ' +' . ($j1 + 1) . ',' . ($j2 - $j1) . " @@\n",
'purple'
);
foreach ($group as [$tag, $i1, $i2, $j1, $j2]) {
if ($tag == 'equal') {
$string = implode(
"\n ",
$this->diff->getArrayRange($this->diff->getVersion1(), $i1, $i2)
);
$diff .= $this->colors->getColoredString(' ' . $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');
}
if ($tag == 'replace' || $tag == 'insert') {
$string = implode(
"\n+",
$this->diff->getArrayRange($this->diff->getVersion2(), $j1, $j2)
);
$diff .= $this->colors->getColoredString('+' . $string . "\n", 'light_green');
}
}
}
return $diff;
}
}
Binary file added textUnifiedCli.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.