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

Python API? #68

Closed
jakubka opened this issue Sep 14, 2017 · 2 comments
Closed

Python API? #68

jakubka opened this issue Sep 14, 2017 · 2 comments

Comments

@jakubka
Copy link

jakubka commented Sep 14, 2017

Hi,

Apologies if this was asked already.. couldn't find the answer.

Is it possible to use cdiff programmatically from the Python script?

Something like this:

import cdiff

diff_string = get_diff_somehow()

cdiff.print_colorized(diff_string)

This would be super useful for the utility we are writing.

Thanks!

@ymattw
Copy link
Owner

ymattw commented Sep 15, 2017

DiffParser and DiffMarker might be what you want, I imagine you will have the same logic as [1], sorry I don't have enough docstring in the file.

[1] https://github.com/ymattw/cdiff/blob/a1a33a52/cdiff.py#L642-L647

@jakubka
Copy link
Author

jakubka commented Sep 18, 2017

It works well, thanks @ymattw!

Here is the basic usage if anyone's interested:

import sys
from io import StringIO

from cdiff import (
  PatchStream,
  DiffParser,
  DiffMarker,
)

diff_str = """\
diff --git a/file.txt b/file.txt
index ada066f..a252b8c 100644
--- a/file.txt
+++ b/file.txt
@@ -1,4 +1,4 @@
 line 1
-line 2
-line 3
+this is a new line
+line change 3

"""

raw_stream = StringIO(diff_str) # open('file.diff', 'r')

stream = PatchStream(raw_stream)
diffs = DiffParser(stream).get_diff_generator()
marker = DiffMarker()
color_diff = marker.markup(diffs)

for line in color_diff:
    sys.stdout.write(line)

@jakubka jakubka closed this as completed Sep 18, 2017
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

2 participants