MaKaNu
MaKaNu

Reputation: 1018

How to color a difflib.unified_diff

Do I have similar options like described here https://stackoverflow.com/a/64404008/10985257 with unified_diff?

My solution for now would be to apply the color based on the value a string starts with.

text1 = ["1\n", "2\n", "4\n"]
text2 = ["1\n", "2\n", "3\n", "5\n"]

def map_colored(string):
    if string.startswith("-"):
        return f"\033[38;2;255;0;0m{string}\033[38;2;255;255;255m"
    elif string.startswith("+"):
        return f"\033[38;2;0;255;0m{string}\033[38;2;255;255;255m"
    else:
        return string

result_colored = map(map_colored, result)
sys.stdout.writelines(result_colored)

Exists something similar like get_opcodes for unified_diff?

Upvotes: 0

Views: 92

Answers (0)

Related Questions