Skip to content

Commit

Permalink
Fixes #64 - maxLineMarkerWidth only calculated for input format plain.
Browse files Browse the repository at this point in the history
- Calculation of maxLineMarkerWidth independent of input format.
- Second parameter of string repeat function minimizes to 0.
- Code cleanup.
  • Loading branch information
DigiLive authored and DigiLive committed Oct 13, 2020
1 parent 836b128 commit c5f6d72
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 43 deletions.
43 changes: 20 additions & 23 deletions lib/jblond/Diff/Renderer/MainRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class MainRenderer extends MainRendererAbstract
*
* This method is called by the renderers which extends this class.
*
* @param array $changes Contains the op-codes about the differences between version1 and version2.
* @param object $subRenderer Renderer which is subClass of this class.
* @param array $changes Contains the op-codes about the differences between version1 and version2.
* @param object $subRenderer Renderer which is subClass of this class.
*
* @return string|false String representation of the differences or false when versions are identical.
*/
Expand All @@ -56,15 +56,12 @@ public function renderOutput(array $changes, object $subRenderer)
$output .= $subRenderer->generateSkippedLines();
}

if ($this->options['format'] == 'plain') {
$this->maxLineMarkerWidth =
max(
strlen($this->options['insertMarkers'][0]),
strlen($this->options['deleteMarkers'][0]),
strlen($this->options['equalityMarkers'][0]),
strlen($this->options['equalityMarkers'][1])
);
}
$this->maxLineMarkerWidth = max(
strlen($this->options['insertMarkers'][0]),
strlen($this->options['deleteMarkers'][0]),
strlen($this->options['equalityMarkers'][0]),
strlen($this->options['equalityMarkers'][1])
);

