ZeroBranePackage icon indicating copy to clipboard operation
ZeroBranePackage copied to clipboard

Compare Plugin

Open jjvbsag opened this issue 6 years ago • 1 comments

Dear @pkulchenko by our own demand we created a small compare plugin, which I'd like to share with you. It add's a Compare to the context menu in the filetree, if TWO items are selected, and on activation it calls the configured diff tool. See the comment in the script, how to configure.

Please make a review. If you like it, please feel free to share it at https://github.com/pkulchenko/ZeroBranePackage

------------------------------------------------------------
-- To be configured in user.lua:
------------------------------------------------------------
-- for linux use
-- ide.config.diff_cmd="meld"
-- or for windows use (due difficulties in windows with quotes in os.execute)
-- ide.config.diff_cmd=[[start "DIFF" "C:\Program Files\ExamDiff Pro\ExamDiff.exe"]]
------------------------------------------------------------

local COMPARE_PLUGIN =
{
	name = "Compare plugin",
	description = "Plugin to compare two files",
	author = "J.Jørgen von Bargen",
}


local ID_COMPARE = ID("COMPARE_PLUGIN.compare")

local DIRSEP=package.config:match("^(%S+)%s") or "/"

------------------------------------------------------------
-- get items from the tree
-- return {list-of-selected}
------------------------------------------------------------
local function GetSelectedTreeItems()
	local tree=ide:GetProjectTree()
	local function GetText(itemId)
		if itemId:IsOk() then
			local parentItemId=tree:GetItemParent(itemId)
			if parentItemId:IsOk() then
				local parentText=GetText(parentItemId)
				return parentText..DIRSEP..tree:GetItemText(itemId)
			end
			return tree:GetItemText(itemId)
		end
	end
	local selected={}
	if tree:HasFlag(wx.wxTR_MULTIPLE) then
		for i,treeItemId in ipairs(tree:GetSelections()) do
			selected[#selected+1]=GetText(treeItemId)
		end
	else
		selected[#selected+1]=GetText(tree:GetSelection())
	end
	return selected
end


local function onCompare(event)
	local diff_cmd = ide.config.diff_cmd or "meld"
	local selected=GetSelectedTreeItems()
	if #selected==2 then
		local cmd=('%s "%s" "%s"'):format(diff_cmd,unpack(selected))
		DisplayOutputLn(cmd)
		os.execute(cmd.." &")
	end
end

function COMPARE_PLUGIN:onRegister()
	local tree=ide:GetProjectTree()
	tree:Connect(ID_COMPARE,wx.wxEVT_COMMAND_MENU_SELECTED,onCompare)
end

function COMPARE_PLUGIN:onUnRegister()
	local tree=ide:GetProjectTree()
	tree:Disconnect(ID_COMPARE)
end

function COMPARE_PLUGIN:onMenuFiletree(menu,tree,event)
	local selected=GetSelectedTreeItems()
	if #selected==2 then
		menu:Append(ID_COMPARE,TR("Compare"))
	end
end

return COMPARE_PLUGIN

jjvbsag avatar Sep 07 '18 13:09 jjvbsag

Sure!!

Thanks

On Sep 7, 2018, at 8:37 AM, jjvbsag [email protected] wrote:

Dear Paul, by our own demand we created a small compare plugin, which I'd like to share with you. It add's a Compare to the context menu in the filetree, if TWO items are selected, and on activation it calls the configured diff tool. See the comment in the script, how to configure.

Please make a review. If you like it, please feel free to share it at https://github.com/pkulchenko/ZeroBranePackage https://github.com/pkulchenko/ZeroBranePackage

-- To be configured in user.lua:

-- for linux use -- ide.config.diff_cmd="meld" -- or for windows use (due difficulties in windows with quotes in os.execute) -- ide.config.diff_cmd=[[start "DIFF" "C:\Program Files\ExamDiff Pro\ExamDiff.exe"]]

local COMPARE_PLUGIN = { name = "Compare plugin", description = "Plugin to compare two files", author = "J.Jørgen von Bargen", }

local ID_COMPARE = ID("COMPARE_PLUGIN.compare")

local DIRSEP=package.config:match("^(%S+)%s") or "/"


-- get items from the tree -- return {list-of-selected}

local function GetSelectedTreeItems() local tree=ide:GetProjectTree() local function GetText(itemId) if itemId:IsOk() then local parentItemId=tree:GetItemParent(itemId) if parentItemId:IsOk() then local parentText=GetText(parentItemId) return parentText..DIRSEP..tree:GetItemText(itemId) end return tree:GetItemText(itemId) end end local selected={} if tree:HasFlag(wx.wxTR_MULTIPLE) then for i,treeItemId in ipairs(tree:GetSelections()) do selected[#selected+1]=GetText(treeItemId) end else selected[#selected+1]=GetText(tree:GetSelection()) end return selected end

local function onCompare(event) local diff_cmd = ide.config.diff_cmd or "meld" local selected=GetSelectedTreeItems() if #selected==2 then local cmd=('%s "%s" "%s"'):format(diff_cmd,unpack(selected)) DisplayOutputLn(cmd) os.execute(cmd.." &") end end

function COMPARE_PLUGIN:onRegister() local tree=ide:GetProjectTree() tree:Connect(ID_COMPARE,wx.wxEVT_COMMAND_MENU_SELECTED,onCompare) end

function COMPARE_PLUGIN:onUnRegister() local tree=ide:GetProjectTree() tree:Disconnect(ID_COMPARE) end

function COMPARE_PLUGIN:onMenuFiletree(menu,tree,event) local selected=GetSelectedTreeItems() if #selected==2 then menu:Append(ID_COMPARE,TR("Compare")) end end

return COMPARE_PLUGIN — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/pkulchenko/ZeroBranePackage/issues/74, or mute the thread https://github.com/notifications/unsubscribe-auth/Al9OwUpILebaHSeg2_F6GSZcYFviT_k3ks5uYnaCgaJpZM4We119.

cerebralweb avatar Sep 12 '18 21:09 cerebralweb