仿对比软件的对比功能

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

require "iuplua"
require "iupluacontrols"

--iup.Message("abc", "def")

--[[
fileDlg = iup.filedlg{dialogtype = "save", title = "file save", filter = "*.bmp", filerinfo = "Bitmap files",
	directory = "C:\\Windows"}

fileDlg:popup(iup.anywhere, iup.anywhere)
--]]

--fl = io.open("E:\\Program\\Lua\\Scrawl\\cander.lua")

--for line in fl:lines() do print(line) end

local isOnlyShowDiff = false

local tabTxtFileLeft, tabTxtFileRight = {}, {}

function openFile(filePath)
	local tab = {} --clear
	local flh = io.open(filePath)
	for line in flh:lines() do 
		tab[#tab + 1] = {}
		tab[#tab].str = line 
	end
	--for _, v in ipairs(tab) do print(v) end
	return tab
end

btnAll = iup.button{title = "Show All", id = 1}
btnOnly = iup.button{title = "Only Show Difference", id = 2}

--print(iup.button1)
--[[
print(type(iup))
for k, v in pairs(iup) do 
	if type(v) == "number" then
		print(k, v) 
	end
end
--]]

function changeShow(ih, but, pressed, x, y, stat)
	--print(ih.id)
	--print(but, pressed)
	if(but == iup.BUTTON1 and pressed == 0) then
		--print(ih.id)
		--isOnlyShowDiff = not isOnlyShowDiff
		if tonumber(ih.id) == 1 then
			isOnlyShowDiff = false
		else
			isOnlyShowDiff = true
		end
		print(isOnlyShowDiff)
	end
end

btnAll.button_cb, btnOnly.button_cb = changeShow, changeShow

hbox1 = iup.hbox{btnAll, btnOnly; margin = "3x3", gap = "2"}

slnLeft = iup.text{size = "300x", value = ""}
btnLeft = iup.button{title = "Browse"}
hbox2 = iup.hbox{slnLeft, btnLeft; margin = "3x3", gap = "2"}

mlnLeft = iup.multiline{size = "450x400", value = "", 
	readonly = "yes", formatting = "yes"}

--[[
tags = iup.user{bulk = "Yes", cleanout = "Yes"}
iup.Append(tags, iup.user{selection = "1,1:1,10", fgcolor = "255 0 0"})
iup.Append(tags, iup.user{selection = "2,1:2,10", fgcolor = "0 255 0"})
iup.Append(tags, iup.user{selection = "3,1:3,10", bgcolor = "100 255 255"})
--mlnLeft.addformattag = tags

function mlnLeft.caret_cb(ih, lin, col, pos)
	str = lin..",1:"..lin..",100"
	tags2 = iup.user{bulk = "yes", cleanout = "Yes"}
	iup.Append(tags2, iup.user{selection = str, bgcolor = "255 0 255"})
	mlnLeft.addformattag = tags2
	print(lin, col, pos, str)
end
--]]

vbox1 = iup.vbox{hbox2, mlnLeft}

slnRight = iup.text{size = "300x", value = ""}
btnRight = iup.button{title = "Browse"}
hbox3 = iup.hbox{slnRight, btnRight; margin = "3x3", gap = "2"}

flDlg = iup.filedlg{dialogtype = "OPEN", title = "Open File", filter = "*.*", directory = "D:\\"}

--[[
xxa = "abc"
print("abc" == "abc")
print(xxa == "abc")
xxb = "abc"
print(xxa == xxb)
--]]
--like max-len common sub-squeue problem
function markLabel(p, i, j, tab1, tab2)
	if i == 0 or j == 0 then 
		for k = 1, i do tab1[k].label = 0 end
		for k = 1, j do tab2[k].label = 0 end
		return 
	end
	local a
	local b
	if j == 1 then a = 0 else a = p[i][j - 1] end
	if i == 1 then b = 0 else b = p[i - 1][j] end
	if p[i][j] == a then
		tab2[j].label = 0
		return markLabel(p, i, j - 1, tab1, tab2)
	elseif p[i][j] == b then
		tab1[i].label = 0
		return markLabel(p, i - 1, j, tab1, tab2)
	else
		tab1[i].label, tab2[j].label = 1, 1
		return markLabel(p, i - 1, j - 1, tab1, tab2)
	end
end

function computeDiff(tab1, tab2)
	local p = {}
	local n, m = #tab1, #tab2
	for i = 1, n do p[i] = {} end
	
	for i = 1, n do
		for j = 1, m do
			local a
			local b
			if j == 1 then a = 0 else a = p[i][j - 1] end
			if i == 1 then b = 0 else b = p[i - 1][j] end
			if tab1[i].str == tab2[j].str then
				p[i][j] = math.max(a, b) + 1
			else
				p[i][j] = math.max(a, b)
			end
		end
	end
	markLabel(p, n, m, tab1, tab2)

--[[
	for i = 1, n do
		print(i, tab1[i].label, tab2[i].label)
	end
--]]
end

mlnRight = iup.multiline{size = "450x400", readonly = "yes", formatting = "yes"}
vbox2 = iup.vbox{hbox3, mlnRight}

hbox4 = iup.hbox{vbox1, vbox2}

vbox3 = iup.vbox{hbox1, hbox4}

function showResult()
	local tagsLeft = iup.user{bulk = "Yes", cleanout = "Yes"}
	local tagsRight = iup.user{bulk = "Yes", cleanout = "Yes"}

	local strLeft = ""
	local strRight = ""
	for i = 1, #tabTxtFileLeft do
		if isOnlyShowDiff then
			strLeft = strLeft .. "\n"
		else
			strLeft = strLeft .. tabTxtFileLeft[i].str .. "\n"
		end

		if tabTxtFileLeft[i].label == 0 then
			iup.Append(tagsLeft, iup.user{selection = i .. ",1:" .. i .. ",500", fgcolor = "200 0 0"})
		else
			iup.Append(tagsLeft, iup.user{selection = i .. ",1:" .. i .. ",500", fgcolor = "0 200 0"})
		end
	end

	for i = 1, #tabTxtFileRight do
		if isOnlyShowDiff then
			strRight = strRight .. "\n"
		else
			strRight = strRight .. tabTxtFileRight[i].str .. "\n"
		end

		if tabTxtFileRight[i].label == 0 then
			iup.Append(tagsRight, iup.user{selection = i .. ",1:" .. i .. ",500", fgcolor = "0 0 200"})
		else
			iup.Append(tagsRight, iup.user{selection = i .. ",1:" .. i .. ",500", fgcolor = "0 200 0"})
		end
	end

	print(strLeft, strRight)

	mlnLeft.value, mlnRight.value = strLeft, strRight
	mlnLeft.addformattag = tagsLeft
	mlnRight.addformattag = tagsRight
end

function selectFile(ih, but, pressed, x, y, stat)
	if but == iup.BUTTON1 and pressed == 0 then
		flDlg:popup(iup.ANYWHERE, iup.ANYWHERE)
		--print(flDlg.value)
		if ih == btnLeft and flDlg.value ~= nil then
			slnLeft.value = flDlg.value
			tabTxtFileLeft = openFile(flDlg.value)
			--print(#tabTxtFileLeft)
			--for _, v in ipairs(tabTxtFileLeft) do print(v.str) end
			--print(flDlg.value)
		elseif ih == btnRight and flDlg.value ~= nil then
			slnRight.value = flDlg.value
			tabTxtFileRight = openFile(flDlg.value)
		end

		if slnLeft.value ~= nil and slnLeft.value ~= "" and slnRight.value ~= nil and slnRight.value ~= "" then
			--print(slnLeft.value, slnRight.value)
			--print("asdgasg", slnRight.value)
			computeDiff(tabTxtFileLeft, tabTxtFileRight)
			showResult()
			iup.Flush()
		end
	end
end

btnLeft.button_cb, btnRight.button_cb = selectFile, selectFile

dlg = iup.dialog{vbox3; title = "Contrast Soft"}
dlg:show()

iup.MainLoop()