Skip to content

Commit d5cfaeb

Browse files
committed
Group removes and added lines together.
1 parent c7b36dc commit d5cfaeb

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

demo/xtra/DIFF.LUA

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@ local function open(filename)
55
end
66

77
local function diff_u(fn1,fn2)
8-
local f1, f2, pre, post, x, y, h, l1, l2 = open(fn1), open(fn2), {}, 0, 0, 0
8+
local f1, f2, pre, add, del, post, x, y, h, l1, l2 = open(fn1), open(fn2), {}, {}, {}, 0, 0, 0
99
local function flush_pre()
1010
if not h then print("--- " .. fn1 .. "\n+++ " .. fn2) h = true end
1111
for _, v in ipairs(pre) do print(" " .. v) end
1212
pre = {}
1313
end
14+
local function flush_hunks()
15+
for _, v in ipairs(del) do print("-" .. v) end
16+
for _, v in ipairs(add) do print("+" .. v) end
17+
del, add = {}, {}
18+
end
1419
local function diagonal()
20+
flush_hunks()
1521
if #pre >= 3 then table.remove(pre, 1) end
1622
if #pre <= 0 and post > 0 then
1723
print(" " .. l2) post = post - 1
@@ -24,13 +30,13 @@ local function diff_u(fn1,fn2)
2430
local function right()
2531
flush_pre()
2632
post = 3
27-
if l1 then print("-" .. l1) end
33+
table.insert(del, l1)
2834
l1 = f1:read("*l")
2935
end
3036
local function down()
3137
flush_pre()
3238
post = 3
33-
if l2 then print("+" .. l2) end
39+
table.insert(add, l2)
3440
l2 = f2:read("*l")
3541
end
3642
repeat
@@ -42,6 +48,7 @@ local function diff_u(fn1,fn2)
4248

4349
until not l1 and not l2
4450
f1:close() f2:close()
51+
if #add > 0 or #del > 0 then flush_pre() flush_hunks() end
4552
end
4653

4754
if #arg < 2 then return end

0 commit comments

Comments
 (0)