Skip to content

Commit 4192d8b

Browse files
committed
Add Cli color support
1 parent 7a49d76 commit 4192d8b

File tree

3 files changed

+121
-1
lines changed

3 files changed

+121
-1
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
],
2424
"require": {
2525
"php": ">=7.2",
26-
"ext-mbstring": "*"
26+
"ext-mbstring": "*",
27+
"jblond/php-cli": "*"
2728
},
2829
"require-dev": {
2930
"phpunit/phpunit": "8.*",

example/cli.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
$cli = new Cli();
29+
$cli->output($diff->render($renderer));
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+
$diff .= $this->colors->getColoredString(' ' .
66+
implode(
67+
"\n ",
68+
$this->diff->getArrayRange($this->diff->getVersion1(), $i1, $i2)
69+
) . "\n", 'grey');
70+
continue;
71+
}
72+
if ($tag == 'replace' || $tag == 'delete') {
73+
$diff .= $this->colors->getColoredString('-' .
74+
implode(
75+
"\n- ",
76+
$this->diff->getArrayRange($this->diff->getVersion1(), $i1, $i2)
77+
) . "\n", 'red');
78+
}
79+
if ($tag == 'replace' || $tag == 'insert') {
80+
$diff .= $this->colors->getColoredString('+' .
81+
implode(
82+
"\n+",
83+
$this->diff->getArrayRange($this->diff->getVersion2(), $j1, $j2)
84+
) . "\n", 'green');
85+
}
86+
}
87+
}
88+
return $diff;
89+
}
90+
}

0 commit comments

Comments
 (0)