Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compare Plugin #74

Open
jjvbsag opened this issue Sep 7, 2018 · 1 comment
Open

Compare Plugin #74

jjvbsag opened this issue Sep 7, 2018 · 1 comment
Assignees

Comments

@jjvbsag
Copy link

jjvbsag commented Sep 7, 2018

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
@cerebralweb
Copy link

cerebralweb commented Sep 12, 2018 via email

@pkulchenko pkulchenko self-assigned this Oct 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants