Skip to content

Commit f1cd5c4

Browse files
committed
Squish DIFF.LUA to fit in 5 clusters.
1 parent d9ebe9d commit f1cd5c4

File tree

2 files changed

+51
-117
lines changed

2 files changed

+51
-117
lines changed

demo/xtra/DIFF.LUA

Lines changed: 40 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,43 @@
1-
#!/usr/bin/env lua
1+
#!/usr/bin/env lua
22

3-
CL = tonumber(os.getenv("DIFF_CONTEXT")) or 3
4-
local function diff_u(fn1, fn2)
5-
local function open(fn) local f, e = io.open(fn) if not f then print(e) os.exit(1) end return f end
6-
local f1, f2, pre, add, del, p1, p2, x, y, h, l1, l2 = open(fn1), open(fn2), {}, {}, {}, 0, 0, 0, 0
7-
local buf = {add = 0, del = 0, line = {}}
8-
local function flush_pre()
9-
if not h then print("--- " .. fn1 .. "\n+++ " .. fn2) h = true end
10-
if #pre > 0 then
11-
for _, v in ipairs(pre) do table.insert(buf.line, " " .. v) end
12-
buf.ls, buf.rs = x - #pre, y - #pre
13-
end
14-
pre = {}
15-
end
16-
local function flush_hunk()
17-
local pol = {}
18-
local function optimize()
19-
local i, j = 1
20-
repeat
21-
j = 1
22-
repeat
23-
if del[i] == add[j] then
24-
table.insert(pol, table.remove(del, i)) table.remove(add, j)
25-
break
26-
end
27-
j = j + 1
28-
until j > #add
29-
i = i + 1
30-
until i > #del
31-
end
32-
33-
if #del > 0 and #add > 0 then optimize() end
34-
for _, v in ipairs(del) do table.insert(buf.line, "-" .. v) end
35-
for _, v in ipairs(add) do table.insert(buf.line, "+" .. v) end
36-
for _, v in ipairs(pol) do table.insert(buf.line, " " .. v) end
37-
buf.add, buf.del = buf.add + #add, buf.del + #del del, add = {}, {}
38-
end
39-
local function flush_buf()
40-
if #buf.line > 0 then
41-
buf.del, buf.add = #buf.line - buf.add, #buf.line - buf.del
42-
local header = "@@ -" .. (buf.ls or buf.del > 0 and "1" or "0") ..
43-
(buf.del == 1 and "" or "," .. buf.del) ..
44-
" +" .. (buf.rs or buf.add > 0 and "1" or "0") ..
45-
(buf.add == 1 and "" or "," .. buf.add) .. " @@"
46-
47-
table.insert(buf.line, 1, header)
48-
for _, v in ipairs(buf.line) do print(v) end
49-
end
50-
buf.line, buf.add, buf.del, buf.ls, buf.rs = {}, 0, 0, nil, nil
51-
end
52-
local function diagonal()
53-
flush_hunk()
54-
if #pre > CL then table.remove(pre, 1) end
55-
if p1 > 0 or p2 > 0 then
56-
if p1 > 0 and p2 > 0 then
57-
if p1 > CL or p2 > CL then table.insert(buf.line, " " .. l2) end
58-
p1, p2 = p1 - 1, p2 - 1
59-
elseif p1 > 0 then
60-
if p1 > CL then table.insert(buf.line, " " .. l1) end
61-
p1 = p1 - 1
62-
elseif p2 > 0 then
63-
if p2 > CL then table.insert(buf.line, " " .. l2) end
64-
p2 = p2 - 1
65-
elseif p2 <= CL then table.insert(pre, l2)
66-
elseif p1 <= CL then table.insert(pre, l1) end
67-
68-
if p1 == 0 or p2 == 0 then flush_buf() table.insert(pre, l2) end
69-
else
70-
table.insert(pre, l2)
71-
end
72-
x, y, l1, l2 = x + 1, y + 1, f1:read("*l"), f2:read("*l")
73-
end
74-
local function right()
75-
flush_pre() p1 = (CL * 2) + 1
76-
table.insert(del, l1)
77-
x, l1 = x + 1, f1:read("*l")
78-
end
79-
local function down()
80-
flush_pre() p2 = (CL * 2) + 1
81-
table.insert(add, l2)
82-
y, l2 = y + 1, f2:read("*l")
83-
end
84-
repeat
85-
if l1 == l2 then diagonal()
86-
elseif not l2 then right()
87-
elseif not l1 then down()
88-
else right() down() end
89-
until not l1 and not l2
90-
91-
f1:close() f2:close()
92-
if #add > 0 or #del > 0 then flush_pre() flush_hunk() end
93-
if #buf.line > 0 then flush_buf() end
94-
end
95-
96-
if #arg < 2 or #arg % 2 ~= 0 then
97-
print(arg[-1] .. " " .. arg[0] .. [[ old new ...
3+
CL=tonumber(os.getenv("DIFF_CONTEXT"))or 3
4+
local function diff_u(fn1,fn2)
5+
local function open(fn)local f,e=io.open(fn)if not f then print(e)os.exit(1)end return f end
6+
local f1,f2,p,a,d,p1,p2,x,y,B,h,l1,l2=open(fn1),open(fn2),{},{},{},0,0,0,0,{a=0,d=0,l={}}
7+
local function FP()
8+
if not h then print("--- "..fn1.."\n+++ "..fn2)h=true end
9+
if#p>0 then for _,v in ipairs(p)do table.insert(B.l," "..v)end B.o,B.n=x-#p,y-#p end
10+
p={}end
11+
local function FH()
12+
local pol={}
13+
local function optimize()local i,j=1 repeat j=1 repeat if d[i]== a[j]then table.insert(pol,table.remove(d,i))table.remove(a,j)break end j=j+1 until j>#a i=i+1 until i>#d end
14+
if#d>0 and#a>0 then optimize()end
15+
for _,v in ipairs(d)do table.insert(B.l,"-"..v)end
16+
for _,v in ipairs(a)do table.insert(B.l,"+"..v)end
17+
for _,v in ipairs(pol)do table.insert(B.l," "..v)end
18+
B.a,B.d=B.a+#a,B.d+#d
19+
d,a={},{}end
20+
local function FB()
21+
if#B.l>0 then B.d,B.a=#B.l-B.a,#B.l-B.d
22+
local he="@@-"..(B.o or B.d>0 and "1" or "0")..(B.d==1 and "" or ","..B.d).."+"..(B.n or B.a>0 and "1" or "0")..(B.a==1 and "" or ","..B.a).." @@"
23+
table.insert(B.l,1,he)for _,v in ipairs(B.l)do print(v)end end
24+
B.l,B.a,B.d,B.o,B.n={},0,0,nil,nil end
25+
local function di()
26+
FH()if#p>CL then table.remove(p,1)end
27+
if p1>0 or p2>0 then
28+
if p1>0 and p2>0 then if p1>CL or p2>CL then table.insert(B.l," "..l2)end p1,p2=p1-1,p2-1
29+
elseif p1>0 then if p1>CL then table.insert(B.l," "..l1)end p1=p1-1
30+
elseif p2>0 then if p2>CL then table.insert(B.l," "..l2)end p2=p2-1
31+
elseif p2<=CL then table.insert(p,l2)
32+
elseif p1<=CL then table.insert(p,l1)end
33+
if p1==0 or p2==0 then FB()table.insert(p,l2)end
34+
else table.insert(p,l2)end x,y,l1,l2=x+1,y+1,f1:read("*l"),f2:read("*l")end
35+
local function ri()FP()p1=(CL*2)+1 table.insert(d,l1)x,l1=x+1,f1:read("*l")end
36+
local function dn()FP()p2=(CL*2)+1 table.insert(a,l2)y,l2=y+1,f2:read("*l")end
37+
repeat if l1==l2 then di()elseif not l2 then ri()elseif not l1 then dn()else ri()dn()end until not l1 and not l2 f1:close()f2:close()if#a>0 or#d>0 then FP()FH()end if#B.l>0 then FB()end end
38+
if#arg<2 or#arg%2~=0 then print(arg[-1].." "..arg[0]..[[ old new...
9839
9940
Compare differences between old and new files and print them as unified context.
100-
To compare multiple sets of files at a time, write as sequential parameters.
101-
Example: ']] .. arg[0] .. [[ old1 new1 old2 new2 old3 new3' etc...]])
102-
os.exit(1)
103-
end
104-
for i = 1, #arg, 2 do
105-
diff_u(arg[i], arg[i + 1])
106-
end
41+
To compare multiple sets of files at a time,write as sequential parameters.
42+
Example: ']]..arg[0]..[[ old1 new1 old2 new2 old3 new3' etc...]])os.exit(1)end
43+
for i=1,#arg,2 do diff_u(arg[i],arg[i+1])end

