aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/hldiff.py
blob: e12f6887cfbe6f8d981d1d520d4e388a47cd71da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/env python3
from colorama import Fore, Style
import sys
import re

patch = sys.stdin.read().replace("\r\n", "\n")
stat_re = re.compile(r'(\+*)(\-*)')

hit_diff = False
for line in patch.split("\n"):
    if line.startswith("diff "):
        hit_diff = True
        print(line)
        continue
    if hit_diff:
        if line.startswith("-"):
            print(f"{Fore.RED}{line}{Style.RESET_ALL}")
        elif line.startswith("+"):
            print(f"{Fore.GREEN}{line}{Style.RESET_ALL}")
        else:
            print(line)
    else:
        if line.startswith(" ") and "|" in line and ("+" in line or "-" in line):
            line = stat_re.sub(
                    f'{Fore.GREEN}\\1{Fore.RED}\\2{Style.RESET_ALL}',
                    line)
        print(line)