From a4440bb14c3636160cd51b57c51b28900476a4e8 Mon Sep 17 00:00:00 2001
From: Energy <71418770+energy-coresky@users.noreply.github.com>
Date: Thu, 19 Dec 2024 22:06:00 +0300
Subject: [PATCH] Continue
---
sky | 2 +-
w2/console.php | 8 +++++-
w2/display.php | 70 +++++++++++++++++++++++++++++---------------------
3 files changed, 49 insertions(+), 31 deletions(-)
diff --git a/sky b/sky
index 5518501..48aaba4 100644
--- a/sky
+++ b/sky
@@ -6,7 +6,7 @@ define('START_TS', microtime(true));
if (1 == $argc)
$argv = [0, ''];
-$d = 'd' == $argv[1];
+$d = 'dir' == $argv[1];
if ($d || 'php' == $argv[1]) { // 2do cut too long lists
require __DIR__ . '/w2/rare.php';
$dirs = Rare::walk_dirs('.');
diff --git a/w2/console.php b/w2/console.php
index 6f4e595..bb52b30 100644
--- a/w2/console.php
+++ b/w2/console.php
@@ -59,8 +59,8 @@ function __call($name, $args) {
$ext = 'Console' == $_cls ? '' : "$cls ";
$ary = $ext ? [] : [
's' => 'Run PHP web-server',
- 'd' => 'List dirs (from current dir)',
'v' => 'Show Coresky version',
+ 'dir' => 'List dirs (from current dir)',
'php' => 'Lint PHP files (from current dir)',
];
$ware = self::$d[1] ? basename(self::$d[1]) : false;
@@ -312,6 +312,12 @@ function c_m($id = 8, $unhtml = false) {
echo !$unhtml ? $s : (1 == $unhtml ? unhtml($s) : unhtml(unhtml($s)));
}
+ /** Diff text files, example: sky d oldfile newfile */
+ function c_d($fno, $fnn) {
+ [$out, $add, $sub, $z] = Display::diffx(file_get_contents($fnn), file_get_contents($fno));
+ echo '' === $out ? 'Files identical' : "@@ -$sub +$add @@ $z\n$out";
+ }
+
/** Check globals */
function c_g() {
DEV::init();
diff --git a/w2/display.php b/w2/display.php
index 56239e7..2a590d5 100644
--- a/w2/display.php
+++ b/w2/display.php
@@ -62,7 +62,6 @@ static function highlight_js($code, &$y, $u = '') { # r g d c m j - gray
} elseif ($y->tok) {
$out .= self::span($u . ('@' == $t[0] ? 'g' : 'g'), $t);
} else {
- #$out .= self::span($u . ('k' == $y->mode ? 'r' : 'd'), $t);
$out .= html($t);
}
}
@@ -170,7 +169,7 @@ static function yaml($yaml, $marker = '', $no_lines = false, $no_layout = false)
}
static function diffx(string $new, string $old, $boundary = 3, $in = false) {
- $out = $plus = '';
+ $out = '';
$add = $sub = $j = $z = $zz = $max = 0;
$ary = self::diff($new, $old, $max, $boundary);
$rest = $ary && is_int($last = $ary[0][0]) ? array_shift($ary) : false;
@@ -178,25 +177,24 @@ static function diffx(string $new, string $old, $boundary = 3, $in = false) {
$in or $in = fn($l, $n, $s, $x = '+') => sprintf("$x%{$len}s|%{$len}s %s\n", $l, $n, $s);
if ($ary || $rest) {
foreach ($ary as $v) {
- [$chr, $n, $new, $l, $old] = $v;
- if (!$x = '+' == $chr) {
+ [$chr, $str, $n, $l] = $v;
+ if ('+' == $chr) {
+ $zz or ++$z && ++$zz;
+ $add++;
+ $out .= $in($j = '', $n, $str);
+ } else {
if ($eq = '=' == $chr) {
- if ($j && ++$j != $n && !$plus)
+ if ($j && ++$j != $n)
$out .= ' =======' . ($gt ? '==' : '') . "\n";
- [$out, $plus, $j] = [$out . $plus, $zz = '', $n];
+ $j = $n;
+ $zz = '';
} else {
$zz or ++$z && ++$zz;
$sub++;
}
- $out .= $in($l, $eq ? $n : '', $old, $eq ? ' ' : '-');
- }
- if ($x || '*' == $chr) {
- $zz or ++$z && ++$zz;
- $add++;
- $plus .= $in($j = '', $n, $new);
+ $out .= $in($l, $eq ? $n : '', $str, $eq ? ' ' : '-');
}
}
- $out .= $plus;
if ($rest) {
$cnt = count($ary = $rest[1] ?: $rest[2]);
($plus = (bool)$rest[1]) ? ($add += $cnt) : ($sub += $cnt);
@@ -207,35 +205,40 @@ static function diffx(string $new, string $old, $boundary = 3, $in = false) {
return [$out, $add, $sub, "$z " . round(100 * $z / (($add + $sub) ?: 1)) . '%'];
}
- static function diff(string $new, string $old, &$mode = null, int $boundary = 0) {
+ static function diff(string $new, string $old, &$mode = null, int $boundary = 1) {
$new = explode("\n", unl($new));
- $old = explode("\n", unl($old));
- $diff = $eq = [];
- for ($rN = '', $n = $l = $z = 0; $new && $old; $rN .= $chr) { # 2do: LCS algo..
+ $old = explode("\n", unl($old)); # 2do: LCS algo..
+ $pm = fn($p, $m) => str_pad('', min($p, $m), '*') . str_pad('', abs($p - $m), $p > $m ? '+' : '.');
+ $diff = $eq = $plus = [];
+ $n = $l = $b = $p = $m = 0;
+ for ($RR = ''; $new && $old; '.' == $r or array_shift($new), '+' == $r or array_shift($old)) {
if (($sn = $new[0]) === $old[0]) {
- $ary = [$chr = '=', ++$n, $sn, ++$l, $sn];
- $z++ ? ($diff[] = $ary) : ($eq[] = $ary);
- $z < 1 or $z = 0;
+ $diff = array_merge($diff, array_splice($plus, 0));
+ $RR .= $pm($p, $m) . ($r = '=');
+ $p = $m = 0;
+ $b++ ? ($diff[] = ['=', $sn, ++$n, ++$l]) : ($eq[] = ['=', $sn, ++$n, ++$l]);
+ $b < 1 or $b = 0;
} else {
- $diff = array_merge($diff, array_slice($eq, $z = -$boundary));
+ $diff = array_merge($diff, array_slice($eq, $b = -$boundary));
$eq = [];
$x = (bool)$fl = array_keys($new, $sl = $old[0]);
if (($fn = array_keys($old, $sn)) || $x) {
if ($fn && $x) { # both found
for ($i = 1, $vn = $fn[0]; ($new[$i] ?? 0) === ($old[$vn + $i] ?? 1); $i++);
for ($j = 1, $vl = $fl[0]; ($old[$j] ?? 0) === ($new[$vl + $j] ?? 1); $j++);
- $x = $i / $vn / count($fn) < $j / $vl / count($fl); // ++$vl
+ $x = $i / $vn / count($fn) < $j / $vl / count($fl); // ++$vl div by zero
}
- $diff[] = $x ? [$chr = '+', ++$n, $sn, 0, ''] : [$chr = '.', 0, '', ++$l, $sl];
+ $x ? ($plus[] = [$r = '+', $sn, ++$n, ++$p]) : ($diff[] = [$r = '.', $sl, ++$m, ++$l]);
} else {
- $diff[] = [$chr = '*', ++$n, $sn, ++$l, $sl];
+ $plus[] = ['+', $sn, ++$n, 0];
+ $diff[] = ['.', $sl, 0, ++$l];
+ $RR .= $r = '*';
}
}
- '.' == $chr or array_shift($new);
- '+' == $chr or array_shift($old);
}
if (null === $mode)
- return $rN . str_pad('', count($new ?: $old), $new ? '+' : '.');
+ return $RR . $pm($p, $m) . str_pad('', count($new ?: $old), $new ? '+' : '.');
+ $diff = array_merge($diff, $plus);
$mode = max(++$n + count($new), ++$l + count($old));
return $new || $old ? array_merge([[$new ? $n : $l, $new, $old]], $diff) : $diff;
}
@@ -470,6 +473,8 @@ static function xdata($in) {
'cut' => $cut,
'_' => '',
'prev' => '',
+ 'invert' => false,
+ 'colors' => 0, # 0: RGY, 1: Y, 2: RG
];
}
@@ -477,12 +482,17 @@ static function highlight_line(&$val, $key, $x) {
if (-9 != $x->cut[0] && $key < $x->cut[0])
return $val = '';
$pad = $c = '';
- if (!$key) for (; $x->len > $x->disp && $x->diff[$x->disp] == '.'; $x->disp++) {
+ $pt = $x->invert ? '+' : '.';
+ if (!$key) for (; $x->len > $x->disp && $x->diff[$x->disp] == $pt; $x->disp++) {
$x->lnum .= "
";
$pad .= '