demo/xtra/DIFF.md

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,20 @@ The script performs the following steps:
5959
e.g., `@@ -start,count +start,count @@`.
6060
- If the old and new files are identical, nothing will be printed
6161

62-
## Code Breakdown
63-
Here’s an explanation of core components:
64-
65-
### Global Variable
62+
## Global Variables
6663

6764
| Variable | Description |
6865
|----------|------------------------------------------------------------------------------------------------------------------------------------------------|
6966
| `CL` | **C**ontext **L**ines to print above and below a hunk. Defaults to `3`, can be to something else with the `DIFF_CONTEXT` environment variable. |
7067

71-
### Functions
68+
## Functions
7269

73-
| Function | Parameters | Description |
74-
|--------------|-------------------------|---------------------------------------------------------------------|
75-
| `diff_u` | filename1<br/>filename2 | Prints differences between the two files in to stdout. |
76-
| `flush_pre` | | Flushes pre-context lines into the buffer when changes are detected |
77-
| `flush_hunk` | | Outputs a complete hunk of changes into the buffer |
78-
| `flush_buf` | | Print and clear the buffer |
79-
| `diagonal` | | Iterate over lines that match in both files |
80-
| `right` | | Iterate over a line only present in the old file |
81-
| `down` | | Iterate over a line only present in the new file |
70+
| Function | Parameters | Description |
71+
|----------|-------------------------------------------------|-----------------------------------------------------------------------------|
72+
| `diff_u` | **f**ile**n**ame**1**<br/>**f**ile**n**ame**2** | Prints differences between the two files in to stdout. |
73+
| `fp` | | **F**lushes **p**re-context lines into the buffer when changes are detected |
74+
| `fh` | | **F**lushes a complete **h**unk of changes into the buffer |
75+
| `fb` | | **F**lush (print) and clear the **b**uffer |
76+
| `di` | | **D**iagonally **i**terate (lines that match in both files) |
77+
| `ri` | | **Ri**ght iterate (for a line only present in the old file) |
78+
| `dn` | | **D**ow**n** Iterate (for a line only present in the new file) |

0 commit comments

Comments
 (0)