foreach ($blocks as $change) {
$output .= $subRenderer->generateBlockHeader($change);
Expand Down Expand Up @@ -184,11 +181,11 @@ protected function renderSequences(): array
* New => "ab123fg" End marker inserted at position 6
* </pre>
*
* @param array $oldText Collection of lines of old text.
* @param array $newText Collection of lines of new text.
* @param int $startOld First line of the block in old to replace.
* @param int $endOld last line of the block in old to replace.
* @param int $startNew First line of the block in new to replace.
* @param array $oldText Collection of lines of old text.
* @param array $newText Collection of lines of new text.
* @param int $startOld First line of the block in old to replace.
* @param int $endOld last line of the block in old to replace.
* @param int $startNew First line of the block in new to replace.
*/
private function markInlineChange(array &$oldText, array &$newText, int $startOld, int $endOld, int $startNew)
{
Expand Down Expand Up @@ -231,8 +228,8 @@ private function markInlineChange(array &$oldText, array &$newText, int $startOl
* The second element defines the last (starting at -0) character from the end of the old string which is different.
*
*
* @param string $oldString The first string to compare.
* @param string $newString The second string to compare.
* @param string $oldString The first string to compare.
* @param string $newString The second string to compare.
*
* @return array Array containing the starting position (0 by default) and the ending position (-1 by default)
*/
Expand Down Expand Up @@ -270,10 +267,10 @@ private function getInlineChange(string $oldString, string $newString): array
*
* The index of the last element of the array is always returned.
*
* @param array $blocks The array which keeps the changes for the HTML renderer.
* @param string $tag Kind of difference.
* @param integer $lineInOld Start of block in "old".
* @param integer $lineInNew Start of block in "new".
* @param array $blocks The array which keeps the changes for the HTML renderer.
* @param string $tag Kind of difference.
* @param integer $lineInOld Start of block in "old".
* @param integer $lineInNew Start of block in "new".
*
* @return int The index of the last element.
*/
Expand Down Expand Up @@ -306,7 +303,7 @@ private function appendChangesArray(array &$blocks, string $tag, int $lineInOld,
* This involves replacing tab characters with spaces, making the HTML safe for output by ensuring that double
* spaces are replaced with &nbsp; etc.
*
* @param array $strings Array of strings to format.
* @param array $strings Array of strings to format.
*
* @return array Array of formatted strings.
*/
Expand Down
53 changes: 33 additions & 20 deletions lib/jblond/Diff/Renderer/Text/InlineCli.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
*
* PHP version 7.2 or greater
*
* @package jblond\Diff\Renderer\Text
* @author Ferry Cools <[email protected]>
* @package jblond\Diff\Renderer\Text
* @author Ferry Cools <[email protected]>
* @copyright (c) 2020 Ferry Cools
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
* @version 2.2.1
* @link https://github.com/JBlond/php-diff
* @license New BSD License http://www.opensource.org/licenses/bsd-license.php
* @version 2.2.1
* @link https://github.com/JBlond/php-diff
*/
class InlineCli extends MainRenderer implements SubRendererInterface
{
Expand All @@ -31,7 +31,7 @@ class InlineCli extends MainRenderer implements SubRendererInterface
/**
* InlineCli constructor.
*
* @param array $options Custom defined options for the inline diff renderer.
* @param array $options Custom defined options for the inline diff renderer.
*
* @see Inline::$subOptions
*/
Expand Down Expand Up @@ -67,7 +67,7 @@ public function generateDiffHeader(): string
/**
* Generate a string representation of the start of a block.
*
* @param array $changes Contains the op-codes about the changes between two blocks of lines.
* @param array $changes Contains the op-codes about the changes between two blocks of lines.
*
* @return string Start of the diff view.
*/
Expand All @@ -89,14 +89,17 @@ public function generateSkippedLines(): string
/**
* Generate a string representation lines without differences between the two versions.
*
* @param array $changes Contains the op-codes about the changes between two blocks of lines.
* @param array $changes Contains the op-codes about the changes between two blocks of lines.
*
* @return string Text with no difference.
*/
public function generateLinesEqual(array $changes): string
{
$returnValue = '';
$padding = str_repeat(' ', $this->maxLineMarkerWidth - strlen($this->options['equalityMarkers'][0]));
$padding = str_repeat(
' ',
max($this->maxLineMarkerWidth - strlen($this->options['equalityMarkers'][0]), 0)
);

foreach ($changes['base']['lines'] as $line) {
$returnValue .= $this->options['equalityMarkers'][0] . $padding . '|' . $line . "\n";
Expand All @@ -108,15 +111,18 @@ public function generateLinesEqual(array $changes): string
/**
* Generate a string representation of lines that are added to the 2nd version.
*
* @param array $changes Contains the op-codes about the changes between two blocks of text.
* @param array $changes Contains the op-codes about the changes between two blocks of text.
*
* @return string Added text.
*/
public function generateLinesInsert(array $changes): string
{
$colorize = new CliColors();
$returnValue = '';
$padding = str_repeat(' ', $this->maxLineMarkerWidth - strlen($this->options['insertMarkers'][0]));
$padding = str_repeat(
' ',
max($this->maxLineMarkerWidth - strlen($this->options['insertMarkers'][0]), 0)
);

foreach ($changes['changed']['lines'] as $line) {
if ($this->options['cliColor']) {
Expand All @@ -132,15 +138,18 @@ public function generateLinesInsert(array $changes): string
/**
* Generate a string representation of lines that are removed from the 2nd version.
*
* @param array $changes Contains the op-codes about the changes between two blocks of text.
* @param array $changes Contains the op-codes about the changes between two blocks of text.
*
* @return string Removed text.
*/
public function generateLinesDelete(array $changes): string
{
$colorize = new CliColors();
$returnValue = '';
$padding = str_repeat(' ', $this->maxLineMarkerWidth - strlen($this->options['deleteMarkers'][0]));
$padding = str_repeat(
' ',
max($this->maxLineMarkerWidth - strlen($this->options['deleteMarkers'][0]), 0)
);

foreach ($changes['base']['lines'] as $line) {
if ($this->options['cliColor']) {
Expand All @@ -156,7 +165,7 @@ public function generateLinesDelete(array $changes): string
/**
* Generate a string representation of lines that are partially modified.
*
* @param array $changes Contains the op-codes about the changes between two blocks of text.
* @param array $changes Contains the op-codes about the changes between two blocks of text.
*
* @return string Modified text.
*/
Expand All @@ -179,10 +188,10 @@ public function generateLinesReplace(array $changes): string
/**
* Merge the changes between two lines together and mark these changes.
*
* @param array $baseLines Lines of version 1.
* @param array $changedLines Lines of version 2.
* @param array|null[] $deleteColors Fore- and background colors of part that is removed from the 2nd version.
* @param array|null[] $insertColors Fore- and background colors of part that is added to the 2nd version.
* @param array $baseLines Lines of version 1.
* @param array $changedLines Lines of version 2.
* @param array|null[] $deleteColors Fore- and background colors of part that is removed from the 2nd version.
* @param array|null[] $insertColors Fore- and background colors of part that is added to the 2nd version.
*
* Option $deleteColors and $insertColors only have affect when this class's cliColors option is set to true.
*
Expand All @@ -194,7 +203,11 @@ private function mergeChanges(
array $deleteColors = [null, null],
array $insertColors = [null, null]
): array {
$padding = str_repeat(' ', $this->maxLineMarkerWidth - strlen($this->options['equalityMarkers'][1]));
$padding = str_repeat(
' ',
max($this->maxLineMarkerWidth - strlen($this->options['equalityMarkers'][1]), 0)
);

if ($this->options['cliColor']) {
$colorize = new CliColors();
}
Expand Down Expand Up @@ -233,7 +246,7 @@ private function mergeChanges(
/**
* Generate a string representation of the end of a block.
*
* @param array $changes Contains the op-codes about the changes between two blocks of text.
* @param array $changes Contains the op-codes about the changes between two blocks of text.
*
* @return string End of the block
*/
Expand Down

0 comments on commit c5f6d72

Please sign in to comment.