Skip to content

Instantly share code, notes, and snippets.

@themadsens
Last active December 1, 2023 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save themadsens/609033606987ea29ab6b148d2b1c7f78 to your computer and use it in GitHub Desktop.
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 :)
#!/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