icdiff icon indicating copy to clipboard operation
icdiff copied to clipboard

APIs?

Open pwwang opened this issue 7 years ago • 5 comments

icdiff is great! But do we have any plans to expose the APIs like difflib?

pwwang avatar Apr 25 '18 20:04 pwwang

Sorry, haven't noticed this: #105

pwwang avatar Apr 25 '18 20:04 pwwang

I reopen this because I found the APIs are not so convenient to be integrated. For example, if I want to compare two files in my program, I have to modify the sys.argv to get default options:

def _getIcOptions():
	origargv = [arg for arg in sys.argv]
	options = icdiff.get_options()[0]
	sys.argv = origargv
	return options

def ndiff(a, b, linejunk=None, charjunk=difflib.IS_CHARACTER_JUNK):
	options = _getIcOptions()
	cd = icdiff.ConsoleDiff(
		cols              = int(options.cols),
		show_all_spaces   = options.show_all_spaces,
		highlight         = options.highlight,
		line_numbers      = options.line_numbers,
		tabsize           = int(options.tabsize)
	)
	with warnings.catch_warnings():
		warnings.simplefilter('ignore')
		for line in cd.make_table(
				a, b, 'First', 'Second',
				context=(not options.whole_file),
				numlines=int(options.unified)):
			line = "%s\n" % line
			yield line.encode(options.output_encoding)

pwwang avatar May 01 '18 20:05 pwwang

Can you just use the difflib api? What sort of program do you have that you want to invoke icdiff via api?

(In general, icdiff is trying to be a tool for users to invoke directly)

jeffkaufman avatar May 01 '18 20:05 jeffkaufman

@jeffkaufman I understand. But the reason (the beauty of line-by-line comparison and colorized output) why I replaced diff with icdiff is also the reason I want to replace difflib with "icdifflib". Let's say a simple scenario would be enhancing messages of unittest, which originally uses difflib.

pwwang avatar May 01 '18 23:05 pwwang

Thinking about this, it's fine with me if someone wants to make a PR to add API support.

jeffkaufman avatar May 16 '18 21:05 jeffkaufman