Fixed some questionable newline printing behavior and added support for
passing in arbitrary tables instead of strings.
This commit is contained in:
parent
0da2b29e45
commit
dc84e1e107
15
diff.lua
15
diff.lua
@ -63,7 +63,7 @@ local function print_diff(d, options)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
if not (options and options.context == 0) then
|
if not (options and options.context == 0) then
|
||||||
io.write((old:gsub("[^\n]*\n?", " %1")))
|
io.write((old:gsub("([^\n]*)\n?", " %1\n")))
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
-- Changed
|
-- Changed
|
||||||
@ -72,18 +72,17 @@ local function print_diff(d, options)
|
|||||||
print(colors.underscore..colors.bright..colors.red..
|
print(colors.underscore..colors.bright..colors.red..
|
||||||
("Old line %d-%d:"):format(chunk.old_line, chunk.old_line_end)..colors.reset)
|
("Old line %d-%d:"):format(chunk.old_line, chunk.old_line_end)..colors.reset)
|
||||||
end
|
end
|
||||||
io.write(colors.red..(chunk.old:gsub("[^\n]*\n?", "- %1"))..colors.reset)
|
io.write(colors.red..(chunk.old:gsub("([^\n]*)\n?", "- %1\n"))..colors.reset)
|
||||||
end
|
end
|
||||||
if #chunk.new > 0 then
|
if #chunk.new > 0 then
|
||||||
if line_numbers then
|
if line_numbers then
|
||||||
print(colors.underscore..colors.bright..colors.green..
|
print(colors.underscore..colors.bright..colors.green..
|
||||||
("New line %d-%d:"):format(chunk.new_line, chunk.new_line_end)..colors.reset)
|
("New line %d-%d:"):format(chunk.new_line, chunk.new_line_end)..colors.reset)
|
||||||
end
|
end
|
||||||
io.write(colors.green..(chunk.new:gsub("[^\n]*\n?", "+ %1"))..colors.reset)
|
io.write(colors.green..(chunk.new:gsub("([^\n]*)\n?", "+ %1\n"))..colors.reset)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
io.write('\n')
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local diff_mt = {__index={print=print_diff}}
|
local diff_mt = {__index={print=print_diff}}
|
||||||
@ -95,9 +94,15 @@ local function diff(old, new)
|
|||||||
local insert, concat = table.insert, table.concat
|
local insert, concat = table.insert, table.concat
|
||||||
|
|
||||||
-- Split into lines
|
-- Split into lines
|
||||||
local A, B = {}, {}
|
local A, B = old, new
|
||||||
|
if type(A) ~= 'table' then
|
||||||
|
A = {}
|
||||||
for c in old:gmatch("[^\n]*\n?") do insert(A, c) end
|
for c in old:gmatch("[^\n]*\n?") do insert(A, c) end
|
||||||
|
end
|
||||||
|
if type(B) ~= 'table' then
|
||||||
|
B = {}
|
||||||
for c in new:gmatch("[^\n]*\n?") do insert(B, c) end
|
for c in new:gmatch("[^\n]*\n?") do insert(B, c) end
|
||||||
|
end
|
||||||
|
|
||||||
-- Find the longest common subsequence between A[a_min..a_max] and B[b_min..b_max] (inclusive),
|
-- Find the longest common subsequence between A[a_min..a_max] and B[b_min..b_max] (inclusive),
|
||||||
-- and return (the starting position in a), (the starting position in b), (the length)
|
-- and return (the starting position in a), (the starting position in b), (the length)
|
||||||
|
Loading…
Reference in New Issue
Block a user