Last active
December 1, 2023 09:59
-
-
Save themadsens/609033606987ea29ab6b148d2b1c7f78 to your computer and use it in GitHub Desktop.
Line by line diff that highlights changes from one line to the next. Prototype kindly authored by ChatGPT :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import difflib | |
import fileinput | |
def display_diff(diff): | |
for item in diff: | |
action, data = item[0], item[2:] | |
if action == ' ': | |
print(data, end='') | |
elif action == '-': | |
print(f"\033[91m{data}\033[0m", end='') # Use red color for deletions | |
elif action == '+': | |
print(f"\033[92m{data}\033[0m", end='') # Use green color for additions | |
prevline = None | |
for line in fileinput.input(): | |
if prevline is None: | |
prevline = line | |
continue | |
diff = difflib.ndiff(prevline, line) | |
display_diff(diff) | |
prevline = line | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment