Skip to content

Commit

Permalink
Merge branch 'inlineCli'
Browse files Browse the repository at this point in the history
  • Loading branch information
DigiLive authored and DigiLive committed Jul 17, 2020
2 parents eda111b + 281579a commit 10986ac
Show file tree
Hide file tree
Showing 21 changed files with 1,135 additions and 452 deletions.
55 changes: 45 additions & 10 deletions example/cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@

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

// Include and instantiate autoloader.
require '../vendor/autoload.php';
// Validate the interpreter.
if (php_sapi_name() !== 'cli') {
echo 'This script demonstrates console support for the php-diff package.<br>';
echo 'Please execute it from a cli interpreter.';
throw new RuntimeException('Script for CLI use only!');
}

// jblond\cli\Cli
$cli = new Cli();

// 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');
$sampleA = file_get_contents(dirname(__FILE__) . '/a.txt');
$sampleB = file_get_contents(dirname(__FILE__) . '/b.txt');

$customOptions = [
'context' => 2,
Expand All @@ -23,18 +28,48 @@
];

// Choose one of the initializations.
$diff = new Diff($a, $b);
$diff = new Diff($sampleA, $sampleB);
//$diff = new Diff($a, $b, $customOptions); // Initialize the diff class with custom options.

// Instantiate Cli wrapper
$cli = new Cli();

// Generate a unified diff.
// \jblond\Diff\Renderer\Text
$renderer = new UnifiedCli();
echo "-= Unified Default =-\n\n";
$cli->output($diff->render($renderer));

echo "\n\n-= Unified Colored =-\n\n";

$renderer = new UnifiedCli(
// Define renderer options.
[
'cliColor' => 'simple',
]
);

$cli->output($diff->render($renderer));


// Generate an inline diff.
$renderer = new InlineCli(
// Define renderer options.
[
'deleteMarkers' => ['-', '-'],
'insertMarkers' => ['+', '+'],
'equalityMarkers' => ['=', 'x'],
]
);
echo "-= Inline Marked =-\n\n";
$cli->output($diff->render($renderer));

echo "\n\n Now Colored\n\n";
echo "-= Inline Colored =-\n\n";

$coloredRenderer = new UnifiedCli(['cliColor'=>'simple']);
$coloredRenderer = new InlineCli(
// Define renderer options.
[
'cliColor' => true,
]
);

$cli->output($diff->render($coloredRenderer));
32 changes: 12 additions & 20 deletions example/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

use jblond\Diff;
use jblond\Diff\Renderer\Html\Inline;
use jblond\Diff\Renderer\Html\Unified as HtmlUnified;
use jblond\Diff\Renderer\Html\SideBySide;
use jblond\Diff\Renderer\Html\Unified as HtmlUnified;
use jblond\Diff\Renderer\Text\Context;
use jblond\Diff\Renderer\Text\Unified;

// 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');
$sampleA = file_get_contents(dirname(__FILE__) . '/a.txt');
$sampleB = file_get_contents(dirname(__FILE__) . '/b.txt');

// Options for generating the diff.
$customOptions = [
Expand All @@ -23,7 +23,7 @@
];

// Choose one of the initializations.
$diff = new Diff($a, $b); // Initialize the diff class with default options.
$diff = new Diff($sampleA, $sampleB); // Initialize the diff class with default options.
//$diff = new Diff($a, $b, $customOptions); // Initialize the diff class with custom options.
?><!DOCTYPE html>
<html lang="en">
Expand All @@ -34,14 +34,14 @@
<script>
function changeCSS(cssFile, cssLinkIndex) {

var oldLink = document.getElementsByTagName("link").item(cssLinkIndex);
const oldLink = document.getElementsByTagName('link').item(cssLinkIndex);

var newLink = document.createElement("link");
newLink.setAttribute("rel", "stylesheet");
newLink.setAttribute("type", "text/css");
newLink.setAttribute("href", cssFile);
const newLink = document.createElement('link');
newLink.setAttribute('rel', 'stylesheet');
newLink.setAttribute('type', 'text/css');
newLink.setAttribute('href', cssFile);

document.getElementsByTagName("head").item(0).replaceChild(newLink, oldLink);
document.getElementsByTagName('head').item(0).replaceChild(newLink, oldLink);
}
</script>
</head>
Expand All @@ -52,17 +52,13 @@ function changeCSS(cssFile, cssLinkIndex) {
<a href="#" onclick="changeCSS('styles.css', 0);">Light Theme</a>
<a href="#" onclick="changeCSS('dark-theme.css', 0);">Dark Theme</a>
</aside>
<hr />
<hr>

<h2>HTML Side by Side Diff</h2>

<?php
// Generate a side by side diff.
// \jblond\Diff\Renderer\Html
$renderer = new SideBySide([
'title1' => 'Custom title for version1',
'title2' => 'Custom title for version2',
]);
$renderer = new SideBySide();
echo $diff->Render($renderer);
?>

Expand All @@ -71,31 +67,27 @@ function changeCSS(cssFile, cssLinkIndex) {
<?php

// Generate an inline diff.
// \jblond\Diff\Renderer\Html
$renderer = new Inline();
echo $diff->render($renderer);
?>

<h2>HTML Unified Diff</h2>
<?php
// Generate a unified diff.
// \jblond\Diff\Renderer\Html
$renderer = new HtmlUnified();
echo "<pre>{$diff->render($renderer)}</pre>";
?>

<h2>Text Unified Diff</h2>
<?php
// Generate a unified diff.
// \jblond\Diff\Renderer\Text
$renderer = new Unified();
echo '<pre>' . htmlspecialchars($diff->render($renderer)) . '</pre>';
?>

<h2>Text Context Diff</h2>
<?php
// Generate a context diff.
// jblond\Diff\Renderer\Text\Context
$renderer = new Context();
echo '<pre>' . htmlspecialchars($diff->render($renderer)) . '</pre>';
?>
Expand Down
Loading

0 comments on commit 10986ac

Please sign in to comment.