Skip to content

Commit ff6803f

Browse files
committed
Initial implementation of the hunk header.
1 parent e89bc36 commit ff6803f

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

demo/xtra/DIFF.LUA

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
11
local function diff_u(fn1,fn2)
22
local function open(fn) local f, e = io.open(fn) if not f then print(e) os.exit(1) end return f end
33
local f1, f2, pre, add, del, post, x, y, h, l1, l2 = open(fn1), open(fn2), {}, {}, {}, 0, 0, 0
4+
local buf = {add = 0, del = 0, line = {}}
45
local function flush_pre()
56
if not h then print("--- " .. fn1 .. "\n+++ " .. fn2) h = true end
6-
for _, v in ipairs(pre) do print(" " .. v) end
7-
pre = {}
7+
for _, v in ipairs(pre) do table.insert(buf.line, " " .. v) end
8+
buf.ls, buf.rs, pre = x, y, {}
89
end
9-
local function flush_hunks()
10-
for _, v in ipairs(del) do print("-" .. v) end
11-
for _, v in ipairs(add) do print("+" .. v) end
12-
del, add = {}, {}
10+
local function flush_hunk()
11+
for _, v in ipairs(del) do table.insert(buf.line, "-" .. v) end
12+
for _, v in ipairs(add) do table.insert(buf.line, "+" .. v) end
13+
buf.add, buf.del = buf.add + #add, buf.del + #del del, add = {}, {}
14+
end
15+
local function flush_buf()
16+
if #buf.line then
17+
local header = "@@ -" .. (buf.ls or "0") .. (buf.del == 1 and "" or "," .. buf.del) ..
18+
" +" .. (buf.rs or "0") .. (buf.add == 1 and "" or "," .. buf.add) .. " @@"
19+
table.insert(buf.line, 1, header)
20+
for _, v in ipairs(buf.line) do print(v) end
21+
end
22+
buf.line, buf.add, buf.del, buf.ls, buf.rs = {}, 0, 0, nil, nil
1323
end
1424
local function diagonal()
15-
flush_hunks()
25+
flush_hunk()
1626
if #pre >= 3 then table.remove(pre, 1) end
17-
if #pre <= 0 and post > 0 then print(" " .. l2) post = post - 1 else table.insert(pre, l2) end
27+
if #pre <= 0 and post > 0 then
28+
table.insert(buf.line, " " .. l2) post = post - 1
29+
if post == 0 then flush_buf() end
30+
else
31+
table.insert(pre, l2)
32+
end
1833
x, y, l1, l2 = x + 1, y + 1, f1:read("*l"), f2:read("*l")
1934
end
2035
local function right()
@@ -36,7 +51,7 @@ local function diff_u(fn1,fn2)
3651

3752
until not l1 and not l2
3853
f1:close() f2:close()
39-
if #add > 0 or #del > 0 then flush_pre() flush_hunks() end
54+
if #add > 0 or #del > 0 then flush_pre() flush_hunk() flush_buf() end
4055
end
4156

4257
if #arg < 2 then return end

0 commit comments

Comments
 (0)